Store<S> constructor

Store<S>({
  1. required Reducer<S> reducer,
  2. required S initialState,
  3. List<Module<S>> modules = const [],
})

Implementation

Store({
  required this.reducer,
  required this.initialState,
  this.modules = const []
}) {
  if(initialState != null)
    _stateChannel.send(StateAction(initialState, null));

  channel = _stateChannel.asStream();

  _dispatcher = modules.reversed.fold<Dispatcher>(
    (action) async => _stateChannel.send(
      StateAction(
        await reducer(action, _stateChannel.valueOrNull()?.state ?? initialState),
        action
      )
    ),
    (previousDispatcher, nextModule) => nextModule(
      _dispatchRoot,
      (registrar) => _registerInitialValueForModule(nextModule, registrar(this.initialState)),
      _stateChannel,
      previousDispatcher
    )
  );

  dispatch(MeowChannelInit());
}