-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRouter.js
251 lines (220 loc) · 6.09 KB
/
Router.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import React, {Component} from 'react';
import { View } from 'react-native';
import Scene from './Scene';
import AnimationManager from './AnimationManager';
class RouterSingleton {
static setInstance(inst) {
this._inst = inst;
}
static getInstance() {
return this._inst;
}
}
export const Actions = {
push(routeName, params) {
const inst = RouterSingleton.getInstance();
inst._queue.push({
action: 'push',
routeName,
params
});
inst._triggerQueue();
},
pop() {
const inst = RouterSingleton.getInstance();
let action = 'pop';
if (inst.state.stack[inst.state.stack.length - 1].action === 'modal') {
action = 'popModal';
}
inst._queue.push({
action: action,
routeName: inst.state.stack[inst.state.stack.length - 1].routeName
});
inst._triggerQueue();
},
reset(routeName, params) {
const inst = RouterSingleton.getInstance();
inst._queue.push({
action: 'reset',
routeName,
params
});
inst._triggerQueue();
},
modal(routeName, params) {
const inst = RouterSingleton.getInstance();
inst._queue.push({
action: 'modal',
routeName,
params
});
inst._triggerQueue();
}
}
export class Router extends Component {
static getState() {
const inst = RouterSingleton.getInstance();
const index = inst.state.stack.length - 1;
const current = inst.state.stack[index];
const state = {
routeName: current.routeName,
params: current.params || {},
index
};
return state;
}
_animationManager = new AnimationManager(this.props.animations)
state = {
removed: false,
action: 'push',
stack: []
}
_queue = []
constructor(props) {
super(props);
RouterSingleton.setInstance(this);
}
_triggerQueue() {
if (!this._updating && this._queue.length > 0) {
let queuedAction = this._queue.shift();
if ((queuedAction.action === 'pop' || queuedAction.action === 'popModal') && this.state.stack.length <= 1) {
// Don't pop the last scene
return;
}
this._executeAction(queuedAction.action, queuedAction.routeName ? queuedAction.routeName : null, queuedAction.params);
} else {
this._updating = false;
}
}
_executeAction(action, routeName, params) {
if (routeName && !this.props.routes[routeName]) {
// Route does not exist
return;
}
if (action === 'push' || action === 'modal') {
const newStack = this.state.stack;
newStack.push({
routeName,
params,
action
});
this.setState({
action,
stack: newStack
});
} else if (action === 'pop' || action === 'popModal') {
this.setState({
action: action
});
} else if (action === 'reset') {
this.setState({
action: 'reset',
stack: routeName ? [{routeName}] : [this.state.stack[0]]
})
}
}
_removeAfterPop() {
this._triggerAfterUpdate = true;
const newStack = this.state.stack;
newStack.pop();
this.setState({
action: newStack[newStack.length - 1].action,
stack: newStack
});
}
_getAction(index) {
let action = null;
const nbScenes = this.state.stack.length;
if (index === nbScenes - 1) {
// Last scene
action = this.state.action;
} else if (index === nbScenes - 2) {
const scene = this.state.stack[index];
if (scene.action === 'modal' || this.state.action === 'modal' || this.state.action === 'popModal') {
// If previous scene is a modal OR current action is to deal with a modal, use previous action
action = scene.action;
} else if (this.state.action === 'pop') {
// If focus previous scene
action = 'focus';
} else {
action = 'blur';
}
} else {
action = this.state.stack[index].action;
}
return action;
}
_getAnimation(index, action) {
const nbScenes = this.state.stack.length;
if (nbScenes > 1 && index >= nbScenes - 2) {
// One of the last two scenes
const prevLastRoute = this.state.stack[nbScenes - 2];
const lastRoute = this.state.stack[nbScenes - 1];
return this._animationManager.getAnimation(prevLastRoute.routeName, lastRoute.routeName, action);
}
return this._animationManager.getDefaultAnimation(action);
}
_onAnimationDone(index) {
this.state.stack[index]._isReady = true;
if (this.state.stack.every(route => route._isReady)) {
// Every route has finished with the animation
if (this.state.action === 'pop' || this.state.action === 'popModal') {
// Action was to pop the screen, pop it first and go
this._removeAfterPop()
} else {
// Action was not 'pop', let's trigger now
this._updating = false;
this._triggerQueue();
}
}
}
componentWillUpdate() {
this._updating = true;
}
componentDidUpdate() {
if (this._updating && this._triggerAfterUpdate) {
this._triggerAfterUpdate = false;
this._updating = false;
this._triggerQueue();
}
}
componentWillMount() {
const newStack = this.state.stack;
newStack.push({
routeName: Object.keys(this.props.routes)[0]
});
this.setState({
action: 'reset',
stack: newStack
});
}
render() {
return (
<View style={{flex: 1}}>
{
this.state.stack.map((route, index) => {
const action = this._getAction(index);
if (action !== route.action) {
route._isReady = false;
}
const animation = this._getAnimation(index, action);
const RouteScreen = this.props.routes[route.routeName].screen;
return (
<Scene
key={index}
action={action}
routeName={route.routeName}
animation={animation}
onAnimationDone={action => {
this._onAnimationDone(index);
}}
>
<RouteScreen {...route.params} />
</Scene>
);
})
}
</View>
);
}
}