The SagaRunner
service allows you to run, pause, and stop saga effects dynamically:
const authEffect = createSaga(...);
@Component({
selector: 'profile-route',
template: '...'
})
class ProfileRoute{
constructor(private runner: SagaRunner){ }
routerOnActivate(){
this.runner.run(incrementEffect);
}
routerOnDeactivate(){
this.runner.stop(incrementEffect);
}
}
Sagas that are started during bootstrap use the root injector. However, a dynamically started saga may want to use a different injector to resolve the saga effect. This is especially the case when you are working on code splitting.
The SagaRunner
service is designed to work in tandem with Angular 2's hierarchical injector. To use a different injector, simply re-provide the SagaRunner
service in your component's providers array:
@Component({
selector: 'admin-route',
template: '...',
providers: [ SagaRunner ]
})
class AdminRoute{
constructor(runner: SagaRunner){
// runner uses this component's injector to resolve
// any saga effects, storing the resolved effect
// and subscription with the root SagaRunner
}
}
Resolves and starts a new saga, or resumes a previously paused saga.
Params
saga
SagaProvider Saga to begin running
sagaRunner.run(saga);
Stops a saga from running and discards the resolved saga.
Params
saga
SagaProvider Saga to stop
sagaRunner.stop(saga)
Pauses a saga but retains the resolved saga. In most cases you should stop
sagas. Only pause a saga if you are certain you will be restarting it.
Params
saga
SagaProvider Saga to pause
sagaRunner.pause(saga)