From e8fe9fdc6f8603de949f8dc50701fe21a762f13f Mon Sep 17 00:00:00 2001 From: Tim Deschryver <28659384+timdeschryver@users.noreply.github.com> Date: Fri, 24 Jan 2020 03:12:18 +0100 Subject: [PATCH] refactor(effects): lifecycle methods guards (#2320) --- modules/effects/src/effect_sources.ts | 30 ++++++++------------------ modules/effects/src/lifecycle_hooks.ts | 28 +++++++++++++++++++++--- 2 files changed, 34 insertions(+), 24 deletions(-) diff --git a/modules/effects/src/effect_sources.ts b/modules/effects/src/effect_sources.ts index db7099cec0..57d7202cab 100644 --- a/modules/effects/src/effect_sources.ts +++ b/modules/effects/src/effect_sources.ts @@ -21,6 +21,9 @@ import { onRunEffectsKey, OnRunEffects, onInitEffects, + isOnIdentifyEffects, + isOnRunEffects, + isOnInitEffects, } from './lifecycle_hooks'; import { getSourceForInstance } from './utils'; @@ -44,10 +47,7 @@ export class EffectSources extends Subject { return source$.pipe( groupBy(effectsInstance), tap(() => { - if ( - onInitEffects in source$.key && - typeof source$.key[onInitEffects] === 'function' - ) { + if (isOnInitEffects(source$.key)) { this.store.dispatch(source$.key.ngrxOnInitEffects()); } }) @@ -72,11 +72,8 @@ export class EffectSources extends Subject { } function effectsInstance(sourceInstance: any) { - if ( - onIdentifyEffectsKey in sourceInstance && - typeof sourceInstance[onIdentifyEffectsKey] === 'function' - ) { - return sourceInstance[onIdentifyEffectsKey](); + if (isOnIdentifyEffects(sourceInstance)) { + return sourceInstance.ngrxOnIdentifyEffects(); } return ''; @@ -88,20 +85,11 @@ function resolveEffectSource( return sourceInstance => { const mergedEffects$ = mergeEffects(sourceInstance, errorHandler); - if (isOnRunEffects(sourceInstance)) { - return sourceInstance.ngrxOnRunEffects(mergedEffects$); + const source = getSourceForInstance(sourceInstance); + if (isOnRunEffects(source)) { + return source.ngrxOnRunEffects(mergedEffects$); } return mergedEffects$; }; } - -function isOnRunEffects( - sourceInstance: Partial -): sourceInstance is OnRunEffects { - const source = getSourceForInstance(sourceInstance); - - return ( - onRunEffectsKey in source && typeof source[onRunEffectsKey] === 'function' - ); -} diff --git a/modules/effects/src/lifecycle_hooks.ts b/modules/effects/src/lifecycle_hooks.ts index ada4d08c83..dc57fdcf41 100644 --- a/modules/effects/src/lifecycle_hooks.ts +++ b/modules/effects/src/lifecycle_hooks.ts @@ -26,7 +26,7 @@ import { Action } from '@ngrx/store'; * * ``` */ -export interface OnIdentifyEffects { +export declare interface OnIdentifyEffects { /** * @description * String identifier to differentiate effect instances. @@ -37,6 +37,12 @@ export interface OnIdentifyEffects { export const onIdentifyEffectsKey: keyof OnIdentifyEffects = 'ngrxOnIdentifyEffects'; +export function isOnIdentifyEffects( + instance: any +): instance is OnIdentifyEffects { + return isFunction(instance, onIdentifyEffectsKey); +} + /** * @description * Interface to control the lifecycle of effects. @@ -64,7 +70,7 @@ export const onIdentifyEffectsKey: keyof OnIdentifyEffects = * } * ``` */ -export interface OnRunEffects { +export declare interface OnRunEffects { /** * @description * Method to control the lifecycle of effects. @@ -76,6 +82,10 @@ export interface OnRunEffects { export const onRunEffectsKey: keyof OnRunEffects = 'ngrxOnRunEffects'; +export function isOnRunEffects(instance: any): instance is OnRunEffects { + return isFunction(instance, onRunEffectsKey); +} + /** * @description * Interface to dispatch an action after effect registration. @@ -96,7 +106,7 @@ export const onRunEffectsKey: keyof OnRunEffects = 'ngrxOnRunEffects'; * } * ``` */ -export interface OnInitEffects { +export declare interface OnInitEffects { /** * @description * Action to be dispatched after the effect is registered. @@ -105,3 +115,15 @@ export interface OnInitEffects { } export const onInitEffects: keyof OnInitEffects = 'ngrxOnInitEffects'; + +export function isOnInitEffects(instance: any): instance is OnInitEffects { + return isFunction(instance, onInitEffects); +} + +function isFunction(instance: any, functionName: string) { + return ( + instance && + functionName in instance && + typeof instance[functionName] === 'function' + ); +}