dispatch<T extends JState> method

  1. @override
Future<void> dispatch<T extends JState>(
  1. JIntent<T> intent,
  2. JController<T> controller
)
override

Dispatches the given intent to the provided controller.

The intent represents an action or event that can modify the controller's state. The implementation should ensure the intent is invoked appropriately.

Implementation

@override
Future<void> dispatch<T extends JState>(
  JIntent<T> intent,
  JController<T> controller,
) async {
  if (!controller.mounted) {
    throw StateError('JController has been disposed.');
  }
  final slot = (controller, _keyOf(intent));
  // An instance that is still executing cannot be started again, even when
  // a newer intent already replaced it in the slot: reject the overlap
  // before cancelling or replacing anything.
  if (isIntentRunning(intent)) {
    throw StateError('${intent.runtimeType} is already running.');
  }
  _running[slot]?.cancel();
  _running[slot] = intent;
  try {
    await intent.run(controller);
    JObserver.notifyIntentCompleted(intent);
  } finally {
    // A newer intent may already own the slot; only release our own.
    if (identical(_running[slot], intent)) _running.remove(slot);
  }
}