send method

void send(
  1. A action
)

Dispatches an action to the store's reducer.

The reducer processes the action, producing a new state and an effect. The state is updated synchronously and observers are notified. The effect is then executed, which may emit further actions.

Actions dispatched during an active send cycle (e.g., from effects) are buffered and processed sequentially.

Implementation

void send(A action) {
  if (_send != null) {
    return _send!(action);
  }
  bufferedActions.append(action);
  if (_isSending) return;
  _isSending = true;
  //LOOP FOR ALL BUFFERED ACTIONS
  while (bufferedActions.isNotEmpty) {
    final action = bufferedActions.removeLast();
    _actionSubject.add(action);
    try {
      final (state: newState, effect: effect) = reducer(
        state,
        action,
      );
      //UPDATE STATE
      state = newState;
      _isSending = false;

      effect.run(
        (
          emit: send,
          dispose: ({id, shouldCancel = true}) =>
              id != null ? cancellableEffectHandler.dispose(id, shouldCancel: shouldCancel) : null,
          register: (cancellable, {id, cancelInFlight = false}) => id != null
              ? cancellableEffectHandler.register(
                  id,
                  cancellable,
                  cancelInFlight,
                )
              : null,
          guard: ({id}) => id != null ? cancellableEffectHandler.isUnique(id) : true,
        ),
      );
    } catch (e) {
      continue;
    } finally {
      _isSending = false;
    }
  }
}