Skip to content

Commit

Permalink
refactor(effects): lifecycle methods guards (#2320)
Browse files Browse the repository at this point in the history
  • Loading branch information
timdeschryver authored and brandonroberts committed Jan 24, 2020
1 parent 4b302ec commit e8fe9fd
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 24 deletions.
30 changes: 9 additions & 21 deletions modules/effects/src/effect_sources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import {
onRunEffectsKey,
OnRunEffects,
onInitEffects,
isOnIdentifyEffects,
isOnRunEffects,
isOnInitEffects,
} from './lifecycle_hooks';
import { getSourceForInstance } from './utils';

Expand All @@ -44,10 +47,7 @@ export class EffectSources extends Subject<any> {
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());
}
})
Expand All @@ -72,11 +72,8 @@ export class EffectSources extends Subject<any> {
}

function effectsInstance(sourceInstance: any) {
if (
onIdentifyEffectsKey in sourceInstance &&
typeof sourceInstance[onIdentifyEffectsKey] === 'function'
) {
return sourceInstance[onIdentifyEffectsKey]();
if (isOnIdentifyEffects(sourceInstance)) {
return sourceInstance.ngrxOnIdentifyEffects();
}

return '';
Expand All @@ -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<OnRunEffects>
): sourceInstance is OnRunEffects {
const source = getSourceForInstance(sourceInstance);

return (
onRunEffectsKey in source && typeof source[onRunEffectsKey] === 'function'
);
}
28 changes: 25 additions & 3 deletions modules/effects/src/lifecycle_hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { Action } from '@ngrx/store';
*
* ```
*/
export interface OnIdentifyEffects {
export declare interface OnIdentifyEffects {
/**
* @description
* String identifier to differentiate effect instances.
Expand All @@ -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.
Expand Down Expand Up @@ -64,7 +70,7 @@ export const onIdentifyEffectsKey: keyof OnIdentifyEffects =
* }
* ```
*/
export interface OnRunEffects {
export declare interface OnRunEffects {
/**
* @description
* Method to control the lifecycle of effects.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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'
);
}

0 comments on commit e8fe9fd

Please sign in to comment.