Last active
May 31, 2018 15:21
-
-
Save wesleygrimes/d31728ae8ebf99db7170bf896ea2c833 to your computer and use it in GitHub Desktop.
Entity Feature Store Module - Effects.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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