-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(onBefore): Run onBefore hooks asynchronously.
BREAKING CHANGE: `onBefore` hooks are now run asynchronously like all the other hooks. - #### Old behavior Previously, the `onBefore` hooks were run in the same stackframe as `transitionTo`. If they threw an error, it could be caught using try/catch. ```js transitionService.onBefore({ to: 'foo' }), () => { throw new Error('doh'); }); try { stateService.go('foo'); } catch (error) { // handle error } ``` - #### New behavior Now, `onBefore` hooks are processed asynchronously. To handle errors, use any of the async error handling paradigms: - Chain off the promise ```js transitionService.onBefore({ to: 'foo' }), () => { throw new Error('doh'); }); stateService.go('foo').catch(error => { //handle error }); ``` - Define an error handler ```js transitionService.onBefore({ to: 'foo' }), () => { throw new Error('doh'); }); transitionService.onError({ to: 'foo' }), () => { // handle error }); stateService.go('foo'); ``` - Use the global defaultErrorHandler ```js transitionService.onBefore({ to: 'foo' }), () => { throw new Error('doh'); }); stateService.go('foo'); stateService.defaultErrorHandler(error => { // global error handler }); ``` - #### Motivation Why introduce a BC? - No subtle behavior differences by hook type - Simpler code and mental model - Fewer edge cases to account for - #### BC Liklihood How likely is this to affect my app? Very Low: Apps that registered onBefore hooks and depend on synchronous execution are affected. - #### BC Severity How severe is this BC? Low: Switch to asynchronous handling, such as chaining off the transition promise
- Loading branch information
1 parent
07c0ae4
commit 30b82aa
Showing
6 changed files
with
119 additions
and
99 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,23 @@ | ||
|
||
import { trace } from '../common/trace'; | ||
import { Rejection } from '../transition/rejectFactory'; | ||
import { TransitionService } from '../transition/transitionService'; | ||
import { Transition } from '../transition/transition'; | ||
|
||
/** | ||
* A [[TransitionHookFn]] that skips a transition if it should be ignored | ||
* | ||
* This hook is invoked at the end of the onBefore phase. | ||
* | ||
* If the transition should be ignored (because no parameter or states changed) | ||
* then the transition is ignored and not processed. | ||
*/ | ||
function ignoredHook(trans: Transition) { | ||
if (trans.ignored()) { | ||
trace.traceTransitionIgnored(this); | ||
return Rejection.ignored().toPromise(); | ||
} | ||
} | ||
|
||
export const registerIgnoredTransitionHook = (transitionService: TransitionService) => | ||
transitionService.onBefore({}, ignoredHook, { priority: -9999 }); |
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,18 @@ | ||
import { TransitionService } from '../transition/transitionService'; | ||
import { Transition } from '../transition/transition'; | ||
|
||
/** | ||
* A [[TransitionHookFn]] that rejects the Transition if it is invalid | ||
* | ||
* This hook is invoked at the end of the onBefore phase. | ||
* If the transition is invalid (for example, param values do not validate) | ||
* then the transition is rejected. | ||
*/ | ||
function invalidTransitionHook(trans: Transition) { | ||
if (!trans.valid()) { | ||
throw new Error(trans.error()); | ||
} | ||
} | ||
|
||
export const registerInvalidTransitionHook = (transitionService: TransitionService) => | ||
transitionService.onBefore({}, invalidTransitionHook, { priority: -10000 }); |
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