forked from Expensify/App
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path@react-navigation+core+6.4.11+001+getStateFromPath-getPathFromState-configs-caching.patch
259 lines (249 loc) · 7.9 KB
/
@react-navigation+core+6.4.11+001+getStateFromPath-getPathFromState-configs-caching.patch
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
diff --git a/node_modules/@react-navigation/core/src/getPathFromState.tsx b/node_modules/@react-navigation/core/src/getPathFromState.tsx
index 26a6213..bdbb056 100644
--- a/node_modules/@react-navigation/core/src/getPathFromState.tsx
+++ b/node_modules/@react-navigation/core/src/getPathFromState.tsx
@@ -37,6 +37,11 @@ const getActiveRoute = (state: State): { name: string; params?: object } => {
return route;
};
+let cachedNormalizedConfigs: [
+ PathConfigMap<{}> | undefined,
+ Record<string, ConfigItem>,
+] = [undefined, {}];
+
/**
* Utility to serialize a navigation state object to a path string.
*
@@ -81,9 +86,13 @@ export default function getPathFromState<ParamList extends {}>(
}
// Create a normalized configs object which will be easier to use
- const configs: Record<string, ConfigItem> = options?.screens
- ? createNormalizedConfigs(options?.screens)
- : {};
+ if (cachedNormalizedConfigs[0] !== options?.screens) {
+ cachedNormalizedConfigs = [
+ options?.screens,
+ options?.screens ? createNormalizedConfigs(options.screens) : {},
+ ];
+ }
+ const configs: Record<string, ConfigItem> = cachedNormalizedConfigs[1];
let path = '/';
let current: State | undefined = state;
diff --git a/node_modules/@react-navigation/core/src/getStateFromPath.tsx b/node_modules/@react-navigation/core/src/getStateFromPath.tsx
index b61e1e5..d244bef 100644
--- a/node_modules/@react-navigation/core/src/getStateFromPath.tsx
+++ b/node_modules/@react-navigation/core/src/getStateFromPath.tsx
@@ -41,6 +41,12 @@ type ParsedRoute = {
params?: Record<string, any> | undefined;
};
+type ConfigResources = {
+ initialRoutes: InitialRouteConfig[];
+ configs: RouteConfig[];
+ configWithRegexes: RouteConfig[];
+};
+
/**
* Utility to parse a path string to initial state object accepted by the container.
* This is useful for deep linking when we need to handle the incoming URL.
@@ -66,18 +72,8 @@ export default function getStateFromPath<ParamList extends {}>(
path: string,
options?: Options<ParamList>
): ResultState | undefined {
- if (options) {
- validatePathConfig(options);
- }
-
- let initialRoutes: InitialRouteConfig[] = [];
-
- if (options?.initialRouteName) {
- initialRoutes.push({
- initialRouteName: options.initialRouteName,
- parentScreens: [],
- });
- }
+ const { initialRoutes, configs, configWithRegexes } =
+ getConfigResources(options);
const screens = options?.screens;
@@ -106,8 +102,111 @@ export default function getStateFromPath<ParamList extends {}>(
return undefined;
}
+ if (remaining === '/') {
+ // We need to add special handling of empty path so navigation to empty path also works
+ // When handling empty path, we should only look at the root level config
+ const match = configs.find(
+ (config) =>
+ config.path === '' &&
+ config.routeNames.every(
+ // Make sure that none of the parent configs have a non-empty path defined
+ (name) => !configs.find((c) => c.screen === name)?.path
+ )
+ );
+
+ if (match) {
+ return createNestedStateObject(
+ path,
+ match.routeNames.map((name) => ({ name })),
+ initialRoutes,
+ configs
+ );
+ }
+
+ return undefined;
+ }
+
+ let result: PartialState<NavigationState> | undefined;
+ let current: PartialState<NavigationState> | undefined;
+
+ // We match the whole path against the regex instead of segments
+ // This makes sure matches such as wildcard will catch any unmatched routes, even if nested
+ const { routes, remainingPath } = matchAgainstConfigs(
+ remaining,
+ configWithRegexes
+ );
+
+ if (routes !== undefined) {
+ // This will always be empty if full path matched
+ current = createNestedStateObject(path, routes, initialRoutes, configs);
+ remaining = remainingPath;
+ result = current;
+ }
+
+ if (current == null || result == null) {
+ return undefined;
+ }
+
+ return result;
+}
+
+/**
+ * Reference to the last used config resources. This is used to avoid recomputing the config resources when the options are the same.
+ */
+let cachedConfigResources: [Options<{}> | undefined, ConfigResources] = [
+ undefined,
+ prepareConfigResources(),
+];
+
+function getConfigResources<ParamList extends {}>(
+ options?: Options<ParamList>
+) {
+ if (cachedConfigResources[0] !== options) {
+ cachedConfigResources = [options, prepareConfigResources(options)];
+ }
+
+ return cachedConfigResources[1];
+}
+
+function prepareConfigResources(options?: Options<{}>) {
+ if (options) {
+ validatePathConfig(options);
+ }
+
+ const initialRoutes = getInitialRoutes(options);
+
+ const configs = getNormalizedConfigs(initialRoutes, options?.screens);
+
+ checkForDuplicatedConfigs(configs);
+
+ const configWithRegexes = getConfigsWithRegexes(configs);
+
+ return {
+ initialRoutes,
+ configs,
+ configWithRegexes,
+ };
+}
+
+function getInitialRoutes(options?: Options<{}>) {
+ const initialRoutes: InitialRouteConfig[] = [];
+
+ if (options?.initialRouteName) {
+ initialRoutes.push({
+ initialRouteName: options.initialRouteName,
+ parentScreens: [],
+ });
+ }
+
+ return initialRoutes;
+}
+
+function getNormalizedConfigs<ParamList extends {}>(
+ initialRoutes: InitialRouteConfig[],
+ screens: PathConfigMap<ParamList> = {}
+) {
// Create a normalized configs array which will be easier to use
- const configs = ([] as RouteConfig[])
+ return ([] as RouteConfig[])
.concat(
...Object.keys(screens).map((key) =>
createNormalizedConfigs(
@@ -169,7 +268,9 @@ export default function getStateFromPath<ParamList extends {}>(
}
return bParts.length - aParts.length;
});
+}
+function checkForDuplicatedConfigs(configs: RouteConfig[]) {
// Check for duplicate patterns in the config
configs.reduce<Record<string, RouteConfig>>((acc, config) => {
if (acc[config.pattern]) {
@@ -198,57 +299,14 @@ export default function getStateFromPath<ParamList extends {}>(
[config.pattern]: config,
});
}, {});
+}
- if (remaining === '/') {
- // We need to add special handling of empty path so navigation to empty path also works
- // When handling empty path, we should only look at the root level config
- const match = configs.find(
- (config) =>
- config.path === '' &&
- config.routeNames.every(
- // Make sure that none of the parent configs have a non-empty path defined
- (name) => !configs.find((c) => c.screen === name)?.path
- )
- );
-
- if (match) {
- return createNestedStateObject(
- path,
- match.routeNames.map((name) => ({ name })),
- initialRoutes,
- configs
- );
- }
-
- return undefined;
- }
-
- let result: PartialState<NavigationState> | undefined;
- let current: PartialState<NavigationState> | undefined;
-
- // We match the whole path against the regex instead of segments
- // This makes sure matches such as wildcard will catch any unmatched routes, even if nested
- const { routes, remainingPath } = matchAgainstConfigs(
- remaining,
- configs.map((c) => ({
- ...c,
- // Add `$` to the regex to make sure it matches till end of the path and not just beginning
- regex: c.regex ? new RegExp(c.regex.source + '$') : undefined,
- }))
- );
-
- if (routes !== undefined) {
- // This will always be empty if full path matched
- current = createNestedStateObject(path, routes, initialRoutes, configs);
- remaining = remainingPath;
- result = current;
- }
-
- if (current == null || result == null) {
- return undefined;
- }
-
- return result;
+function getConfigsWithRegexes(configs: RouteConfig[]) {
+ return configs.map((c) => ({
+ ...c,
+ // Add `$` to the regex to make sure it matches till end of the path and not just beginning
+ regex: c.regex ? new RegExp(c.regex.source + '$') : undefined,
+ }));
}
const joinPaths = (...paths: string[]): string =>