-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(UIRouterGlobals): Create UIRouterGlobals
Created an isolated place to hold the public global mutable state fix(UIRouterGlobals): Only store the last successful transition and the last attempted transition closes #2525
- Loading branch information
1 parent
de252a0
commit 0eb7406
Showing
8 changed files
with
137 additions
and
88 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
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,72 @@ | ||
/** @module common */ /** */ | ||
import {StateParams} from "./params/stateParams"; | ||
import {StateDeclaration} from "./state/interface"; | ||
import {State} from "./state/stateObject"; | ||
import {Transition} from "./transition/transition"; | ||
import {Queue} from "./common/queue"; | ||
import {TransitionService} from "./transition/transitionService"; | ||
import {copy} from "./common/common"; | ||
|
||
/** | ||
* Global mutable state | ||
*/ | ||
export class UIRouterGlobals { | ||
/** | ||
* Current parameter values | ||
* | ||
* The parameter values from the latest successful transition | ||
*/ | ||
params: StateParams = new StateParams(); | ||
/** | ||
* Current state | ||
* | ||
* The to-state from the latest successful transition | ||
*/ | ||
current: StateDeclaration; | ||
/** | ||
* Current state | ||
* | ||
* The to-state from the latest successful transition | ||
*/ | ||
$current: State; | ||
/** | ||
* The current transition (in progress) | ||
*/ | ||
transition: Transition; | ||
/** | ||
* The transition history | ||
* | ||
* This queue's size is limited to a maximum number (default: 1) | ||
*/ | ||
transitionHistory = new Queue<Transition>([], 1); | ||
/** | ||
* The history of successful transitions | ||
* | ||
* This queue's size is limited to a maximum number (default: 1) | ||
*/ | ||
successfulTransitions = new Queue<Transition>([], 1); | ||
|
||
constructor(transitionService: TransitionService) { | ||
const beforeNewTransition = ($transition$) => { | ||
|
||
this.transition = $transition$; | ||
this.transitionHistory.enqueue($transition$); | ||
|
||
const updateGlobalState = () => { | ||
this.successfulTransitions.enqueue($transition$); | ||
this.$current = $transition$.$to(); | ||
this.current = this.$current.self; | ||
copy($transition$.params(), this.params); | ||
}; | ||
|
||
$transition$.onSuccess({}, updateGlobalState, {priority: 10000}); | ||
|
||
const clearCurrentTransition = () => { if (this.transition === $transition$) this.transition = null; }; | ||
|
||
$transition$.promise.finally(clearCurrentTransition) | ||
|
||
}; | ||
|
||
transitionService.onBefore({}, ['$transition$', beforeNewTransition]); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -32,6 +32,3 @@ export class StateParams { | |
}; | ||
} | ||
|
||
export function stateParamsFactory() { | ||
return new StateParams(); | ||
} |
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.