Skip to content

Instantly share code, notes, and snippets.

@wesleygrimes
Last active May 31, 2018 15:21
Show Gist options
  • Save wesleygrimes/d31728ae8ebf99db7170bf896ea2c833 to your computer and use it in GitHub Desktop.
Save wesleygrimes/d31728ae8ebf99db7170bf896ea2c833 to your computer and use it in GitHub Desktop.
Entity Feature Store Module - Effects.ts
import { Injectable } from '@angular/core';
import { Actions, Effect, ofType } from '@ngrx/effects';
import { Action } from '@ngrx/store';
import { Observable, of as observableOf } from 'rxjs';
import { catchError, map, startWith, switchMap } from 'rxjs/operators';
import { DataService } from '../../services/data.service';
import * as featureActions from './actions';
@Injectable()
export class MyFeatureStoreEffects {
constructor(private dataService: DataService, private actions$: Actions) {}
@Effect()
loadRequestEffect$: Observable<Action> = this.actions$.pipe(
ofType<featureActions.LoadRequestAction>(
featureActions.ActionTypes.LOAD_REQUEST
),
startWith(new featureActions.LoadRequestAction()),
switchMap(action =>
this.dataService
.getItems()
.pipe(
map(
items =>
new featureActions.LoadSuccessAction({
items
})
),
catchError(error =>
observableOf(new featureActions.LoadFailureAction({ error }))
)
)
)
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment