Skip to content

Instantly share code, notes, and snippets.

@machinescream
Created September 15, 2019 19:21
Show Gist options
  • Save machinescream/aa7d9b8ae2b664789aff77e9c69aa281 to your computer and use it in GitHub Desktop.
Save machinescream/aa7d9b8ae2b664789aff77e9c69aa281 to your computer and use it in GitHub Desktop.
class Bloc {
final broadcaster1 = StreamController<Model1>.broadcast();
final broadcaster2 = StreamController<Model2>.broadcast();
Stream<Model1> get stream1 => broadcaster1.stream;
Stream<Model2> get stream2 => broadcaster2.stream;
Bloc(){
subscribe();
}
StreamSubscription _model1Subscription;
StreamSubscription _model2Subscription;
void subscribe() {
_model1Subscription = Store().nextSubstate((AppState state) => state.subState1).listen((state) {
final model = Model1(state.value);
broadcaster1.add(model);
});
_model2Subscription = Store().nextSubstate((AppState state) => state.subState2).listen((state) {
final model = Model1(state.value);
broadcaster2.add(model);
});
}
void dispose() {
_model1Subscription?.cancel();
_model2Subscription?.cancel();
broadcaster1.close();
broadcaster2.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment