Skip to content

Commit

Permalink
introduce EffectPropertyKey type
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-okrushko committed Oct 30, 2019
1 parent 72ac133 commit 3151941
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 19 deletions.
6 changes: 3 additions & 3 deletions modules/effects/src/effect_decorator.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { compose } from '@ngrx/store';

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

const METADATA_KEY = '__@ngrx/effects__';

export function Effect({
dispatch = true,
resubscribeOnError = true,
}: EffectConfig = {}): PropertyDecorator {
return function<T extends Object, K extends Extract<keyof T, string>>(
}: EffectConfig = {}) {
return function<T extends Object, K extends EffectPropertyKey<T>>(
target: T,
propertyName: K
) {
Expand Down
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 3151941

Please sign in to comment.