forked from ngrx/platform
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patheffects_metadata.ts
More file actions
45 lines (38 loc) · 1.33 KB
/
effects_metadata.ts
File metadata and controls
45 lines (38 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { merge } from 'rxjs/observable/merge';
import { ignoreElements } from 'rxjs/operator/ignoreElements';
import { Observable } from 'rxjs/Observable';
import { compose } from '@ngrx/store';
const METADATA_KEY = '__@ngrx/effects__';
const r: any = Reflect;
export interface EffectMetadata {
propertyName: string;
dispatch: boolean;
}
function getEffectMetadataEntries(sourceProto: any): EffectMetadata[] {
return sourceProto.constructor[METADATA_KEY] || [];
}
function setEffectMetadataEntries(sourceProto: any, entries: EffectMetadata[]) {
const constructor = sourceProto.constructor;
const meta: EffectMetadata[] = constructor.hasOwnProperty(METADATA_KEY)
? (constructor as any)[METADATA_KEY]
: Object.defineProperty(constructor, METADATA_KEY, { value: [] })[
METADATA_KEY
];
Array.prototype.push.apply(meta, entries);
}
/**
* @ExportDecoratedItems
*/
export function Effect({ dispatch } = { dispatch: true }): PropertyDecorator {
return function(target: any, propertyName: string) {
const metadata: EffectMetadata = { propertyName, dispatch };
setEffectMetadataEntries(target, [metadata]);
};
}
export function getSourceForInstance(instance: Object): any {
return Object.getPrototypeOf(instance);
}
export const getSourceMetadata = compose(
getEffectMetadataEntries,
getSourceForInstance
);