Skip to content
This repository was archived by the owner on Dec 26, 2017. It is now read-only.

Commit 8f0383d

Browse files
christophercrMikeRyanDev
authored andcommitted
feat(Effect): Support decorating a class method
`@Effect()` decorator can now decorate a method in addition to a property ```ts @Injectable() export class AuthEffects { constructor(private updates$) { } @effect() login() { return this.updates$.whenAction(...).mergeMap(...); } } ```
1 parent 2679029 commit 8f0383d

2 files changed

Lines changed: 16 additions & 4 deletions

File tree

lib/effects.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,14 @@ import { getEffectKeys } from './metadata';
55
import { flatten } from './util';
66

77
export function mergeEffects(...instances: any[]): Observable<any> {
8-
const observables = flatten(instances).map(i => getEffectKeys(i).map(key => i[key]));
8+
const observables: Observable<any>[] = flatten(instances).map((i: any): any => getEffectKeys(i).map(
9+
(key: string): Observable<any> => {
10+
if (typeof i[key] === 'function') {
11+
return i[key]();
12+
}
13+
return i[key];
14+
}
15+
));
916

1017
return Observable.merge(...flatten(observables));
1118
}

spec/effects.spec.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ describe('mergeEffects', function() {
1010
@Effect() a = Observable.of('a');
1111
@Effect() b = Observable.of('b');
1212
@Effect() c = Observable.of('c');
13+
@Effect() d() { return Observable.of('d'); }
1314
}
1415

1516
const mock = new Fixture();
16-
const expected = ['a', 'b', 'c'];
17+
const expected = ['a', 'b', 'c', 'd'];
1718

1819
mergeEffects(mock).toArray().subscribe({
1920
next(actual) {
@@ -37,9 +38,13 @@ describe('mergeEffects', function() {
3738
@Effect() c = Observable.of('c');
3839
}
3940

40-
const expected = ['a', 'b', 'c'];
41+
class Fourth {
42+
@Effect() d() { return Observable.of('d'); }
43+
}
44+
45+
const expected = ['a', 'b', 'c', 'd'];
4146

42-
mergeEffects([ new First(), new Second(), new Third() ]).toArray().subscribe({
47+
mergeEffects([ new First(), new Second(), new Third(), new Fourth() ]).toArray().subscribe({
4348
next(actual) {
4449
expect(actual).toEqual(expected);
4550
},

0 commit comments

Comments
 (0)