Skip to content

Commit ad30d40

Browse files
robwormaldMikeRyanDev
authored andcommitted
fix(Effects): Simplify decorator handling for Closure compatibility
1 parent 7fa0cf2 commit ad30d40

File tree

2 files changed

+10
-61
lines changed

2 files changed

+10
-61
lines changed

modules/effects/spec/effects_metadata.spec.ts

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,26 +23,6 @@ describe('Effect Metadata', () => {
2323
]);
2424
});
2525

26-
it('should get the effects metadata for a downleveled class instance', () => {
27-
class Fixture {
28-
static get propDecorators() {
29-
return {
30-
a: [{ type: Effect, args: [{ dispatch: false }] }],
31-
b: [{ type: Effect, args: [] }],
32-
c: [{ type: Effect }],
33-
};
34-
}
35-
}
36-
37-
const mock = new Fixture();
38-
39-
expect(getSourceMetadata(mock)).toEqual([
40-
{ propertyName: 'a', dispatch: false },
41-
{ propertyName: 'b', dispatch: true },
42-
{ propertyName: 'c', dispatch: true },
43-
]);
44-
});
45-
4626
it('should return an empty array if the class has not been decorated', () => {
4727
class Fixture {
4828
a: any;

modules/effects/src/effects_metadata.ts

Lines changed: 10 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,63 +3,32 @@ import { ignoreElements } from 'rxjs/operator/ignoreElements';
33
import { Observable } from 'rxjs/Observable';
44
import { compose } from '@ngrx/store';
55

6-
const METADATA_KEY = '@ngrx/effects';
6+
const METADATA_KEY = '__@ngrx/effects__';
77
const r: any = Reflect;
88

99
export interface EffectMetadata {
1010
propertyName: string;
1111
dispatch: boolean;
1212
}
1313

14-
function hasStaticMetadata(sourceType: any): boolean {
15-
return !!(sourceType as any).propDecorators;
16-
}
17-
18-
function getStaticMetadata(sourceType: any): EffectMetadata[] {
19-
const propDecorators = sourceType.propDecorators;
20-
return Object.keys(propDecorators).reduce(
21-
(all, key) => all.concat(getStaticMetadataEntry(propDecorators[key], key)),
22-
[]
23-
);
24-
}
25-
26-
function getStaticMetadataEntry(metadataEntry: any, propertyName: string) {
27-
return metadataEntry
28-
.filter((entry: any) => entry.type === Effect)
29-
.map((entry: any) => {
30-
let dispatch = true;
31-
if (entry.args && entry.args.length) {
32-
dispatch = !!entry.args[0].dispatch;
33-
}
34-
return { propertyName, dispatch };
35-
});
36-
}
37-
3814
function getEffectMetadataEntries(sourceProto: any): EffectMetadata[] {
39-
if (hasStaticMetadata(sourceProto.constructor)) {
40-
return getStaticMetadata(sourceProto.constructor);
41-
}
42-
43-
if (r.hasOwnMetadata(METADATA_KEY, sourceProto)) {
44-
return r.getOwnMetadata(METADATA_KEY, sourceProto);
45-
}
46-
47-
return [];
15+
return sourceProto.constructor[METADATA_KEY] || [];
4816
}
4917

5018
function setEffectMetadataEntries(sourceProto: any, entries: EffectMetadata[]) {
51-
r.defineMetadata(METADATA_KEY, entries, sourceProto);
19+
const constructor = sourceProto.constructor;
20+
const meta: EffectMetadata[] = constructor.hasOwnProperty(METADATA_KEY)
21+
? (constructor as any)[METADATA_KEY]
22+
: Object.defineProperty(constructor, METADATA_KEY, { value: [] })[
23+
METADATA_KEY
24+
];
25+
Array.prototype.push.apply(meta, entries);
5226
}
5327

54-
/**
55-
* @Annotation
56-
*/
5728
export function Effect({ dispatch } = { dispatch: true }): PropertyDecorator {
5829
return function(target: any, propertyName: string) {
59-
const effects: EffectMetadata[] = getEffectMetadataEntries(target);
6030
const metadata: EffectMetadata = { propertyName, dispatch };
61-
62-
setEffectMetadataEntries(target, [...effects, metadata]);
31+
setEffectMetadataEntries(target, [metadata]);
6332
};
6433
}
6534

0 commit comments

Comments
 (0)