hybrid-navigation 5.4.0 → 5.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (150) hide show
  1. package/HybridNavigation.podspec +1 -0
  2. package/android/proguard-rules.pro +5 -0
  3. package/android/src/main/java/com/navigation/androidx/AppUtils.java +8 -1
  4. package/android/src/main/java/com/navigation/androidx/AwesomeActivity.java +115 -36
  5. package/android/src/main/java/com/navigation/androidx/AwesomeFragment.java +153 -100
  6. package/android/src/main/java/com/navigation/androidx/DefaultTabBarProvider.java +1 -1
  7. package/android/src/main/java/com/navigation/androidx/DrawableUtils.java +5 -1
  8. package/android/src/main/java/com/navigation/androidx/DrawerFragment.java +95 -39
  9. package/android/src/main/java/com/navigation/androidx/FragmentHelper.java +195 -29
  10. package/android/src/main/java/com/navigation/androidx/HybridDrawerLayout.java +225 -27
  11. package/android/src/main/java/com/navigation/androidx/LifecycleDelegate.java +33 -9
  12. package/android/src/main/java/com/navigation/androidx/PresentationDelegate.java +5 -107
  13. package/android/src/main/java/com/navigation/androidx/StackDelegate.java +37 -10
  14. package/android/src/main/java/com/navigation/androidx/StackFragment.java +82 -19
  15. package/android/src/main/java/com/navigation/androidx/Style.java +8 -4
  16. package/android/src/main/java/com/navigation/androidx/SystemUI.java +17 -5
  17. package/android/src/main/java/com/navigation/androidx/TabBar.java +69 -29
  18. package/android/src/main/java/com/navigation/androidx/TabBarFragment.java +48 -11
  19. package/android/src/main/java/com/navigation/androidx/TabBarItem.java +1 -2
  20. package/android/src/main/java/com/navigation/androidx/TabView.java +20 -10
  21. package/android/src/main/java/com/reactnative/hybridnavigation/Constants.java +1 -0
  22. package/android/src/main/java/com/reactnative/hybridnavigation/Garden.java +19 -15
  23. package/android/src/main/java/com/reactnative/hybridnavigation/GlobalStyle.java +10 -48
  24. package/android/src/main/java/com/reactnative/hybridnavigation/HBDRootView.java +22 -23
  25. package/android/src/main/java/com/reactnative/hybridnavigation/HybridFragment.java +54 -22
  26. package/android/src/main/java/com/reactnative/hybridnavigation/NativeEvent.java +4 -2
  27. package/android/src/main/java/com/reactnative/hybridnavigation/NativeNavigation.java +35 -64
  28. package/android/src/main/java/com/reactnative/hybridnavigation/NavigationCommandQueue.java +185 -121
  29. package/android/src/main/java/com/reactnative/hybridnavigation/Navigator.java +21 -5
  30. package/android/src/main/java/com/reactnative/hybridnavigation/NavigatorRegistry.java +27 -30
  31. package/android/src/main/java/com/reactnative/hybridnavigation/OrientationCoordinator.java +41 -4
  32. package/android/src/main/java/com/reactnative/hybridnavigation/Parameters.java +55 -154
  33. package/android/src/main/java/com/reactnative/hybridnavigation/ReactAppCompatActivity.java +30 -1
  34. package/android/src/main/java/com/reactnative/hybridnavigation/ReactAppCompatActivityDelegate.java +12 -37
  35. package/android/src/main/java/com/reactnative/hybridnavigation/ReactFragment.java +31 -190
  36. package/android/src/main/java/com/reactnative/hybridnavigation/ReactManager.java +115 -71
  37. package/android/src/main/java/com/reactnative/hybridnavigation/ReactStackFragment.java +27 -16
  38. package/android/src/main/java/com/reactnative/hybridnavigation/ReactSurfaceCleanup.java +50 -8
  39. package/android/src/main/java/com/reactnative/hybridnavigation/ReactTabBarFragment.java +34 -4
  40. package/android/src/main/java/com/reactnative/hybridnavigation/ReactTabBarProvider.java +39 -10
  41. package/android/src/main/java/com/reactnative/hybridnavigation/RemovalPlan.java +76 -0
  42. package/android/src/main/java/com/reactnative/hybridnavigation/SceneLifecycleStateMachine.java +72 -6
  43. package/android/src/main/java/com/reactnative/hybridnavigation/Utils.java +19 -3
  44. package/android/src/main/java/com/reactnative/hybridnavigation/navigator/DrawerNavigator.java +11 -5
  45. package/android/src/main/java/com/reactnative/hybridnavigation/navigator/ScreenNavigator.java +217 -99
  46. package/android/src/main/java/com/reactnative/hybridnavigation/navigator/StackNavigator.java +122 -59
  47. package/android/src/main/java/com/reactnative/hybridnavigation/navigator/TabNavigator.java +87 -24
  48. package/dist/DeepLink.d.ts +3 -1
  49. package/dist/DeepLink.js +39 -19
  50. package/dist/NativeNavigation.d.ts +8 -5
  51. package/dist/Navigation.d.ts +9 -24
  52. package/dist/Navigation.js +32 -97
  53. package/dist/NavigationComponentWrap.d.ts +1 -2
  54. package/dist/NavigationComponentWrap.js +0 -15
  55. package/dist/Navigator.d.ts +7 -5
  56. package/dist/Navigator.js +38 -33
  57. package/dist/Options.d.ts +1 -7
  58. package/dist/Options.js +1 -2
  59. package/dist/Route.d.ts +24 -12
  60. package/dist/handler/DispatchCommandHandler.d.ts +4 -5
  61. package/dist/handler/DispatchCommandHandler.js +37 -16
  62. package/dist/handler/LayoutCommandHandler.js +18 -3
  63. package/dist/handler/LeaveGuardHandler.d.ts +12 -8
  64. package/dist/handler/LeaveGuardHandler.js +174 -281
  65. package/dist/handler/LifecycleEventHandler.d.ts +7 -4
  66. package/dist/handler/LifecycleEventHandler.js +119 -32
  67. package/dist/handler/ResultEventHandler.d.ts +2 -2
  68. package/dist/handler/ResultEventHandler.js +72 -36
  69. package/dist/handler/SceneExitHandler.d.ts +6 -0
  70. package/dist/handler/SceneExitHandler.js +48 -8
  71. package/dist/hooks.d.ts +1 -0
  72. package/dist/hooks.js +39 -23
  73. package/dist/index.d.ts +9 -8
  74. package/dist/index.js +5 -8
  75. package/dist/routeGraph.d.ts +5 -0
  76. package/dist/routeGraph.js +47 -0
  77. package/dist/router/drawer.d.ts +2 -2
  78. package/dist/router/drawer.js +19 -9
  79. package/dist/router/helpers.d.ts +17 -0
  80. package/dist/router/helpers.js +90 -0
  81. package/dist/router/index.d.ts +2 -2
  82. package/dist/router/index.js +169 -97
  83. package/dist/router/screen.d.ts +2 -2
  84. package/dist/router/screen.js +16 -7
  85. package/dist/router/stack.d.ts +2 -2
  86. package/dist/router/stack.js +24 -49
  87. package/dist/router/tabs.js +5 -1
  88. package/dist/store.d.ts +2 -0
  89. package/dist/store.js +4 -0
  90. package/ios/HBDGlobalStyle.h +0 -1
  91. package/ios/HBDGlobalStyle.mm +27 -39
  92. package/ios/HBDUtils.h +0 -2
  93. package/ios/HBDUtils.mm +81 -41
  94. package/ios/Navigator/HBDDrawerNavigator.mm +31 -26
  95. package/ios/Navigator/HBDNavigatorRegistry.mm +23 -26
  96. package/ios/Navigator/HBDScreenNavigator.mm +240 -137
  97. package/ios/Navigator/HBDStackNavigator.mm +147 -97
  98. package/ios/Navigator/HBDTabNavigator.mm +99 -46
  99. package/ios/React/HBDNativeEvent.mm +38 -5
  100. package/ios/React/HBDNativeNavigation.mm +49 -51
  101. package/ios/React/HBDNavigationCommandQueue.h +55 -0
  102. package/ios/React/HBDNavigationCommandQueue.mm +341 -0
  103. package/ios/React/HBDNavigator.h +20 -0
  104. package/ios/React/HBDOrientationCoordinator.mm +26 -3
  105. package/ios/React/HBDReactBridgeManager.h +18 -6
  106. package/ios/React/HBDReactBridgeManager.mm +536 -315
  107. package/ios/React/HBDSceneLifecycleStateMachine.h +2 -0
  108. package/ios/React/HBDSceneLifecycleStateMachine.mm +18 -0
  109. package/ios/View/HBDRootView.h +1 -0
  110. package/ios/View/HBDRootView.mm +112 -4
  111. package/ios/View/UIImage+WithBadge.mm +9 -7
  112. package/ios/ViewController/HBDDrawerController.h +10 -0
  113. package/ios/ViewController/HBDDrawerController.mm +103 -71
  114. package/ios/ViewController/HBDNavigationController.mm +166 -58
  115. package/ios/ViewController/HBDReactViewController.h +0 -4
  116. package/ios/ViewController/HBDReactViewController.mm +13 -110
  117. package/ios/ViewController/HBDTabBarController.mm +67 -33
  118. package/ios/ViewController/HBDViewController.h +6 -0
  119. package/ios/ViewController/HBDViewController.mm +94 -37
  120. package/ios/ViewController/UINavigationController+HBD.h +2 -2
  121. package/ios/ViewController/UINavigationController+HBD.mm +13 -13
  122. package/ios/ViewController/UIViewController+HBD.h +23 -1
  123. package/ios/ViewController/UIViewController+HBD.mm +298 -40
  124. package/package.json +1 -1
  125. package/src/DeepLink.ts +39 -19
  126. package/src/NativeNavigation.ts +17 -5
  127. package/src/Navigation.ts +49 -170
  128. package/src/NavigationComponentWrap.tsx +1 -16
  129. package/src/Navigator.ts +50 -52
  130. package/src/Options.ts +4 -10
  131. package/src/Route.ts +37 -19
  132. package/src/handler/DispatchCommandHandler.ts +42 -20
  133. package/src/handler/LayoutCommandHandler.ts +15 -3
  134. package/src/handler/LeaveGuardHandler.ts +224 -329
  135. package/src/handler/LifecycleEventHandler.ts +131 -37
  136. package/src/handler/ResultEventHandler.ts +71 -37
  137. package/src/handler/SceneExitHandler.ts +60 -9
  138. package/src/hooks.ts +54 -26
  139. package/src/index.ts +77 -8
  140. package/src/routeGraph.ts +50 -0
  141. package/src/router/drawer.ts +21 -10
  142. package/src/router/helpers.ts +113 -0
  143. package/src/router/index.ts +187 -112
  144. package/src/router/screen.ts +35 -8
  145. package/src/router/stack.ts +40 -53
  146. package/src/router/tabs.ts +5 -1
  147. package/src/store.ts +5 -0
  148. package/dist/sceneCoverage.d.ts +0 -2
  149. package/dist/sceneCoverage.js +0 -25
  150. package/src/sceneCoverage.ts +0 -30
@@ -1,4 +1,4 @@
1
- import { pathToRegexp, match, MatchFunction } from 'path-to-regexp';
1
+ import { match, MatchFunction } from 'path-to-regexp';
2
2
  import {
3
3
  DispatchResult,
4
4
  RouteConfig,
@@ -7,15 +7,18 @@ import {
7
7
  RouteHandler,
8
8
  RouteInfo,
9
9
  RouteInterceptor,
10
+ RouteProcessContext,
10
11
  } from '../Route';
11
12
  import { NavigationItem } from '../Options';
12
13
  import { StackRouteHandler } from './stack';
13
14
  import { ScreenRouteHandler } from './screen';
14
15
  import { TabsRouteHandler } from './tabs';
15
16
  import { DrawerRouteHandler } from './drawer';
17
+ import { checkRootSnapshot, pushRouteChain } from './helpers';
16
18
 
17
19
  import Navigation from '../Navigation';
18
20
  import type { NavigationSubscription } from '../Navigation';
21
+ import { containsScene, findNodeBySceneId, hasLayoutInGraph } from '../routeGraph';
19
22
  import { getRouteConfigs, getRouteConfigVersion } from '../routeConfigRegistry';
20
23
 
21
24
  interface IndexType {
@@ -26,6 +29,8 @@ interface CompiledRouteData extends RouteData {
26
29
  matcher?: MatchFunction<IndexType>;
27
30
  }
28
31
 
32
+ const FORBIDDEN_QUERY_KEYS = new Set(['__proto__', 'constructor', 'prototype']);
33
+
29
34
  function safeDecodeURIComponent(value: string) {
30
35
  try {
31
36
  return decodeURIComponent(value);
@@ -34,27 +39,25 @@ function safeDecodeURIComponent(value: string) {
34
39
  }
35
40
  }
36
41
 
42
+ function safeDecodeQueryComponent(value: string) {
43
+ return safeDecodeURIComponent(value.replace(/\+/g, ' '));
44
+ }
45
+
37
46
  function queryParams(queryString: string) {
38
47
  return (queryString || '').split('&').reduce((result: IndexType, item: string) => {
39
48
  if (item !== '') {
40
- const nextResult = result || {};
41
49
  const separatorIndex = item.indexOf('=');
42
50
  const rawKey = separatorIndex >= 0 ? item.slice(0, separatorIndex) : item;
43
51
  const rawValue = separatorIndex >= 0 ? item.slice(separatorIndex + 1) : '';
44
- const key = safeDecodeURIComponent(rawKey);
45
- if (key) {
46
- nextResult[key] = safeDecodeURIComponent(rawValue);
52
+ const key = safeDecodeQueryComponent(rawKey);
53
+ if (key && !FORBIDDEN_QUERY_KEYS.has(key)) {
54
+ result[key] = safeDecodeQueryComponent(rawValue);
47
55
  }
48
- return nextResult;
49
56
  }
50
57
  return result;
51
58
  }, {});
52
59
  }
53
60
 
54
- function containsScene(node: RouteGraph, sceneId: string): boolean {
55
- return node.sceneId === sceneId || !!node.children?.some(child => containsScene(child, sceneId));
56
- }
57
-
58
61
  class Router {
59
62
  constructor() {
60
63
  this.registerRouteHandler('screen', new ScreenRouteHandler());
@@ -64,28 +67,35 @@ class Router {
64
67
  }
65
68
 
66
69
  private handlers = new Map<string, RouteHandler>();
67
- private openQueue: Promise<unknown> = Promise.resolve();
70
+ private requestSeq = 0;
71
+ private openGeneration = 0;
68
72
 
69
73
  registerRouteHandler(layout: string, handler: RouteHandler) {
70
74
  this.handlers.set(layout, handler);
71
75
  }
72
76
 
73
77
  private convertConfigToData(moduleName: string, route: RouteConfig): CompiledRouteData {
74
- let { path, dependency, mode } = route;
75
- let regexp: RegExp | undefined;
78
+ const { dependency } = route;
79
+ const normalizedPath = route.path
80
+ ? route.path.startsWith('/')
81
+ ? route.path
82
+ : '/' + route.path
83
+ : undefined;
76
84
  let matcher: MatchFunction<IndexType> | undefined;
77
- if (path) {
78
- if (!path.startsWith('/')) {
79
- path = '/' + path;
80
- route.path = path;
85
+ if (normalizedPath) {
86
+ try {
87
+ matcher = match<IndexType>(normalizedPath, {
88
+ decode: safeDecodeURIComponent,
89
+ });
90
+ } catch (error) {
91
+ console.error(
92
+ `[hybrid-navigation] Invalid route path "${route.path}" for module "${moduleName}".`,
93
+ error,
94
+ );
81
95
  }
82
- regexp = pathToRegexp(path).regexp;
83
- matcher = match<IndexType>(path, {
84
- decode: safeDecodeURIComponent,
85
- });
86
96
  }
87
- mode = mode || 'push';
88
- return { moduleName, mode, path, regexp, matcher, dependency };
97
+ const mode = route.mode || 'push';
98
+ return { moduleName, mode, path: normalizedPath, matcher, dependency };
89
99
  }
90
100
 
91
101
  private inflatedRouteConfigVersion = -1;
@@ -96,29 +106,32 @@ class Router {
96
106
  if (this.inflatedRouteConfigVersion === currentVersion) {
97
107
  return;
98
108
  }
99
- this.inflatedRouteConfigVersion = currentVersion;
100
- this.routeDataSet.clear();
101
- const routeConfigs = getRouteConfigs();
102
- for (const [moduleName, config] of routeConfigs) {
103
- const data = this.convertConfigToData(moduleName, config);
104
- this.routeDataSet.set(moduleName, data);
109
+ const nextRouteDataSet = new Map<string, CompiledRouteData>();
110
+ for (const [moduleName, config] of getRouteConfigs()) {
111
+ nextRouteDataSet.set(moduleName, this.convertConfigToData(moduleName, config));
105
112
  }
113
+ this.routeDataSet = nextRouteDataSet;
114
+ this.inflatedRouteConfigVersion = currentVersion;
106
115
  }
107
116
 
108
- private interceptors: RouteInterceptor[] = [];
117
+ private interceptors: Array<{ fn: RouteInterceptor }> = [];
109
118
 
110
119
  addInterceptor(interceptor: RouteInterceptor): NavigationSubscription {
111
- this.interceptors.push(interceptor);
120
+ const entry = { fn: interceptor };
121
+ this.interceptors.push(entry);
112
122
  return {
113
123
  remove: () => {
114
- this.interceptors = this.interceptors.filter(item => item !== interceptor);
124
+ const index = this.interceptors.indexOf(entry);
125
+ if (index >= 0) {
126
+ this.interceptors.splice(index, 1);
127
+ }
115
128
  },
116
129
  };
117
130
  }
118
131
 
119
132
  private async intercept(path: string) {
120
- for (const interceptor of this.interceptors.slice()) {
121
- const result = interceptor(path);
133
+ for (const { fn } of this.interceptors.slice()) {
134
+ const result = fn(path);
122
135
  const intercepted = result instanceof Promise ? await result : result;
123
136
  if (intercepted) {
124
137
  return true;
@@ -145,13 +158,15 @@ class Router {
145
158
 
146
159
  const matched = data.matcher(pathName);
147
160
  if (matched) {
148
- const moduleName = data.moduleName;
149
161
  const params = matched.params || {};
150
162
  const query = queryParams(queryString);
151
- const mergedProps = { ...query, ...params, ...props };
152
- const dependencies = this.routeDependencies(data);
153
- const mode = data.mode;
154
- return { moduleName, props: mergedProps, dependencies, mode, options };
163
+ return {
164
+ moduleName: data.moduleName,
165
+ props: { ...query, ...params, ...props },
166
+ dependencies: this.routeDependencies(data),
167
+ mode: data.mode,
168
+ options,
169
+ };
155
170
  }
156
171
  }
157
172
  }
@@ -160,7 +175,7 @@ class Router {
160
175
  const dependencies: string[] = [];
161
176
  const visited = new Set<string>([routeData.moduleName]);
162
177
  let data: RouteData | undefined = routeData;
163
- while (data && data.dependency) {
178
+ while (data?.dependency) {
164
179
  if (visited.has(data.dependency)) {
165
180
  break;
166
181
  }
@@ -174,57 +189,57 @@ class Router {
174
189
  private async processRoute(
175
190
  graph: RouteGraph,
176
191
  target: RouteInfo,
192
+ context: RouteProcessContext,
177
193
  ): Promise<[boolean, DispatchResult]> {
178
194
  const handler = this.handlers.get(graph.layout);
179
195
  if (!handler) {
180
196
  return [false, { ok: false, code: 'invalid_action' }];
181
197
  }
182
198
 
183
- const [consumed, childGraph, result] = await handler.process(graph, target);
199
+ const [consumed, childGraph, result] = await handler.process(graph, target, context);
184
200
  if (consumed && childGraph) {
185
- return await this.processRoute(childGraph, target);
201
+ const check = await checkRootSnapshot(context.rootGeneration, context.isSuperseded);
202
+ if (!check.ok) {
203
+ return [true, check.result];
204
+ }
205
+ const rootLayer = check.snapshot.layers[0];
206
+ const freshChildGraph = rootLayer ? findNodeBySceneId(rootLayer, childGraph.sceneId) : null;
207
+ if (!freshChildGraph) {
208
+ return [
209
+ true,
210
+ {
211
+ ok: false,
212
+ code: 'scene_not_found',
213
+ message: 'Target container disappeared during navigation.',
214
+ },
215
+ ];
216
+ }
217
+ return await this.processRoute(freshChildGraph, target, context);
186
218
  }
187
219
  return [consumed, result ?? { ok: true, code: 'ok' }];
188
220
  }
189
221
 
190
- open(
222
+ async open(
191
223
  path: string,
192
224
  props: IndexType = {},
193
225
  options: NavigationItem = {},
194
- ): Promise<DispatchResult> {
195
- const task = this.openQueue.then(
196
- () => this.performOpen(path, props, options),
197
- () => this.performOpen(path, props, options),
198
- );
199
- this.openQueue = task.then(
200
- () => undefined,
201
- () => undefined,
202
- );
203
- return task;
204
- }
205
-
206
- private hasDrawerLayout(graph: RouteGraph): boolean {
207
- if (graph.layout === 'drawer') {
208
- return true;
209
- }
210
- if (!graph.children) {
211
- return false;
212
- }
213
- return graph.children.some(child => this.hasDrawerLayout(child));
214
- }
215
-
216
- private async performOpen(
217
- path: string,
218
- props: IndexType,
219
- options: NavigationItem,
220
226
  ): Promise<DispatchResult> {
221
227
  this.inflateRouteData();
222
-
223
- const intercepted = await this.intercept(path);
224
- if (intercepted) {
225
- return { ok: false, code: 'intercepted' };
228
+ const requestId = ++this.requestSeq;
229
+ if (this.interceptors.length > 0) {
230
+ const intercepted = await this.intercept(path);
231
+ if (intercepted) {
232
+ return { ok: false, code: 'intercepted' };
233
+ }
234
+ if (requestId < this.openGeneration) {
235
+ return {
236
+ ok: false,
237
+ code: 'superseded',
238
+ message: 'Superseded by a newer router.open call.',
239
+ };
240
+ }
241
+ this.inflateRouteData();
226
242
  }
227
-
228
243
  const route = this.pathToRoute(path, props, options);
229
244
  if (!route) {
230
245
  return {
@@ -233,8 +248,21 @@ class Router {
233
248
  message: `No route matched path: ${path}`,
234
249
  };
235
250
  }
251
+ this.openGeneration = requestId;
252
+ return this.performOpen(route, requestId);
253
+ }
236
254
 
255
+ private async performOpen(route: RouteInfo, openToken: number): Promise<DispatchResult> {
256
+ const isSuperseded = () => openToken !== this.openGeneration;
237
257
  let snapshot = await Navigation.navigationSnapshot();
258
+ if (isSuperseded()) {
259
+ return {
260
+ ok: false,
261
+ code: 'superseded',
262
+ message: 'Superseded by a newer router.open call.',
263
+ };
264
+ }
265
+
238
266
  const generation = snapshot.rootGeneration;
239
267
  const dismissed = new Set<string>();
240
268
  const maxSteps = Math.max(snapshot.layers.length, 12);
@@ -243,26 +271,66 @@ class Router {
243
271
  const layer = snapshot.layers[snapshot.layers.length - 1];
244
272
  const current = snapshot.currentRoute;
245
273
  if (!current || !containsScene(layer, current.sceneId)) {
246
- return { ok: false, code: 'scene_not_found', message: 'Current scene is not in the top layer.' };
274
+ return {
275
+ ok: false,
276
+ code: 'scene_not_found',
277
+ message: 'Current scene is not in the top layer.',
278
+ };
247
279
  }
248
- if (dismissed.has(layer.sceneId) || dismissed.size >= maxSteps) {
249
- return { ok: false, code: 'invalid_action', message: 'Unable to finish dismissing presented layers.' };
280
+ const dismissKey = `${layer.sceneId}:${current.sceneId}`;
281
+ if (dismissed.has(dismissKey) || dismissed.size >= maxSteps) {
282
+ return {
283
+ ok: false,
284
+ code: 'invalid_action',
285
+ message: 'Unable to finish dismissing presented layers.',
286
+ };
250
287
  }
251
288
  if (layer.mode !== 'modal' && layer.mode !== 'present') {
252
- return { ok: false, code: 'invalid_action', message: `Unexpected layer mode: ${layer.mode}` };
289
+ return {
290
+ ok: false,
291
+ code: 'invalid_action',
292
+ message: `Unexpected layer mode: ${layer.mode}`,
293
+ };
253
294
  }
254
295
 
255
- dismissed.add(layer.sceneId);
256
- const action = layer.mode === 'modal' ? 'hideModal' : 'dismiss';
257
- const result = await Navigation.dispatch(current.sceneId, action, {
258
- from: current.moduleName,
259
- target: 'self',
260
- });
296
+ dismissed.add(dismissKey);
297
+ const result =
298
+ layer.mode === 'modal'
299
+ ? await Navigation.dispatch(current.sceneId, 'close', {
300
+ from: current.moduleName,
301
+ })
302
+ : await Navigation.dispatch(current.sceneId, 'dismiss', {
303
+ from: current.moduleName,
304
+ target: 'self',
305
+ });
261
306
  snapshot = await Navigation.navigationSnapshot();
307
+ if (isSuperseded()) {
308
+ return {
309
+ ok: false,
310
+ code: 'superseded',
311
+ message: 'Superseded by a newer router.open call.',
312
+ };
313
+ }
262
314
  if (snapshot.rootGeneration !== generation) {
263
- return { ok: false, code: 'superseded', message: 'Root layout changed during navigation.' };
315
+ return {
316
+ ok: false,
317
+ code: 'superseded',
318
+ message: 'Root layout changed during navigation.',
319
+ };
320
+ }
321
+ if (!result.ok) {
322
+ if (result.code === 'scene_not_found') {
323
+ const topLayer = snapshot.layers[snapshot.layers.length - 1];
324
+ if (
325
+ !topLayer ||
326
+ topLayer.sceneId !== layer.sceneId ||
327
+ snapshot.currentRoute?.sceneId !== current.sceneId
328
+ ) {
329
+ continue;
330
+ }
331
+ }
332
+ return result;
264
333
  }
265
- if (!result.ok) return result;
266
334
  }
267
335
 
268
336
  const routeGraph = snapshot.layers[0];
@@ -274,19 +342,21 @@ class Router {
274
342
  };
275
343
  }
276
344
 
277
- const [consumed, routeResult] = await this.processRoute(routeGraph, route);
345
+ const context: RouteProcessContext = {
346
+ rootGeneration: generation,
347
+ isSuperseded,
348
+ };
349
+
350
+ const [consumed, routeResult] = await this.processRoute(routeGraph, route, context);
278
351
  if (consumed) {
279
352
  return routeResult;
280
353
  }
281
354
 
282
- snapshot = await Navigation.navigationSnapshot();
283
- if (snapshot.rootGeneration !== generation) {
284
- return { ok: false, code: 'superseded', message: 'Root layout changed during navigation.' };
285
- }
286
- if (snapshot.layers.length > 1) {
287
- return { ok: false, code: 'covered', message: 'A new layer was presented during navigation.' };
355
+ let check = await checkRootSnapshot(generation, isSuperseded);
356
+ if (!check.ok) {
357
+ return check.result;
288
358
  }
289
- const currentRoute = snapshot.currentRoute;
359
+ let currentRoute = check.snapshot.currentRoute;
290
360
  if (!currentRoute) {
291
361
  return {
292
362
  ok: false,
@@ -295,29 +365,34 @@ class Router {
295
365
  };
296
366
  }
297
367
 
298
- const { sceneId, moduleName: from } = currentRoute;
299
- if (this.hasDrawerLayout(routeGraph)) {
300
- const closeMenuResult = await Navigation.dispatch(sceneId, 'closeMenu', { from });
368
+ if (hasLayoutInGraph(routeGraph, 'drawer')) {
369
+ const closeMenuResult = await Navigation.dispatch(currentRoute.sceneId, 'closeMenu', {
370
+ from: currentRoute.moduleName,
371
+ });
301
372
  if (closeMenuResult.code === 'intercepted') {
302
373
  return closeMenuResult;
303
374
  }
375
+ check = await checkRootSnapshot(generation, isSuperseded);
376
+ if (!check.ok) {
377
+ return check.result;
378
+ }
379
+ currentRoute = check.snapshot.currentRoute;
380
+ if (!currentRoute) {
381
+ return {
382
+ ok: false,
383
+ code: 'scene_not_found',
384
+ message: 'Unable to resolve current route.',
385
+ };
386
+ }
304
387
  }
305
388
 
306
- const { moduleName, mode: routeMode, props: initialProps } = route;
307
- let action = 'push';
308
- if (routeMode === 'present') {
309
- action = 'present';
310
- } else if (routeMode === 'modal') {
311
- action = 'showModal';
312
- }
313
-
314
- return await Navigation.dispatch(sceneId, action, {
315
- moduleName,
316
- props: initialProps,
317
- options: route.options,
318
- from,
319
- to: moduleName,
320
- });
389
+ return await pushRouteChain(
390
+ currentRoute.sceneId,
391
+ currentRoute.moduleName,
392
+ route.dependencies,
393
+ route,
394
+ context,
395
+ );
321
396
  }
322
397
  }
323
398
 
@@ -1,4 +1,12 @@
1
- import { LayoutMode, RouteGraph, RouteHandler, RouteHandleResult, RouteInfo } from '../Route';
1
+ import {
2
+ LayoutMode,
3
+ RouteGraph,
4
+ RouteHandler,
5
+ RouteHandleResult,
6
+ RouteInfo,
7
+ RouteProcessContext,
8
+ } from '../Route';
9
+ import { pushRouteChain, resolveProcessContext } from './helpers';
2
10
 
3
11
  export interface ScreenGraph extends RouteGraph {
4
12
  layout: 'screen';
@@ -14,10 +22,9 @@ export function isScreenGraph(graph: RouteGraph): graph is ScreenGraph {
14
22
  function extractModuleNames(graph: RouteGraph, set: string[]) {
15
23
  if (isScreenGraph(graph)) {
16
24
  set.push(graph.moduleName);
17
- } else {
18
- const children = graph.children!;
19
- for (let i = 0; i < children.length; i++) {
20
- extractModuleNames(children[i], set);
25
+ } else if (Array.isArray(graph.children)) {
26
+ for (let i = 0; i < graph.children.length; i++) {
27
+ extractModuleNames(graph.children[i], set);
21
28
  }
22
29
  }
23
30
  }
@@ -31,15 +38,35 @@ export function isTargetLocateIn(graph: RouteGraph, target: RouteInfo) {
31
38
  }
32
39
 
33
40
  export class ScreenRouteHandler implements RouteHandler {
34
- process(graph: RouteGraph, target: RouteInfo): Promise<RouteHandleResult> {
41
+ async process(
42
+ graph: RouteGraph,
43
+ target: RouteInfo,
44
+ context?: RouteProcessContext,
45
+ ): Promise<RouteHandleResult> {
35
46
  if (!isScreenGraph(graph)) {
36
47
  throw new Error(`${graph} is Not a ScreenGraph`);
37
48
  }
38
49
 
39
50
  if (graph.moduleName === target.moduleName) {
40
- return Promise.resolve([true, null, { ok: true, code: 'ok' }]);
51
+ return [true, null, { ok: true, code: 'ok' }];
41
52
  }
42
53
 
43
- return Promise.resolve([false, graph]);
54
+ const depIndex = target.dependencies.lastIndexOf(graph.moduleName);
55
+ if (depIndex >= 0) {
56
+ const resolved = await resolveProcessContext(context);
57
+ if (!resolved.ok) {
58
+ return [true, null, resolved.result];
59
+ }
60
+ const result = await pushRouteChain(
61
+ graph.sceneId,
62
+ graph.moduleName,
63
+ target.dependencies.slice(depIndex + 1),
64
+ target,
65
+ resolved.context,
66
+ );
67
+ return [true, null, result];
68
+ }
69
+
70
+ return [false, graph];
44
71
  }
45
72
  }
@@ -1,4 +1,5 @@
1
1
  import { ScreenGraph, isScreenGraph, isTargetLocateIn } from './screen';
2
+ import { checkRootSnapshot, pushRouteChain, resolveProcessContext } from './helpers';
2
3
  import {
3
4
  DispatchResult,
4
5
  LayoutMode,
@@ -6,6 +7,7 @@ import {
6
7
  RouteHandler,
7
8
  RouteHandleResult,
8
9
  RouteInfo,
10
+ RouteProcessContext,
9
11
  } from '../Route';
10
12
  import Navigation from '../Navigation';
11
13
 
@@ -21,7 +23,11 @@ export function isStackGraph(graph: RouteGraph): graph is StackGraph {
21
23
  }
22
24
 
23
25
  export class StackRouteHandler implements RouteHandler {
24
- async process(graph: RouteGraph, target: RouteInfo): Promise<RouteHandleResult> {
26
+ async process(
27
+ graph: RouteGraph,
28
+ target: RouteInfo,
29
+ context?: RouteProcessContext,
30
+ ): Promise<RouteHandleResult> {
25
31
  if (!isStackGraph(graph)) {
26
32
  throw new Error(`${graph} is Not a StackGraph`);
27
33
  }
@@ -31,16 +37,13 @@ export class StackRouteHandler implements RouteHandler {
31
37
  for (let i = children.length - 1; i > -1; i--) {
32
38
  const childGraph = children[i];
33
39
  if (isTargetLocateIn(childGraph, target)) {
40
+ const isTop = i === children.length - 1;
34
41
  if (isScreenGraph(childGraph)) {
35
- const result = await this.navigateTo(
36
- childGraph,
37
- target,
38
- i === children.length - 1,
39
- );
42
+ const result = await this.navigateTo(childGraph, target, isTop, context);
40
43
  return [true, null, result];
41
44
  }
42
45
 
43
- const popResult = await this.popToIfNeeded(childGraph, i === children.length - 1);
46
+ const popResult = await this.popToIfNeeded(childGraph, isTop);
44
47
  if (!popResult.ok) {
45
48
  return [true, null, popResult];
46
49
  }
@@ -67,6 +70,7 @@ export class StackRouteHandler implements RouteHandler {
67
70
  childGraph: ScreenGraph,
68
71
  target: RouteInfo,
69
72
  isLastChildAtStack: boolean,
73
+ context?: RouteProcessContext,
70
74
  ): Promise<DispatchResult> {
71
75
  const { moduleName, dependencies, props, options } = target;
72
76
  const expectations = [...dependencies, moduleName];
@@ -74,9 +78,7 @@ export class StackRouteHandler implements RouteHandler {
74
78
  const pendings = expectations.slice(index + 1);
75
79
  const sceneId = childGraph.sceneId;
76
80
 
77
- const moduleExisting = pendings.length === 0;
78
-
79
- if (moduleExisting) {
81
+ if (pendings.length === 0) {
80
82
  const hasProps = props && Object.keys(props).length > 0;
81
83
  const hasOptions = options && Object.keys(options).length > 0;
82
84
  if (!hasProps && !hasOptions) {
@@ -88,57 +90,42 @@ export class StackRouteHandler implements RouteHandler {
88
90
  });
89
91
  }
90
92
  return { ok: true, code: 'ok' };
91
- } else {
92
- return await Navigation.dispatch(sceneId, 'redirectTo', {
93
- moduleName,
94
- from: childGraph.moduleName,
95
- to: moduleName,
96
- props,
97
- options,
98
- });
99
93
  }
94
+ return await Navigation.dispatch(sceneId, 'redirectTo', {
95
+ moduleName,
96
+ from: childGraph.moduleName,
97
+ to: moduleName,
98
+ props,
99
+ options,
100
+ });
100
101
  }
101
102
 
103
+ const resolved = await resolveProcessContext(context);
104
+ if (!resolved.ok) {
105
+ return resolved.result;
106
+ }
107
+ const resolvedContext = resolved.context;
108
+
102
109
  const popResult = await this.popToIfNeeded(childGraph, isLastChildAtStack);
103
110
  if (!popResult.ok) {
104
111
  return popResult;
105
112
  }
106
- const finalAction =
107
- target.mode === 'present'
108
- ? 'present'
109
- : target.mode === 'modal'
110
- ? 'showModal'
111
- : 'push';
112
- let currentSceneId = sceneId;
113
- let fromModule = childGraph.moduleName;
114
- let lastResult: DispatchResult = { ok: true, code: 'ok' };
115
- for (let i = 0; i < pendings.length; i++) {
116
- const isLast = i === pendings.length - 1;
117
- const targetModule = isLast ? moduleName : pendings[i];
118
- lastResult = isLast
119
- ? await Navigation.dispatch(currentSceneId, finalAction, {
120
- moduleName,
121
- from: fromModule,
122
- to: moduleName,
123
- props,
124
- options,
125
- })
126
- : await Navigation.dispatch(currentSceneId, 'push', {
127
- moduleName: targetModule,
128
- from: fromModule,
129
- to: targetModule,
130
- });
131
- if (!lastResult.ok) {
132
- return lastResult;
133
- }
134
- fromModule = targetModule;
135
- if (!isLast) {
136
- const current = await Navigation.currentRoute();
137
- if (current?.sceneId) {
138
- currentSceneId = current.sceneId;
139
- }
113
+ if (!isLastChildAtStack) {
114
+ const check = await checkRootSnapshot(
115
+ resolvedContext.rootGeneration,
116
+ resolvedContext.isSuperseded,
117
+ );
118
+ if (!check.ok) {
119
+ return check.result;
140
120
  }
141
121
  }
142
- return lastResult;
122
+
123
+ return await pushRouteChain(
124
+ sceneId,
125
+ childGraph.moduleName,
126
+ pendings.slice(0, -1),
127
+ target,
128
+ resolvedContext,
129
+ );
143
130
  }
144
131
  }
@@ -22,12 +22,16 @@ export class TabsRouteHandler implements RouteHandler {
22
22
  }
23
23
 
24
24
  const { children, selectedIndex } = graph;
25
+ if (!Array.isArray(children) || children.length === 0) {
26
+ return [false, graph];
27
+ }
25
28
 
26
29
  for (let i = 0; i < children.length; i++) {
27
30
  if (isTargetLocateIn(children[i], target)) {
28
31
  if (selectedIndex !== i) {
32
+ const dispatchSceneId = children[selectedIndex]?.sceneId ?? graph.sceneId;
29
33
  const result = await Navigation.dispatch(
30
- children[selectedIndex].sceneId,
34
+ dispatchSceneId,
31
35
  'switchTab',
32
36
  {
33
37
  from: selectedIndex,