-
-
Save truongluu/3ce47a6d4796a85a93f1f942e5b0e06e to your computer and use it in GitHub Desktop.
Two options for conditional ngrx effects
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
@Effect() | |
selectAndLoadStore$: Observable<Action> = this.actions$ | |
.ofType(storeActions.SELECT_AND_LOAD_STORE) | |
.withLatestFrom(this.store.select(ngrx.storeState)) | |
.map(([action, storeState]) => [action.payload, storeState]) | |
.switchMap(([storeName, storeState]) => { | |
const existsInStore = Boolean(storeState.urlNameMap[storeName]); | |
return Observable.if( | |
() => existsInStore, | |
Observable.of(new storeActions.SetSelectedStore(storeName)), | |
this.storeService.getByUrlName(storeName) | |
.map(store => new storeActions.LoadSelectedStoreSuccess(store)) | |
); | |
}); | |
@Effect() | |
selectAndLoadStore$: Observable<Action> = this.actions$ | |
.ofType(storeActions.SELECT_AND_LOAD_STORE) | |
.withLatestFrom(this.store.select(ngrx.storeState)) | |
.map(([action, storeState]) => [action.payload, storeState]) | |
.switchMap(([storeName, storeState]) => { | |
const existsInStore = Boolean(storeState.urlNameMap[storeName]); | |
let obs; | |
if (existsInStore) { | |
obs = Observable.of(new storeActions.SetSelectedStore(storeName)); | |
} else { | |
obs = this.storeService.getByUrlName(storeName) | |
.map(store => new storeActions.LoadSelectedStoreSuccess(store)); | |
} | |
return obs; | |
}); // @Effect() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment