Store<S extends StoreState> constructor

Store<S extends StoreState>({
  1. required S initialState,
  2. Reducer<S>? reducer,
  3. List<Effect<S>>? effects,
})

Implementation

Store({
  required S initialState,
  this.reducer,
  this.effects,
}) {
  state.add(initialState);
  if (reducer != null) {
    _actionStream.listen((StoreAction action) {
      S newState = reducer!(state.value, action);
      state.add(newState);
      _effectsActionStream.add(action);
    });
  }
  if (effects != null) {
    for (var effect in effects!) {
      effect(_effectsActionStream, this).listen((effectResult) {
        if (effectResult is StoreAction) {
          dispatch(effectResult);
        }
        if (effectResult is List &&
            effectResult.every((element) => element is StoreAction)) {
          for (var action in effectResult) {
            dispatch(action);
          }
        }
      });
    }
  }
}