Skip to content

refactor(effects): prepare for TS 3.5 #2191

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May be T extends Record<keyof T, Object> would be better?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's the same thing. Don't think we used Record at the codebase yet.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then fine :)

>(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;
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Woohoo! Love this change!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😃

}

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 ? '()' : ''}"`;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this change required? I thought Typescript knew that propertyName will be transformer to string under the hood

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question @L2jLiga ! I'm bringing the propertyName back to the actual allowed property types, which are string | number | Symbol.
Symbol cannot be implicitly converted to string, and needs some help.

I probably should rename propertyName to the propertyKey to be more descriptive. I'll leave it out for later. Thanks for asking!

}

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
};