update method

void update(
  1. T reducer(
    1. T state
    ), {
  2. JIntent<JState>? origin,
})

Updates the current state by applying the given reducer function.

The reducer receives the current state and returns a new modified state. Equivalent states (previous == next) are neither assigned nor reported to observers. After disposal the reducer is not called, and updates made by a cancelled intent (JIntent.isCancelled) are ignored. Reducers must return immutable states rather than mutate the current state in place.

  • reducer: A function that takes the current state and returns a new state.
  • origin: (Optional) The intent that triggered this state update. When omitted, the intent currently executing in this zone (JIntent.current) is used if it runs on this controller; updates from elsewhere keep a null origin. Callers outside an intent are unaffected.

Implementation

void update(T Function(T state) reducer, {JIntent? origin}) {
  if (!mounted) {
    if (kDebugMode) debugPrint('JController is no longer mounted.');
    return;
  }
  origin ??= _currentIntentOnThisController();
  if (origin != null && origin.isCancelled) return;
  final newState = reducer(currentState);
  if (!mounted) return;

  final prev = currentState;
  if (prev == newState) return;

  state = newState;

  JObserver.notifyStateChanged(prev, newState, origin);
}