Skip to content

Commit

Permalink
feat(Effects): Add lettable ofType operator
Browse files Browse the repository at this point in the history
Introduces lettable operator for Actions.ofType.

BREAKING CHANGE:

Updates minimum version of RxJS dependency.

BEFORE:

Minimum peer dependency of RxJS ^5.0.0

AFTER:

Minimum peer dependency of RxJS ^5.5.0
  • Loading branch information
brandonroberts authored and MikeRyanDev committed Dec 18, 2017
1 parent caf6c8d commit d5e1814
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
7 changes: 3 additions & 4 deletions modules/effects/spec/actions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import {
ScannedActionsSubject,
ActionsSubject,
} from '@ngrx/store';
import { Actions } from '../';
import { Actions, ofType } from '../';
import { map, toArray } from 'rxjs/operators';

describe('Actions', function() {
let actions$: Actions;
Expand Down Expand Up @@ -64,9 +65,7 @@ describe('Actions', function() {
const expected = actions.filter(type => type === ADD);

actions$
.ofType(ADD)
.map(update => update.type)
.toArray()
.pipe(ofType(ADD), map(update => update.type), toArray())
.subscribe({
next(actual) {
expect(actual).toEqual(expected);
Expand Down
11 changes: 9 additions & 2 deletions modules/effects/src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Action, ScannedActionsSubject } from '@ngrx/store';
import { Observable } from 'rxjs/Observable';
import { Operator } from 'rxjs/Operator';
import { filter } from 'rxjs/operator/filter';
import { OperatorFunction } from 'rxjs/interfaces';

@Injectable()
export class Actions<V = Action> extends Observable<V> {
Expand All @@ -22,8 +23,14 @@ export class Actions<V = Action> extends Observable<V> {
}

ofType<V2 extends V = V>(...allowedTypes: string[]): Actions<V2> {
return filter.call(this, (action: Action) =>
return ofType<any>(...allowedTypes)(this.source as Actions<V2>);
}
}

export function ofType<T extends Action>(...allowedTypes: string[]) {
return function ofTypeOperator(source$: Actions<T>): Actions<T> {
return filter.call(source$, (action: Action) =>
allowedTypes.some(type => type === action.type)
);
}
};
}
2 changes: 1 addition & 1 deletion modules/effects/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export {
getEffectsMetadata,
} from './effects_metadata';
export { mergeEffects } from './effects_resolver';
export { Actions } from './actions';
export { Actions, ofType } from './actions';
export { EffectsModule } from './effects_module';
export { EffectSources } from './effect_sources';
export { OnRunEffects } from './on_run_effects';
Expand Down

0 comments on commit d5e1814

Please sign in to comment.