Skip to content

Instantly share code, notes, and snippets.

@vteivans
Last active January 31, 2019 12:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save vteivans/da5adf19a94da9e32d27cb8b9d5b8884 to your computer and use it in GitHub Desktop.
Save vteivans/da5adf19a94da9e32d27cb8b9d5b8884 to your computer and use it in GitHub Desktop.
Simple ngrx effect example with `withLatestFrom` operator for blog post: https://medium.com/@viestursv/how-to-get-store-state-in-ngrx-effect-fab9e9c8f087#.oekqp1ucb
import { Store, Action } from '@ngrx/store';
import { Actions, Effect } from '@ngrx/effects';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/filter';
import 'rxjs/add/operator/withLatestFrom';
import { ADD_LINE } from './lines.reducer';
import { INC_PAGE_COUNT } from './pages.reducer';
import { AppState } from './app.store';
@Injectable()
export class LineEffects {
@Effect() increasePageCount$ = this.actions$
.ofType(ADD_LINE)
.withLatestFrom(this.store$)
.filter(([action: Action, storeState: AppState]) => {
return storeState.lines.length / 100 > storeState.pages.count;
})
.map((actionAndStoreState) => {
return {
type: INC_PAGE_COUNT
}
});
constructor (
private actions$: Actions,
private store$: Store<AppState>) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment