runWithContext<Context> method

System<State, Event> runWithContext<Context>({
  1. required Context createContext(),
  2. required Disposer run(
    1. Context context,
    2. Run<State, Event> run,
    3. Reduce<State, Event>? nextReduce,
    4. Effect<State, Event>? nextEffect,
    5. Interceptor<Event>? nextInterceptor,
    ),
  3. void dispose(
    1. Context context
    )?,
})

Create a new system which can redefine how to run the system with a custom context.

This is a low level operator which can redefine how to run the system, It has ability to create a custom context associating it with the system, With a dispose callback to clean up the context.

It can be used for supporting other operator like system.eventInterceptor and system.runWithContext.

API Overview


class SomeContext { ... }

...

system
 .runWithContext<SomeContext>(
   createContext: () => SomeContext(), // create context here
   run: (context, run, nextReduce, nextEffect, nextInterceptor) {
     final effect = _redefineEffect(context, nextEffect);    // redefine next effect if needed
     final interceptor = _redefineInterceptor(context, nextInterceptor); // redefine interceptor if needed
     final _run = _redefineRun(context, run);                // redefine run if needed
     final disposer = _run(reduce: nextReduce, effect: effect, interceptor: interceptor);
     final _disposer = _redefineDisposer(context, disposer); // redefine disposer if needed
     return _disposer;
   },
   dispose: (context) {
     // dispose the context if needed.
   }
 )
 ...

Implementation

System<State, Event> runWithContext<Context>({
  required Context Function() createContext,
  required Disposer Function(
    Context context, Run<State, Event> run,
    Reduce<State, Event>? nextReduce,
    Effect<State, Event>? nextEffect,
    Interceptor<Event>? nextInterceptor
  ) run,
  void Function(Context context)? dispose,
}) {
  final localRun = run;
  return copy((run) => ({reduce, effect, interceptor}) {
    final context = createContext();
    final sourceDisposer = localRun(context, run, reduce, effect, interceptor);
    final combinedDisposer = dispose == null ? sourceDisposer : Disposer(() {
      dispose(context);
      sourceDisposer();
    });
    return combinedDisposer;
  });
}