Store<Model> constructor
Store<Model> ({})
Implementation
Store(
{required Model initialModel,
Cmd<Model> Function(Model initial)? initialCmd,
List<FutureOr<Stream<Msg<Model>>>> Function(Model initial)? subscriptions,
Cmd<Model> Function(Model current)? onDispose})
: _model = initialModel,
_onDispose = onDispose {
_msgSubscription = _msgController.stream.listen((msg) {
final old = _model;
if (msg.update != null) {
_model = msg.update!(_model);
if (_model != old) {
_modelController.add(_model);
}
}
if (msg.cmd != null) {
_cmdController.add(msg.cmd!(old));
}
});
_cmdSubscription = _cmdController.stream.listen((cmd) async {
if (cmd is CmdMsg<Model>) _msgController.add(await cmd.msg);
if (cmd is CmdEffect<Model>) cmd.effect();
if (cmd is CmdBatch<Model>) cmd.cmds.forEach(_cmdController.add);
if (cmd is CmdStream<Model>) cmd.cmds.forEach(_cmdController.add);
});
if (subscriptions != null) {
_wireSubscriptions(subscriptions(initialModel));
}
if (initialCmd != null) {
_cmdController.add(initialCmd(initialModel));
}
}