runWithContext<Context> method
System<State, Event>
runWithContext<Context>({
- required Context createContext(),
- required Disposer run(
- Context context,
- Run<
State, Event> run, - Reduce<
State, Event> ? nextReduce, - Effect<
State, Event> ? nextEffect, - Interceptor<
Event> ? nextInterceptor,
- void dispose(
- 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;
});
}