Skip to content

Commit

Permalink
refactor(Effects): move OnRunEffects to lifecycle_hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
timdeschryver committed Dec 4, 2018
1 parent efa3cb9 commit 8980112
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 39 deletions.
29 changes: 27 additions & 2 deletions modules/effects/src/effect_sources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@ import {
} from 'rxjs/operators';

import { verifyOutput } from './effect_notification';
import { resolveEffectSource } from './effects_resolver';
import { mergeEffects } from './effects_resolver';
import { getSourceForInstance } from './effects_metadata';
import { onIdentifyEffectsKey } from './lifecycle_hooks';
import {
onIdentifyEffectsKey,
onRunEffectsKey,
onRunEffectsFn,
OnRunEffects,
} from './lifecycle_hooks';

@Injectable()
export class EffectSources extends Subject<any> {
Expand Down Expand Up @@ -61,3 +66,23 @@ function effectsInstance(sourceInstance: any) {

return '';
}

function resolveEffectSource(sourceInstance: any) {
const mergedEffects$ = mergeEffects(sourceInstance);

if (isOnRunEffects(sourceInstance)) {
return sourceInstance.ngrxOnRunEffects(mergedEffects$);
}

return mergedEffects$;
}

function isOnRunEffects(sourceInstance: {
[onRunEffectsKey]?: onRunEffectsFn;
}): sourceInstance is OnRunEffects {
const source = getSourceForInstance(sourceInstance);

return (
onRunEffectsKey in source && typeof source[onRunEffectsKey] === 'function'
);
}
11 changes: 0 additions & 11 deletions modules/effects/src/effects_resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { ignoreElements, map, materialize } from 'rxjs/operators';

import { EffectNotification } from './effect_notification';
import { getSourceForInstance, getSourceMetadata } from './effects_metadata';
import { isOnRunEffects } from './on_run_effects';

export function mergeEffects(
sourceInstance: any
Expand Down Expand Up @@ -40,13 +39,3 @@ export function mergeEffects(

return merge(...observables);
}

export function resolveEffectSource(sourceInstance: any) {
const mergedEffects$ = mergeEffects(sourceInstance);

if (isOnRunEffects(sourceInstance)) {
return sourceInstance.ngrxOnRunEffects(mergedEffects$);
}

return mergedEffects$;
}
3 changes: 1 addition & 2 deletions modules/effects/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ export { mergeEffects } from './effects_resolver';
export { Actions, ofType } from './actions';
export { EffectsModule } from './effects_module';
export { EffectSources } from './effect_sources';
export { OnRunEffects } from './on_run_effects';
export { EffectNotification } from './effect_notification';
export { ROOT_EFFECTS_INIT } from './effects_root_module';
export { UPDATE_EFFECTS, UpdateEffects } from './effects_feature_module';
export { OnIdentifyEffects } from './lifecycle_hooks';
export { OnIdentifyEffects, OnRunEffects } from './lifecycle_hooks';
44 changes: 44 additions & 0 deletions modules/effects/src/lifecycle_hooks.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { Observable } from 'rxjs';
import { EffectNotification } from '.';

/**
* @description
* Interface to set an identifier for effect instances.
Expand Down Expand Up @@ -35,3 +38,44 @@ export interface OnIdentifyEffects {

export const onIdentifyEffectsKey: keyof OnIdentifyEffects =
'ngrxOnIdentifyEffects';

export type onRunEffectsFn = (
resolvedEffects$: Observable<EffectNotification>
) => Observable<EffectNotification>;

/**
* @description
* Interface to control the lifecycle of effects.
*
* By default, effects are merged and subscribed to the store. Implement the OnRunEffects interface to control the lifecycle of the resolved effects.
*
* @usageNotes
*
* ### Implement the OnRunEffects interface on an Effects class
*
* ```ts
* export class UserEffects implements OnRunEffects {
* constructor(private actions$: Actions) {}
*
* ngrxOnRunEffects(resolvedEffects$: Observable<EffectNotification>) {
* return this.actions$.pipe(
* ofType('LOGGED_IN'),
* exhaustMap(() =>
* resolvedEffects$.pipe(
* takeUntil(this.actions$.pipe(ofType('LOGGED_OUT')))
* )
* )
* );
* }
* }
* ```
*/
export interface OnRunEffects {
/**
* @description
* Method to control the lifecycle of effects.
*/
ngrxOnRunEffects: onRunEffectsFn;
}

export const onRunEffectsKey: keyof OnRunEffects = 'ngrxOnRunEffects';
24 changes: 0 additions & 24 deletions modules/effects/src/on_run_effects.ts

This file was deleted.

0 comments on commit 8980112

Please sign in to comment.