Skip to content

Latest commit

 

History

History
88 lines (69 loc) · 2.29 KB

utilities.md

File metadata and controls

88 lines (69 loc) · 2.29 KB

Utilities API

createSaga(factory: SagaFactory, dependencies: any[])

Creates a saga in the context of the injector.

Params

  • factory SagaFactory Factory function called in the context of the injector. Must return a function that implements the Saga interface.
  • dependencies any[] Array of dependencies the factory function needs

Returns Provider

const authEffect = createSaga(function(http: Http) {
  return saga$ => saga$
    .filter(saga => saga.action.type === 'AUTH')
    .map(saga => saga.action.payload)
    .flatMap(payload => {
      return http.post('/auth', JSON.stringify(payload))
        .map(res => {
          return {
            type: 'AUTH_SUCESS',
            payload: res.json()
          }
        })
        .catch(error => Observable.of({
          type: 'AUTH_FAILED',
          payload: error.json()
        }));
    });
}, [ Http ]);

installSagaMiddleware(...sagas: Provider[])

Installs the saga middleware and initializes it to immediately begin running the provided sagas.

Params

  • ...sagas Provider[] Sagas you want to begin running immediately.

Returns Provider[]

boostrap(App, [
  provideStore(reducer),
  installSagaMiddleware(authEffect, signoutEffect)
]);

whenAction(actionType: string)

Filters a stream of SagaIterations to only include iterations with an action of the provided type.

Params

  • actionType string Action type to filter for

Returns (iteration: SagaIteration) => boolean

return saga$ => saga$
  .filter(whenAction('AUTH'))

toPayload

Function you can pass in to map a saga iteration to the payload of that iteration's action

return saga$ => saga$
  .map(toPayload)
  .do(payload => { ... });

applySelector<T,V>(selector: Observable => Observable)

Applies a selector function to the state part of a saga iteration, returning a new saga iteration composed of the action and the selected piece of state

Params

  • selector Observable => Observable Selector function to apply to the state

Returns Observable<SagaIteration<V>>

function selectTodos() {
  return (state$: Observable<{ todos: string[] }>) => state$
    .map(s => s.todos)
    .distinctUntilChanged();
}

return saga$ => saga$
  .let(applySelector(selectTodos))