Skip to content

Commit

Permalink
fix(nx): use pipeable ofType operator from ngrx
Browse files Browse the repository at this point in the history
fix #890
  • Loading branch information
rwoody authored and vsavkin committed Nov 12, 2018
1 parent e4f45af commit 3f493b7
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 50 deletions.
77 changes: 37 additions & 40 deletions packages/nx/spec/data-persistence.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Component, Injectable } from '@angular/core';
import { fakeAsync, TestBed, tick } from '@angular/core/testing';
import { Router, RouterStateSnapshot } from '@angular/router';
import { RouterTestingModule } from '@angular/router/testing';
import { Actions, Effect, EffectsModule } from '@ngrx/effects';
import { Actions, Effect, EffectsModule, ofType } from '@ngrx/effects';
import { provideMockActions } from '@ngrx/effects/testing';
import { StoreRouterConnectingModule } from '@ngrx/router-store';
import { Store, StoreModule } from '@ngrx/store';
Expand Down Expand Up @@ -265,19 +265,18 @@ describe('DataPersistence', () => {
});

@Effect()
loadTodosWithOperator = this.s.actions
.ofType<GetTodos>('GET_TODOS')
.pipe(
withLatestFrom(this.s.store),
fetch({
run: (action, state) => {
return of({
type: 'TODOS',
payload: { user: state.user, todos: 'some todos' }
}).pipe(delay(1));
}
})
);
loadTodosWithOperator = this.s.actions.pipe(
ofType<GetTodos>('GET_TODOS'),
withLatestFrom(this.s.store),
fetch({
run: (action, state) => {
return of({
type: 'TODOS',
payload: { user: state.user, todos: 'some todos' }
}).pipe(delay(1));
}
})
);

constructor(private s: DataPersistence<TodosState>) {}
}
Expand Down Expand Up @@ -395,18 +394,17 @@ describe('DataPersistence', () => {
});

@Effect()
loadTodoWithOperator = this.s.actions
.ofType<UpdateTodo>('UPDATE_TODO')
.pipe(
withLatestFrom(this.s.store),
pessimisticUpdate({
run: (a, state) => ({
type: 'TODO_UPDATED',
payload: { user: state.user, newTitle: a.payload.newTitle }
}),
onError: (a, e: any) => null
})
);
loadTodoWithOperator = this.s.actions.pipe(
ofType<UpdateTodo>('UPDATE_TODO'),
withLatestFrom(this.s.store),
pessimisticUpdate({
run: (a, state) => ({
type: 'TODO_UPDATED',
payload: { user: state.user, newTitle: a.payload.newTitle }
}),
onError: (a, e: any) => null
})
);

constructor(private s: DataPersistence<TodosState>) {}
}
Expand Down Expand Up @@ -576,21 +574,20 @@ describe('DataPersistence', () => {
});

@Effect()
loadTodoWithOperator = this.s.actions
.ofType<UpdateTodo>('UPDATE_TODO')
.pipe(
withLatestFrom(this.s.store),
optimisticUpdate({
run: (a, state) => {
throw new Error('boom');
},

undoAction: (a, e: any) => ({
type: 'UNDO_UPDATE_TODO',
payload: a.payload
})
loadTodoWithOperator = this.s.actions.pipe(
ofType<UpdateTodo>('UPDATE_TODO'),
withLatestFrom(this.s.store),
optimisticUpdate({
run: (a, state) => {
throw new Error('boom');
},

undoAction: (a, e: any) => ({
type: 'UNDO_UPDATE_TODO',
payload: a.payload
})
);
})
);

constructor(private s: DataPersistence<TodosState>) {}
}
Expand Down
26 changes: 16 additions & 10 deletions packages/nx/src/data-persistence.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable, Type } from '@angular/core';
import { ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { Actions } from '@ngrx/effects';
import { Actions, ofType } from '@ngrx/effects';
import { ROUTER_NAVIGATION, RouterNavigationAction } from '@ngrx/router-store';
import { Action, Store } from '@ngrx/store';
import { Observable, of } from 'rxjs';
Expand Down Expand Up @@ -233,9 +233,11 @@ export class DataPersistence<T> {
actionType: string,
opts: PessimisticUpdateOpts<T, A>
): Observable<any> {
return this.actions
.ofType<A>(actionType)
.pipe(withLatestFrom(this.store), pessimisticUpdate(opts));
return this.actions.pipe(
ofType<A>(actionType),
withLatestFrom(this.store),
pessimisticUpdate(opts)
);
}

/**
Expand Down Expand Up @@ -288,9 +290,11 @@ export class DataPersistence<T> {
actionType: string,
opts: OptimisticUpdateOpts<T, A>
): Observable<any> {
return this.actions
.ofType<A>(actionType)
.pipe(withLatestFrom(this.store), optimisticUpdate(opts));
return this.actions.pipe(
ofType<A>(actionType),
withLatestFrom(this.store),
optimisticUpdate(opts)
);
}

/**
Expand Down Expand Up @@ -364,9 +368,11 @@ export class DataPersistence<T> {
actionType: string,
opts: FetchOpts<T, A>
): Observable<any> {
return this.actions
.ofType<A>(actionType)
.pipe(withLatestFrom(this.store), fetch(opts));
return this.actions.pipe(
ofType<A>(actionType),
withLatestFrom(this.store),
fetch(opts)
);
}

/**
Expand Down

0 comments on commit 3f493b7

Please sign in to comment.