This package provides type assertions and methods to implement strongly typed NgRx effects. Implementation depends on Conditional types and can't be used without it.
After importing all actions like this
import * as Actions from "./example.actions";
export declare type Action = ActionsUnion<typeof Actions>;
instead of
export type Action = Actions.RouterNavigation | Actions.Publish | Actions.Published | ...
export type ActionTypes = ActionTypesUnion<typeof Actions>;
instead of
export type ActionTypes = "ROUTER_NAVIGATION" | "PUBLISH" | "PUBLISHED" ...
export const ActionTypes = CreateActionTypesEnum(Actions);
instead of
export enum ActionTypes: {
RouterNavigation = "ROUTER_NAVIGATION";
Publish = "PUBLISH";
Published = "PUBLISHED";
...
}
import { Injectable } from "@angular/core";
import { Actions, Effect } from "@ngrx/effects";
import { switchMap } from "rxjs/operators";
import { of } from "rxjs/observable/of";
import "rxjs/add/operator/switchMap";
import { ActionTypes, Action } from "./example.action-sets";
@Injectable()
export class ExampleEffects
{
constructor(protected readonly actions$: Actions<Action>) { }
@Effect() publishCommand$ = this.actions$.ofType(ActionTypes.Publish)
.switchMap(act =>
{
console.log(act.payload.name);
return of();
});
@Effect() publishedEvent$ = this.actions$.ofType(ActionTypes.Published)
.pipe(switchMap(act =>
{
console.log(act.payload.timestamp);
return of();
}));
}