Skip to content

Latest commit

 

History

History
75 lines (58 loc) · 1.78 KB

saga-runner.md

File metadata and controls

75 lines (58 loc) · 1.78 KB

SagaRunner

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);
  }
}

Important Note About Injection

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
  }
}

SagaRunner API

Methods

run(saga)

Resolves and starts a new saga, or resumes a previously paused saga.

Params

  • saga SagaProvider Saga to begin running
sagaRunner.run(saga);

stop(saga)

Stops a saga from running and discards the resolved saga.

Params

  • saga SagaProvider Saga to stop
sagaRunner.stop(saga)

pause(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)