-
Notifications
You must be signed in to change notification settings - Fork 406
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Action stream consolidation #324
Merged
amcdnl
merged 17 commits into
ngxs:master
from
markwhitfeld:action_stream_consolidation
Apr 28, 2018
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
f0a75ee
refactor: extract dispatcher class from store
abdd19a
refactor: decouple dispatcher from action handlers
2f0099c
fix: ensure actions appear in action stream in correct order
c891d45
fix: dispatcher should call action handlers synchronously
fb6c891
fix: dispatcher observable should be called synchronously if possible
12aaad1
test: fix tests to expected events that at each point in the lifecycle
1cbd1c0
fix: canceled action should notify of cancellation
083ad11
feat: add ofActionCanceled operator
94eabb1
fix: correct spelling of cancelled (UK) to canceled (US)
8f0c382
refactor: extract InternalStateOperations for creation of StateContext
e8ed37e
fix: the Errored status should also be capitalized (issue #313)
4216345
fix: the complete actions should be called before the dispatch subscribe
8da51af
refactor: clean up Dispatcher code
6f16968
refactor: clean up StateFactory code
82af6f8
docs: add API description for new OrderedSubject class
b27c159
fix: the action from the plugins should be used for the action stream
6c236bb
Merge branch 'master' into action_stream_consolidation
markwhitfeld File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,101 @@ | ||
import { Injectable, ErrorHandler } from '@angular/core'; | ||
import { Observable, of, forkJoin, empty, Subject } from 'rxjs'; | ||
import { catchError, shareReplay, filter, exhaustMap, take } from 'rxjs/operators'; | ||
|
||
import { compose } from './compose'; | ||
import { InternalActions, ActionStatus, ActionContext } from './actions-stream'; | ||
import { StateStream } from './state-stream'; | ||
import { PluginManager } from './plugin-manager'; | ||
|
||
/** | ||
* Internal Action result stream that is emitted when an action is completed. | ||
* This is used as a method of returning the action result to the dispatcher | ||
* for the observable returned by the dispatch(...) call. | ||
* The dispatcher then asynchronously pushes the result from this stream onto the main action stream as a result. | ||
*/ | ||
@Injectable() | ||
export class InternalDispatchedActionResults extends Subject<ActionContext> {} | ||
|
||
@Injectable() | ||
export class InternalDispatcher { | ||
constructor( | ||
private _errorHandler: ErrorHandler, | ||
private _actions: InternalActions, | ||
private _actionResults: InternalDispatchedActionResults, | ||
private _pluginManager: PluginManager, | ||
private _stateStream: StateStream | ||
) {} | ||
|
||
/** | ||
* Dispatches event(s). | ||
*/ | ||
dispatch(event: any | any[]): Observable<any> { | ||
let result: Observable<any>; | ||
|
||
if (Array.isArray(event)) { | ||
result = forkJoin(event.map(a => this.dispatchSingle(a))); | ||
} else { | ||
result = this.dispatchSingle(event); | ||
} | ||
|
||
result.pipe( | ||
catchError(err => { | ||
// handle error through angular error system | ||
this._errorHandler.handleError(err); | ||
return of(err); | ||
}) | ||
); | ||
|
||
result.subscribe(); | ||
|
||
return result; | ||
} | ||
|
||
private dispatchSingle(action: any): Observable<any> { | ||
const prevState = this._stateStream.getValue(); | ||
const plugins = this._pluginManager.plugins; | ||
|
||
return compose([ | ||
...plugins, | ||
(nextState, nextAction) => { | ||
if (nextState !== prevState) { | ||
this._stateStream.next(nextState); | ||
} | ||
const actionResult$ = this.getActionResultStream(nextAction); | ||
actionResult$.subscribe(ctx => this._actions.next(ctx)); | ||
this._actions.next({ action: nextAction, status: ActionStatus.Dispatched }); | ||
return this.createDispatchObservable(actionResult$); | ||
} | ||
])(prevState, action) as Observable<any>; | ||
} | ||
|
||
private getActionResultStream(action: any): Observable<ActionContext> { | ||
const actionResult$ = this._actionResults.pipe( | ||
filter((ctx: ActionContext) => { | ||
return ctx.action === action && ctx.status !== ActionStatus.Dispatched; | ||
}), | ||
take(1), | ||
shareReplay() | ||
); | ||
return actionResult$; | ||
} | ||
|
||
private createDispatchObservable(actionResult$: Observable<ActionContext>): Observable<any> { | ||
return actionResult$ | ||
.pipe( | ||
exhaustMap((ctx: ActionContext) => { | ||
switch (ctx.status) { | ||
case ActionStatus.Completed: | ||
return of(this._stateStream.getValue()); | ||
case ActionStatus.Errored: | ||
return of(this._stateStream.getValue()); // This was previously the error value | ||
// I think that this should rather | ||
// return throwError(new Error('the error goes here')) | ||
default: | ||
return empty(); | ||
} | ||
}) | ||
) | ||
.pipe(shareReplay()); | ||
} | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given the complexity of this function now, we should break it out into its own method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done