NavigationRouteStack.js
7.67 KB
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/**
* Copyright (c) 2015, Facebook, Inc. All rights reserved.
*
* Facebook, Inc. ("Facebook") owns all right, title and interest, including
* all intellectual property and other proprietary rights, in and to the React
* Native CustomComponents software (the "Software"). Subject to your
* compliance with these terms, you are hereby granted a non-exclusive,
* worldwide, royalty-free copyright license to (1) use and copy the Software;
* and (2) reproduce and distribute the Software as part of your own software
* ("Your Software"). Facebook reserves all rights not expressly granted to
* you in this license agreement.
*
* THE SOFTWARE AND DOCUMENTATION, IF ANY, ARE PROVIDED "AS IS" AND ANY EXPRESS
* OR IMPLIED WARRANTIES (INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE) ARE DISCLAIMED.
* IN NO EVENT SHALL FACEBOOK OR ITS AFFILIATES, OFFICERS, DIRECTORS OR
* EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THE SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @flow
*/
'use strict';
var immutable = require('immutable');
var invariant = require('fbjs/lib/invariant');
type IterationCallback = (route: any, index: number, key: string) => void;
var {List, Set} = immutable;
function isRouteEmpty(route: any): boolean {
return (route === undefined || route === null || route === '') || false;
}
var _nextID = 0;
class RouteNode {
key: string;
value: any;
constructor(route: any) {
// Key value gets bigger incrementally. Developer can compare the
// keys of two routes then know which route is added to the stack
// earlier.
this.key = String(_nextID++);
this.value = route;
}
}
var StackDiffRecord = immutable.Record({
key: null,
route: null,
index: null,
});
/**
* The immutable route stack.
*/
class RouteStack {
_index: number;
_routeNodes: List<RouteNode>;
constructor(index: number, routeNodes: List<RouteNode>) {
invariant(
routeNodes.size > 0,
'size must not be empty'
);
invariant(
index > -1 && index <= routeNodes.size - 1,
'index out of bound'
);
this._routeNodes = routeNodes;
this._index = index;
}
get size(): number {
return this._routeNodes.size;
}
get index(): number {
return this._index;
}
toArray(): Array<any> {
var result = [];
var ii = 0;
var nodes = this._routeNodes;
while (ii < nodes.size) {
result.push(nodes.get(ii).value);
ii++;
}
return result;
}
get(index: number): any {
if (index < 0 || index > this._routeNodes.size - 1) {
return null;
}
return this._routeNodes.get(index).value;
}
/**
* Returns the key associated with the route.
* When a route is added to a stack, the stack creates a key for this route.
* The key will persist until the initial stack and its derived stack
* no longer contains this route.
*/
keyOf(route: any): ?string {
if (isRouteEmpty(route)) {
return null;
}
var index = this.indexOf(route);
return index > -1 ?
this._routeNodes.get(index).key :
null;
}
indexOf(route: any): number {
if (isRouteEmpty(route)) {
return -1;
}
var finder = (node) => {
return (node: RouteNode).value === route;
};
return this._routeNodes.findIndex(finder, this);
}
slice(begin?: number, end?: number): RouteStack {
var routeNodes = this._routeNodes.slice(begin, end);
var index = Math.min(this._index, routeNodes.size - 1);
return this._update(index, routeNodes);
}
/**
* Returns a new stack with the provided route appended,
* starting at this stack size.
*/
push(route: any): RouteStack {
invariant(
!isRouteEmpty(route),
'Must supply route to push'
);
invariant(this._routeNodes.indexOf(route) === -1, 'route must be unique');
// When pushing, removes the rest of the routes past the current index.
var routeNodes = this._routeNodes.withMutations((list: List<RouteNode>) => {
list.slice(0, this._index + 1).push(new RouteNode(route));
});
return this._update(routeNodes.size - 1, routeNodes);
}
/**
* Returns a new stack a size ones less than this stack,
* excluding the last index in this stack.
*/
pop(): RouteStack {
invariant(this._routeNodes.size > 1, 'should not pop routeNodes stack to empty');
// When popping, removes the rest of the routes past the current index.
var routeNodes = this._routeNodes.slice(0, this._index);
return this._update(routeNodes.size - 1, routeNodes);
}
jumpToIndex(index: number): RouteStack {
invariant(
index > -1 && index < this._routeNodes.size,
'index out of bound'
);
return this._update(index, this._routeNodes);
}
/**
* Replace a route in the navigation stack.
*
* `index` specifies the route in the stack that should be replaced.
* If it's negative, it counts from the back.
*/
replaceAtIndex(index: number, route: any): RouteStack {
invariant(
!isRouteEmpty(route),
'Must supply route to replace'
);
if (this.get(index) === route) {
return this;
}
invariant(this.indexOf(route) === -1, 'route must be unique');
if (index < 0) {
index += this._routeNodes.size;
}
invariant(
index > -1 && index < this._routeNodes.size,
'index out of bound'
);
var routeNodes = this._routeNodes.set(index, new RouteNode(route));
return this._update(index, routeNodes);
}
// Iterations
forEach(callback: IterationCallback, context: ?Object): void {
var ii = 0;
var nodes = this._routeNodes;
while (ii < nodes.size) {
var node = nodes.get(ii);
callback.call(context, node.value, ii, node.key);
ii++;
}
}
mapToArray(callback: IterationCallback, context: ?Object): Array<any> {
var result = [];
this.forEach((route, index, key) => {
result.push(callback.call(context, route, index, key));
});
return result;
}
/**
* Returns a Set excluding any routes contained within the stack given.
*/
subtract(stack: RouteStack): Set<StackDiffRecord> {
var items = [];
this._routeNodes.forEach((node: RouteNode, index: number) => {
if (!stack._routeNodes.contains(node)) {
items.push(
new StackDiffRecord({
route: node.value,
index: index,
key: node.key,
})
);
}
});
return new Set(items);
}
_update(index: number, routeNodes: List<RouteNode>): RouteStack {
if (this._index === index && this._routeNodes === routeNodes) {
return this;
}
return new RouteStack(index, routeNodes);
}
}
/**
* The first class data structure for NavigationContext to manage the navigation
* stack of routes.
*/
class NavigationRouteStack extends RouteStack {
constructor(index: number, routeNodes: Array<any>) {
// For now, `RouteStack` internally, uses an immutable `List` to keep
// track of routeNodes. Since using `List` is really just the implementation
// detail, we don't want to accept `routeNodes` as `list` from constructor
// for developer.
var nodes = routeNodes.map((route) => {
invariant(!isRouteEmpty(route), 'route must not be mepty');
return new RouteNode(route);
});
super(index, new List(nodes));
}
}
module.exports = NavigationRouteStack;