Created
July 18, 2024 09:34
-
-
Save Atsumi3/15c165824f5aa95bcf48b28b4b0062fc to your computer and use it in GitHub Desktop.
Flutterでイベントフローを実装するためのもの
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:async'; | |
class Router {} | |
final _controller = StreamController<Router>.broadcast(); | |
/// [Router]イベントを監視するmixin | |
mixin RouteEventObserver<R extends Router> { | |
StreamSubscription<Router>? _subscription; | |
void onRouteEventReceived(R router); | |
void subscribeToRouteEvent() { | |
_subscription = _controller.stream.listen((router) { | |
if (router is R) { | |
onRouteEventReceived(router); | |
} | |
}); | |
} | |
void cancelRouteEventSubscription() { | |
_subscription?.cancel(); | |
} | |
} | |
/// [Router]イベントを送信するmixin | |
mixin RouteEventEmitter { | |
void publishRouteEvent(Router router) => _controller.add(router); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment