Last active
March 2, 2022 08:03
-
-
Save Hwan-seok/8c0e602091b3ff710053131549f2e4c6 to your computer and use it in GitHub Desktop.
StateBroadcaster
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'; | |
void main() { | |
final broadcaster = StateBroadcaster(); | |
} | |
class StateBroadcaster { | |
final _sinkMap = <Type, StreamController<dynamic>>{}; | |
void notify<T>(T value) { | |
setMapIfNeeded(); | |
_sinkMap[T]?.add(value); | |
} | |
StreamSubscription<T> listen<T>(void Function(T) onData) { | |
setMapIfNeeded(); | |
return _sinkMap[T].stream.listen(onData); | |
} | |
void setMapIfNeeded<T>() { | |
_sinkMap[T] ??= StreamController<T>.broadcast(); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment