-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(Effects): Start child effects after running root effects (#43)
This removes the app initialization logic altogether. The 'OnRunEffects' lifecycle hook should be sufficient to control when your effects start.
- Loading branch information
1 parent
0be1bee
commit 931adb1
Showing
9 changed files
with
96 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ? '()' : ''}"`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters