1
1
/** @module state */ /** for typedoc */
2
- import { copy } from "../../common/common" ;
3
2
import { prop } from "../../common/hof" ;
4
- import { Queue } from "../../common/queue" ;
5
3
import { Param } from "../../params/param" ;
6
4
7
5
import { TreeChanges } from "../../transition/interface" ;
@@ -15,6 +13,8 @@ import {ViewHooks} from "./viewHooks";
15
13
import { EnterExitHooks } from "./enterExitHooks" ;
16
14
import { ResolveHooks } from "./resolveHooks" ;
17
15
import { UrlRouter } from "../../url/urlRouter" ;
16
+ import { services } from "../../common/coreservices" ;
17
+ import { UIRouterGlobals } from "../../globals" ;
18
18
19
19
/**
20
20
* This class:
@@ -38,18 +38,17 @@ export class TransitionManager {
38
38
private enterExitHooks : EnterExitHooks ;
39
39
private viewHooks : ViewHooks ;
40
40
private resolveHooks : ResolveHooks ;
41
+ private $q ;
41
42
42
43
constructor (
43
44
private transition : Transition ,
44
45
private $transitions ,
45
46
private $urlRouter : UrlRouter ,
46
47
private $view , // service
47
48
private $state : StateService ,
48
- private $stateParams , // service/obj
49
- private $q , // TODO: get from runtime.$q
50
- private activeTransQ : Queue < Transition > ,
51
- private changeHistory : Queue < TreeChanges >
49
+ private globals : UIRouterGlobals
52
50
) {
51
+ this . $q = services . $q ;
53
52
this . viewHooks = new ViewHooks ( transition , $view ) ;
54
53
this . enterExitHooks = new EnterExitHooks ( transition ) ;
55
54
this . resolveHooks = new ResolveHooks ( transition ) ;
@@ -63,45 +62,26 @@ export class TransitionManager {
63
62
}
64
63
65
64
runTransition ( ) : Promise < any > {
66
- this . activeTransQ . clear ( ) ; // TODO: nuke this
67
- this . activeTransQ . enqueue ( this . transition ) ;
68
- this . $state . transition = this . transition ;
69
- let promise = this . transition . run ( )
65
+ this . globals . transitionHistory . enqueue ( this . transition ) ;
66
+ return this . transition . run ( )
70
67
. then ( ( trans : Transition ) => trans . to ( ) ) // resolve to the final state (TODO: good? bad?)
71
68
. catch ( error => this . transRejected ( error ) ) ; // if rejected, handle dynamic and redirect
72
-
73
- let always = ( ) => {
74
- this . activeTransQ . remove ( this . transition ) ;
75
- if ( this . $state . transition === this . transition ) this . transition = null ;
76
- } ;
77
-
78
- promise . then ( always , always ) ;
79
-
80
- return promise ;
81
69
}
82
70
83
71
registerUpdateGlobalState ( ) {
84
- this . transition . onFinish ( { } , this . updateGlobalState . bind ( this ) , { priority : - 10000 } ) ;
85
- }
86
-
87
- updateGlobalState ( ) {
88
- let { treeChanges, transition, $state, changeHistory} = this ;
89
- // Update globals in $state
90
- $state . $current = transition . $to ( ) ;
91
- $state . current = $state . $current . self ;
92
- changeHistory . enqueue ( treeChanges ) ;
93
- this . updateStateParams ( ) ;
72
+ // After globals.current is updated at priority: 10000
73
+ this . transition . onSuccess ( { } , this . updateUrl . bind ( this ) , { priority : 9999 } ) ;
94
74
}
95
75
96
76
transRejected ( error ) : ( StateDeclaration | Promise < any > ) {
97
- let { transition, $state, $stateParams , $ q} = this ;
77
+ let { transition, $state, $q} = this ;
98
78
// Handle redirect and abort
99
79
if ( error instanceof TransitionRejection ) {
100
80
if ( error . type === RejectType . IGNORED ) {
101
81
// Update $stateParmas/$state.params/$location.url if transition ignored, but dynamic params have changed.
102
82
let dynamic = $state . $current . parameters ( ) . filter ( prop ( 'dynamic' ) ) ;
103
- if ( ! Param . equals ( dynamic , $stateParams , transition . params ( ) ) ) {
104
- this . updateStateParams ( ) ;
83
+ if ( ! Param . equals ( dynamic , $state . params , transition . params ( ) ) ) {
84
+ this . updateUrl ( ) ;
105
85
}
106
86
return $state . current ;
107
87
}
@@ -120,22 +100,20 @@ export class TransitionManager {
120
100
return $q . reject ( error ) ;
121
101
}
122
102
123
- updateStateParams ( ) {
103
+ updateUrl ( ) {
124
104
let transition = this . transition ;
125
- let { $urlRouter, $state, $stateParams } = this ;
105
+ let { $urlRouter, $state} = this ;
126
106
let options = transition . options ( ) ;
127
- copy ( transition . params ( ) , $state . params ) ;
128
- copy ( $state . params , $stateParams ) ;
107
+ var toState = transition . $to ( ) ;
129
108
130
109
if ( options . location && $state . $current . navigable ) {
131
- $urlRouter . push ( $state . $current . navigable . url , $stateParams , { replace : options . location === 'replace' } ) ;
110
+ $urlRouter . push ( $state . $current . navigable . url , $state . params , { replace : options . location === 'replace' } ) ;
132
111
}
133
-
134
112
$urlRouter . update ( true ) ;
135
113
}
136
114
137
115
private _redirectMgr ( redirect : Transition ) : TransitionManager {
138
- let { $transitions, $urlRouter, $view, $state, $stateParams , $q , activeTransQ , changeHistory } = this ;
139
- return new TransitionManager ( redirect , $transitions , $urlRouter , $view , $state , $stateParams , $q , activeTransQ , changeHistory ) ;
116
+ let { $transitions, $urlRouter, $view, $state, globals } = this ;
117
+ return new TransitionManager ( redirect , $transitions , $urlRouter , $view , $state , globals ) ;
140
118
}
141
119
}
0 commit comments