runPausable<T> method

Future<T> runPausable<T>(
  1. FutureOr<void> onPause(),
  2. FutureOr<void> onResume(),
  3. FutureOr<T> action()
)

Execute an action that can be paused/resumed while it is being executed.

Parameters:

  • onPause: A callback that will pause the execution of the action.
  • onResume: A callback that will resume the execution of the action.
  • action: An action that supports pausing/resume on request.

Performs the following actions:

  • Adds a pause handler onPause
  • Adds a resume handler onResume
  • Executes the action function
  • Removes a pause handler onPause
  • Removes a resume handler onResume

The onPause handler function should initiate the pause procedure which pauses the execution of the action function.
The onResume handler function should initiate the resume procedure which resume the execution of the action function.

Implementation

Future<T> runPausable<T>(
  FutureOr<void> Function() onPause,
  FutureOr<void> Function() onResume,
  FutureOr<T> Function() action,
) async {
  final zone = Zone.current;
  _onPause[onPause] = zone;
  _onResume[onResume] = zone;
  try {
    return await action();
  } finally {
    _onPause.remove(onPause);
    _onResume.remove(onResume);
  }
}