Skip to content

Commit

Permalink
refactor(effects): prepare for TS 3.5 (ngrx#2191)
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-okrushko authored and jordanpowell88 committed Nov 14, 2019
1 parent 084efcd commit 1ee1353
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 25 deletions.
4 changes: 3 additions & 1 deletion modules/effects/src/effect_creator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ export function createEffect<
return effect;
}

export function getCreateEffectMetadata<T>(instance: T): EffectMetadata<T>[] {
export function getCreateEffectMetadata<
T extends { [props in keyof T]: Object }
>(instance: T): EffectMetadata<T>[] {
const propertyNames = Object.getOwnPropertyNames(instance) as Extract<
keyof T,
string
Expand Down
21 changes: 13 additions & 8 deletions modules/effects/src/effect_decorator.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { compose } from '@ngrx/store';
import { EffectMetadata, EffectConfig } from './models';

import { EffectConfig, EffectMetadata, EffectPropertyKey } from './models';
import { getSourceForInstance } from './utils';

const METADATA_KEY = '__@ngrx/effects__';

export function Effect<T>({
export function Effect({
dispatch = true,
resubscribeOnError = true,
}: EffectConfig = {}): PropertyDecorator {
return function<K extends Extract<keyof T, string>>(
}: EffectConfig = {}) {
return function<T extends Object, K extends EffectPropertyKey<T>>(
target: T,
propertyName: K
) {
Expand All @@ -21,7 +22,7 @@ export function Effect<T>({
resubscribeOnError,
};
setEffectMetadataEntries<T>(target, [metadata]);
} as (target: {}, propertyName: string | symbol) => void;
};
}

export function getEffectDecoratorMetadata<T>(
Expand All @@ -35,7 +36,7 @@ export function getEffectDecoratorMetadata<T>(
return effectsDecorators;
}

function setEffectMetadataEntries<T>(
function setEffectMetadataEntries<T extends object>(
sourceProto: T,
entries: EffectMetadata<T>[]
) {
Expand All @@ -50,8 +51,12 @@ function setEffectMetadataEntries<T>(
Array.prototype.push.apply(meta, entries);
}

function getEffectMetadataEntries<T>(sourceProto: T): EffectMetadata<T>[] {
function getEffectMetadataEntries<T extends object>(
sourceProto: T
): EffectMetadata<T>[] {
return sourceProto.constructor.hasOwnProperty(METADATA_KEY)
? (sourceProto.constructor as any)[METADATA_KEY]
? (sourceProto.constructor as typeof sourceProto.constructor & {
[METADATA_KEY]: EffectMetadata<T>[];
})[METADATA_KEY]
: [];
}
4 changes: 2 additions & 2 deletions modules/effects/src/effect_notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Notification, Observable } from 'rxjs';

export interface EffectNotification {
effect: Observable<any> | (() => Observable<any>);
propertyName: string;
propertyName: PropertyKey;
sourceName: string;
sourceInstance: any;
notification: Notification<Action | null | undefined>;
Expand Down Expand Up @@ -46,7 +46,7 @@ function getEffectName({
}: EffectNotification) {
const isMethod = typeof sourceInstance[propertyName] === 'function';

return `"${sourceName}.${propertyName}${isMethod ? '()' : ''}"`;
return `"${sourceName}.${String(propertyName)}${isMethod ? '()' : ''}"`;
}

function stringify(action: Action | null | undefined) {
Expand Down
21 changes: 10 additions & 11 deletions modules/effects/src/effects_metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@ import { getCreateEffectMetadata } from './effect_creator';
import { getEffectDecoratorMetadata } from './effect_decorator';

export function getEffectsMetadata<T>(instance: T): EffectsMetadata<T> {
const metadata: EffectsMetadata<T> = {};

for (const {
propertyName,
dispatch,
resubscribeOnError,
} of getSourceMetadata(instance)) {
metadata[propertyName] = { dispatch, resubscribeOnError };
}

return metadata;
return getSourceMetadata(instance).reduce(
(
acc: EffectsMetadata<T>,
{ propertyName, dispatch, resubscribeOnError }
) => {
acc[propertyName] = { dispatch, resubscribeOnError };
return acc;
},
{}
);
}

export function getSourceMetadata<T>(instance: T): EffectMetadata<T>[] {
Expand Down
12 changes: 9 additions & 3 deletions modules/effects/src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,16 @@ export interface EffectConfig {
resubscribeOnError?: boolean;
}

export interface EffectMetadata<T> extends Required<EffectConfig> {
propertyName: Extract<keyof T, string>;
export type EffectPropertyKey<T extends Object> = Exclude<
keyof T,
keyof Object
>;

export interface EffectMetadata<T extends Object>
extends Required<EffectConfig> {
propertyName: EffectPropertyKey<T>;
}

export type EffectsMetadata<T> = {
[key in Extract<keyof T, string>]?: EffectConfig
[key in EffectPropertyKey<T>]?: EffectConfig
};

0 comments on commit 1ee1353

Please sign in to comment.