Skip to content
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
58 changes: 58 additions & 0 deletions modules/effects/src/effect_notification.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { Observable } from 'rxjs/Observable';
import { Notification } from 'rxjs/Notification';
import { Action } from '@ngrx/store';
import { ErrorReporter } from './error_reporter';

export interface EffectNotification {
effect: Observable<any> | (() => Observable<any>);
propertyName: string;
sourceName: string;
sourceInstance: any;
notification: Notification<Action | null | undefined>;
}

export function verifyOutput(output: EffectNotification, reporter: ErrorReporter) {
reportErrorThrown(output, reporter);
reportInvalidActions(output, reporter);
}

function reportErrorThrown(output: EffectNotification, reporter: ErrorReporter) {
if (output.notification.kind === 'E') {
const errorReason = `Effect ${getEffectName(output)} threw an error`;

reporter.report(errorReason, {
Source: output.sourceInstance,
Effect: output.effect,
Error: output.notification.error,
Notification: output.notification,
});
}
}

function reportInvalidActions(output: EffectNotification, reporter: ErrorReporter) {
if (output.notification.kind === 'N') {
const action = output.notification.value;
const isInvalidAction = !isAction(action);

if (isInvalidAction) {
const errorReason = `Effect ${getEffectName(output)} dispatched an invalid action`;

reporter.report(errorReason, {
Source: output.sourceInstance,
Effect: output.effect,
Dispatched: action,
Notification: output.notification,
});
}
}
}

function isAction(action: any): action is Action {
return action && action.type && typeof action.type === 'string';
}

function getEffectName({ propertyName, sourceInstance, sourceName }: EffectNotification) {
const isMethod = typeof sourceInstance[propertyName] === 'function';

return `"${sourceName}.${propertyName}${isMethod ? '()' : ''}"`;
}
43 changes: 8 additions & 35 deletions modules/effects/src/effect_sources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,21 @@ import { mergeMap } from 'rxjs/operator/mergeMap';
import { exhaustMap } from 'rxjs/operator/exhaustMap';
import { map } from 'rxjs/operator/map';
import { dematerialize } from 'rxjs/operator/dematerialize';
import { concat } from 'rxjs/observable/concat';
import { Observable } from 'rxjs/Observable';
import { Subject } from 'rxjs/Subject';
import { Injectable, isDevMode } from '@angular/core';
import { Injectable } from '@angular/core';
import { Action } from '@ngrx/store';
import { EffectNotification, verifyOutput } from './effect_notification';
import { getSourceForInstance } from './effects_metadata';
import { resolveEffectSource, EffectNotification } from './effects_resolver';
import { resolveEffectSource } from './effects_resolver';
import { ErrorReporter } from './error_reporter';

@Injectable()
export class EffectSources extends Subject<any> {
constructor(private errorReporter: ErrorReporter) {
constructor(
private errorReporter: ErrorReporter,
) {
super();
}

Expand All @@ -32,38 +36,7 @@ export class EffectSources extends Subject<any> {
map.call(
exhaustMap.call(source$, resolveEffectSource),
(output: EffectNotification) => {
switch (output.notification.kind) {
case 'N': {
const action = output.notification.value;
const isInvalidAction =
!action || !action.type || typeof action.type !== 'string';

if (isInvalidAction) {
const errorReason = `Effect "${output.sourceName}.${output.propertyName}" dispatched an invalid action`;

this.errorReporter.report(errorReason, {
Source: output.sourceInstance,
Effect: output.effect,
Dispatched: action,
Notification: output.notification,
});
}

break;
}
case 'E': {
const errorReason = `Effect "${output.sourceName}.${output.propertyName}" threw an error`;

this.errorReporter.report(errorReason, {
Source: output.sourceInstance,
Effect: output.effect,
Error: output.notification.error,
Notification: output.notification,
});

break;
}
}
verifyOutput(output, this.errorReporter);

return output.notification;
},
Expand Down
6 changes: 3 additions & 3 deletions modules/effects/src/effects_feature_module.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { NgModule, Inject, Type } from '@angular/core';
import { EffectSources } from './effect_sources';
import { EffectsRootModule } from './effects_root_module';
import { FEATURE_EFFECTS } from './tokens';

@NgModule({})
export class EffectsFeatureModule {
constructor(
private effectSources: EffectSources,
private root: EffectsRootModule,
@Inject(FEATURE_EFFECTS) effectSourceGroups: any[][],
) {
effectSourceGroups.forEach(group =>
group.forEach(effectSourceInstance =>
effectSources.addEffects(effectSourceInstance),
root.addEffects(effectSourceInstance),
),
);
}
Expand Down
5 changes: 2 additions & 3 deletions modules/effects/src/effects_module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { EffectSources } from './effect_sources';
import { Actions } from './actions';
import { ROOT_EFFECTS, FEATURE_EFFECTS, CONSOLE } from './tokens';
import { EffectsFeatureModule } from './effects_feature_module';
import { EffectsRootModule } from './effects_root_module';
import { EffectsRunner } from './effects_runner';
import { ErrorReporter } from './error_reporter';
import { RUN_EFFECTS } from './run_effects';

@NgModule({})
export class EffectsModule {
Expand All @@ -26,13 +26,12 @@ export class EffectsModule {

static forRoot(rootEffects: Type<any>[]): ModuleWithProviders {
return {
ngModule: EffectsModule,
ngModule: EffectsRootModule,
providers: [
EffectsRunner,
EffectSources,
ErrorReporter,
Actions,
RUN_EFFECTS,
rootEffects,
{
provide: ROOT_EFFECTS,
Expand Down
9 changes: 1 addition & 8 deletions modules/effects/src/effects_resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,10 @@ import { map } from 'rxjs/operator/map';
import { Observable } from 'rxjs/Observable';
import { Notification } from 'rxjs/Notification';
import { Action } from '@ngrx/store';
import { EffectNotification } from './effect_notification';
import { getSourceMetadata, getSourceForInstance } from './effects_metadata';
import { isOnRunEffects } from './on_run_effects';

export interface EffectNotification {
effect: Observable<any> | (() => Observable<any>);
propertyName: string;
sourceName: string;
sourceInstance: any;
notification: Notification<Action | null | undefined>;
}

export function mergeEffects(
sourceInstance: any,
): Observable<EffectNotification> {
Expand Down
23 changes: 23 additions & 0 deletions modules/effects/src/effects_root_module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { NgModule, Inject } from '@angular/core';
import { EffectsRunner } from './effects_runner';
import { EffectSources } from './effect_sources';
import { ROOT_EFFECTS } from './tokens';

@NgModule({})
export class EffectsRootModule {
constructor(
private sources: EffectSources,
runner: EffectsRunner,
@Inject(ROOT_EFFECTS) rootEffects: any[],
) {
runner.start();

rootEffects.forEach(effectSourceInstance =>
sources.addEffects(effectSourceInstance)
);
}

addEffects(effectSourceInstance: any) {
this.sources.addEffects(effectSourceInstance);
}
}
2 changes: 1 addition & 1 deletion modules/effects/src/on_run_effects.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Observable } from 'rxjs/Observable';
import { getSourceForInstance } from './effects_metadata';
import { EffectNotification } from './effects_resolver';
import { EffectNotification } from './effect_notification';

export interface OnRunEffects {
ngrxOnRunEffects(
Expand Down
31 changes: 0 additions & 31 deletions modules/effects/src/run_effects.ts

This file was deleted.

3 changes: 0 additions & 3 deletions modules/effects/src/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ import { InjectionToken, Type } from '@angular/core';
export const IMMEDIATE_EFFECTS = new InjectionToken<any[]>(
'ngrx/effects: Immediate Effects',
);
export const BOOTSTRAP_EFFECTS = new InjectionToken<any[]>(
'ngrx/effects: Bootstrap Effects',
);
export const ROOT_EFFECTS = new InjectionToken<Type<any>[]>(
'ngrx/effects: Root Effects',
);
Expand Down