|
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+/* @flow */
|
|
|
2
|
+
|
|
|
3
|
+import { Component } from 'react';
|
|
|
4
|
+
|
|
1
|
5
|
/**
|
|
2
|
6
|
* Object describing application route.
|
|
3
|
7
|
*
|
|
|
@@ -5,12 +9,18 @@
|
|
5
|
9
|
* @property {Component} component - React Component constructor.
|
|
6
|
10
|
* @property {string} path - URL route, required for web routing.
|
|
7
|
11
|
*/
|
|
|
12
|
+type Route = {
|
|
|
13
|
+ component: Class<Component<*>>, // eslint-disable-line no-undef
|
|
|
14
|
+ path: string
|
|
|
15
|
+};
|
|
8
|
16
|
|
|
9
|
17
|
/**
|
|
10
|
18
|
* A registry for Navigator routes, allowing features to register themselves
|
|
11
|
19
|
* without needing to create additional inter-feature dependencies.
|
|
12
|
20
|
*/
|
|
13
|
21
|
class RouteRegistry {
|
|
|
22
|
+ _elements: Route[];
|
|
|
23
|
+
|
|
14
|
24
|
/**
|
|
15
|
25
|
* Initializes a new RouteRegistry instance.
|
|
16
|
26
|
*/
|
|
|
@@ -19,8 +29,9 @@ class RouteRegistry {
|
|
19
|
29
|
* The set of registered routes.
|
|
20
|
30
|
*
|
|
21
|
31
|
* @private
|
|
|
32
|
+ * @type {Route[]}
|
|
22
|
33
|
*/
|
|
23
|
|
- this._elements = new Set();
|
|
|
34
|
+ this._elements = [];
|
|
24
|
35
|
}
|
|
25
|
36
|
|
|
26
|
37
|
/**
|
|
|
@@ -32,7 +43,7 @@ class RouteRegistry {
|
|
32
|
43
|
* @returns {boolean} True if the specified a and b describe one and the
|
|
33
|
44
|
* same abstract route; otherwise, false.
|
|
34
|
45
|
*/
|
|
35
|
|
- areRoutesEqual(a, b) {
|
|
|
46
|
+ areRoutesEqual(a: Route, b: Route) {
|
|
36
|
47
|
if (a === b) { // reflexive
|
|
37
|
48
|
return true;
|
|
38
|
49
|
}
|
|
|
@@ -60,21 +71,25 @@ class RouteRegistry {
|
|
60
|
71
|
// We use the destructuring operator to 'clone' the route object to
|
|
61
|
72
|
// prevent modifications from outside (e.g. React Native's Navigator
|
|
62
|
73
|
// extends it with additional properties).
|
|
63
|
|
- return [ ...this._elements ].map(r => {
|
|
|
74
|
+ return this._elements.map(r => {
|
|
64
|
75
|
return { ...r };
|
|
65
|
76
|
});
|
|
66
|
77
|
}
|
|
67
|
78
|
|
|
|
79
|
+/* eslint-disable no-undef */
|
|
|
80
|
+
|
|
68
|
81
|
/**
|
|
69
|
82
|
* Returns registered route by name if any.
|
|
70
|
83
|
*
|
|
71
|
|
- * @param {Object} component - The React Component (class) of the route to
|
|
72
|
|
- * retrieve.
|
|
|
84
|
+ * @param {Component} component - The React Component (class) of the route
|
|
|
85
|
+ * to retrieve.
|
|
73
|
86
|
* @returns {Route|null}
|
|
74
|
87
|
*/
|
|
75
|
|
- getRouteByComponent(component) {
|
|
76
|
|
- const route
|
|
77
|
|
- = [ ...this._elements ].find(r => r.component === component);
|
|
|
88
|
+ getRouteByComponent(component: Class<Component<*>>) {
|
|
|
89
|
+
|
|
|
90
|
+/* eslint-enable no-undef */
|
|
|
91
|
+
|
|
|
92
|
+ const route = this._elements.find(r => r.component === component);
|
|
78
|
93
|
|
|
79
|
94
|
// We use destructuring operator to 'clone' route object to prevent
|
|
80
|
95
|
// modifications from outside (e.g. React Native's Navigator extends
|
|
|
@@ -88,12 +103,13 @@ class RouteRegistry {
|
|
88
|
103
|
* @param {Route} route - Route definition object.
|
|
89
|
104
|
* @returns {void}
|
|
90
|
105
|
*/
|
|
91
|
|
- register(route) {
|
|
92
|
|
- if (this._elements.has(route)) {
|
|
93
|
|
- throw new Error(`Route ${route.component} is registered already!`);
|
|
|
106
|
+ register(route: Route) {
|
|
|
107
|
+ if (this._elements.includes(route)) {
|
|
|
108
|
+ throw new Error(
|
|
|
109
|
+ `Route ${String(route.component)} is registered already!`);
|
|
94
|
110
|
}
|
|
95
|
111
|
|
|
96
|
|
- this._elements.add(route);
|
|
|
112
|
+ this._elements.push(route);
|
|
97
|
113
|
}
|
|
98
|
114
|
}
|
|
99
|
115
|
|