hybrid-navigation 5.1.1 → 5.3.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 (101) hide show
  1. package/README.md +9 -20
  2. package/android/src/main/java/com/navigation/androidx/AwesomeFragment.java +5 -13
  3. package/android/src/main/java/com/navigation/androidx/DefaultTabBarProvider.java +2 -2
  4. package/android/src/main/java/com/navigation/androidx/TabBarFragment.java +0 -50
  5. package/android/src/main/java/com/navigation/androidx/TabBarItem.java +0 -4
  6. package/android/src/main/java/com/reactnative/hybridnavigation/GlobalStyle.java +0 -7
  7. package/android/src/main/java/com/reactnative/hybridnavigation/HybridFragment.java +7 -43
  8. package/android/src/main/java/com/reactnative/hybridnavigation/NativeNavigation.java +5 -39
  9. package/android/src/main/java/com/reactnative/hybridnavigation/ReactFragment.java +62 -135
  10. package/android/src/main/java/com/reactnative/hybridnavigation/ReactManager.java +0 -20
  11. package/android/src/main/java/com/reactnative/hybridnavigation/ReactTabBarFragment.java +10 -21
  12. package/android/src/main/java/com/reactnative/hybridnavigation/navigator/ScreenNavigator.java +39 -87
  13. package/android/src/main/java/com/reactnative/hybridnavigation/navigator/TabNavigator.java +2 -16
  14. package/dist/NativeEvent.d.ts +1 -11
  15. package/dist/NativeNavigation.d.ts +7 -6
  16. package/dist/Navigation.d.ts +8 -20
  17. package/dist/Navigation.js +32 -163
  18. package/dist/NavigationComponentWrap.d.ts +3 -3
  19. package/dist/Navigator.d.ts +4 -5
  20. package/dist/Navigator.js +13 -36
  21. package/dist/Options.d.ts +2 -2
  22. package/dist/Route.d.ts +12 -33
  23. package/dist/handler/DispatchCommandHandler.d.ts +3 -17
  24. package/dist/handler/DispatchCommandHandler.js +17 -62
  25. package/dist/handler/LayoutCommandHandler.d.ts +2 -3
  26. package/dist/handler/LayoutCommandHandler.js +0 -3
  27. package/dist/handler/LeaveGuardHandler.d.ts +2 -2
  28. package/dist/handler/LeaveGuardHandler.js +88 -46
  29. package/dist/handler/LifecycleEventHandler.d.ts +2 -14
  30. package/dist/handler/LifecycleEventHandler.js +2 -79
  31. package/dist/handler/ResultEventHandler.d.ts +0 -1
  32. package/dist/handler/ResultEventHandler.js +0 -3
  33. package/dist/handler/SceneExitHandler.d.ts +3 -0
  34. package/dist/handler/SceneExitHandler.js +76 -0
  35. package/dist/hooks.d.ts +2 -2
  36. package/dist/hooks.js +39 -22
  37. package/dist/index.d.ts +1 -12
  38. package/dist/index.js +0 -3
  39. package/dist/router/index.d.ts +0 -1
  40. package/dist/router/index.js +43 -11
  41. package/dist/sceneCoverage.d.ts +2 -0
  42. package/dist/sceneCoverage.js +25 -0
  43. package/dist/store.d.ts +0 -2
  44. package/dist/store.js +0 -4
  45. package/ios/HBDGlobalStyle.h +0 -1
  46. package/ios/HBDGlobalStyle.mm +0 -7
  47. package/ios/HybridNavigation.h +0 -1
  48. package/ios/Navigator/HBDScreenNavigator.mm +89 -237
  49. package/ios/Navigator/HBDStackNavigator.mm +1 -1
  50. package/ios/Navigator/HBDTabNavigator.mm +15 -30
  51. package/ios/React/HBDNativeNavigation.mm +4 -31
  52. package/ios/React/HBDReactBridgeManager.h +0 -5
  53. package/ios/React/HBDReactBridgeManager.mm +0 -17
  54. package/ios/ViewController/HBDReactViewController.h +1 -1
  55. package/ios/ViewController/HBDReactViewController.mm +34 -9
  56. package/ios/ViewController/HBDTabBarController.h +2 -11
  57. package/ios/ViewController/HBDTabBarController.mm +14 -170
  58. package/ios/ViewController/HBDViewController.h +0 -1
  59. package/ios/ViewController/HBDViewController.mm +9 -57
  60. package/ios/ViewController/UIViewController+HBD.h +2 -0
  61. package/ios/ViewController/UIViewController+HBD.mm +5 -0
  62. package/package.json +1 -3
  63. package/src/NativeEvent.ts +1 -12
  64. package/src/NativeNavigation.ts +8 -7
  65. package/src/Navigation.ts +49 -257
  66. package/src/NavigationComponentWrap.tsx +3 -11
  67. package/src/Navigator.ts +15 -45
  68. package/src/Options.ts +2 -3
  69. package/src/Route.ts +17 -54
  70. package/src/handler/DispatchCommandHandler.ts +29 -109
  71. package/src/handler/LayoutCommandHandler.ts +2 -6
  72. package/src/handler/LeaveGuardHandler.ts +97 -46
  73. package/src/handler/LifecycleEventHandler.ts +22 -122
  74. package/src/handler/ResultEventHandler.ts +0 -6
  75. package/src/handler/SceneExitHandler.ts +86 -0
  76. package/src/hooks.ts +39 -22
  77. package/src/index.ts +0 -14
  78. package/src/router/index.ts +45 -12
  79. package/src/sceneCoverage.ts +30 -0
  80. package/src/store.ts +0 -5
  81. package/dist/defineRoutes.d.ts +0 -19
  82. package/dist/defineRoutes.js +0 -6
  83. package/dist/handler/PresentationCoordinator.d.ts +0 -24
  84. package/dist/handler/PresentationCoordinator.js +0 -456
  85. package/dist/handler/TabEventHandler.d.ts +0 -8
  86. package/dist/handler/TabEventHandler.js +0 -25
  87. package/dist/reveal.d.ts +0 -46
  88. package/dist/reveal.js +0 -431
  89. package/dist/routeGraph.d.ts +0 -12
  90. package/dist/routeGraph.js +0 -68
  91. package/dist/testing.d.ts +0 -79
  92. package/dist/testing.js +0 -251
  93. package/ios/ViewController/HBDTabBarRenderer.h +0 -28
  94. package/src/defineRoutes.ts +0 -46
  95. package/src/handler/PresentationCoordinator.ts +0 -566
  96. package/src/handler/TabEventHandler.ts +0 -31
  97. package/src/reveal.ts +0 -565
  98. package/src/routeGraph.ts +0 -87
  99. package/src/testing.ts +0 -395
  100. package/testing.d.ts +0 -1
  101. package/testing.js +0 -3
package/src/Navigation.ts CHANGED
@@ -5,8 +5,6 @@ import type { DefaultOptions, NavigationOption, TabBarStyle, TabItemInfo } from
5
5
  import type {
6
6
  BeforeRemoveListener,
7
7
  BeforeRemoveOptions,
8
- BuildInLayout,
9
- CloseSelfOptions,
10
8
  DispatchResult,
11
9
  Layout,
12
10
  NavigationResult,
@@ -16,20 +14,12 @@ import type {
16
14
  RouteConfig,
17
15
  RouteGraph,
18
16
  RouteName,
19
- SceneExitHandler,
20
- SceneInfo,
21
17
  SceneLifecycleState,
22
18
  SetRootOptions,
23
- TabPressListener,
24
19
  } from './Route';
25
20
 
26
21
  import { layoutHandler, RootLayoutListener } from './handler/LayoutCommandHandler';
27
22
  import { dispatchHandler } from './handler/DispatchCommandHandler';
28
- import { leaveGuardHandler } from './handler/LeaveGuardHandler';
29
- import { presentationCoordinator } from './handler/PresentationCoordinator';
30
- import { tabEventHandler } from './handler/TabEventHandler';
31
- import { executeReveal, type RevealOptions, type RevealResult, type RevealTarget } from './reveal';
32
- import type { RouteDefinition } from './defineRoutes';
33
23
  import {
34
24
  RESULT_BLOCK,
35
25
  RESULT_CANCEL,
@@ -38,27 +28,22 @@ import {
38
28
  } from './handler/ResultEventHandler';
39
29
  import {
40
30
  GlobalSceneLifecycleListener,
41
- GlobalVisibilityEventListener,
42
31
  lifecycleHandler,
43
32
  SceneLifecycleListener,
44
- VisibilityEventListener,
45
33
  } from './handler/LifecycleEventHandler';
46
34
 
35
+ import { leaveGuardHandler, BEFORE_REMOVE_SOURCE_PROP } from './handler/LeaveGuardHandler';
47
36
  import NativeNavigation from './NativeNavigation';
37
+ import { isSceneCoveredInLayers } from './sceneCoverage';
48
38
  import store from './store';
49
39
  import { withNavigation } from './NavigationComponentWrap';
50
40
  import {
51
41
  bumpRouteConfigVersion,
52
42
  clearRouteConfigs,
53
- getRouteConfigs,
54
43
  setRouteConfig,
55
44
  } from './routeConfigRegistry';
56
45
 
57
- export type { ResultType } from './handler/ResultEventHandler';
58
- import type {
59
- DispatchParams,
60
- NavigationInterceptor,
61
- } from './handler/DispatchCommandHandler';
46
+ import type { DispatchParams, NavigationInterceptor } from './handler/DispatchCommandHandler';
62
47
  export type {
63
48
  DispatchParams,
64
49
  InterceptParams,
@@ -66,9 +51,7 @@ export type {
66
51
  } from './handler/DispatchCommandHandler';
67
52
  export type {
68
53
  GlobalSceneLifecycleListener,
69
- GlobalVisibilityEventListener,
70
54
  SceneLifecycleListener,
71
- VisibilityEventListener,
72
55
  } from './handler/LifecycleEventHandler';
73
56
  export type { RootLayoutListener } from './handler/LayoutCommandHandler';
74
57
 
@@ -88,45 +71,20 @@ export interface Navigation {
88
71
  getComponentFunc: ComponentProvider,
89
72
  routeConfig?: RouteConfig,
90
73
  ): void;
91
- registerRoutes(routes: readonly RouteDefinition<any, any, any>[]): void;
92
- preloadRoute(moduleName: RouteName): void;
93
74
 
94
75
  addLifecycleListener(sceneId: string, listener: SceneLifecycleListener): NavigationSubscription;
95
76
  addGlobalLifecycleListener(listener: GlobalSceneLifecycleListener): NavigationSubscription;
96
77
  getSceneState(sceneId: string): SceneLifecycleState;
97
- getSceneInfo(sceneId: string): SceneInfo | undefined;
98
- addGlobalVisibilityEventListener(
99
- listener: GlobalVisibilityEventListener,
100
- ): NavigationSubscription;
101
- addVisibilityEventListener(
102
- sceneId: string,
103
- listener: VisibilityEventListener,
104
- ): NavigationSubscription;
105
- addTabPressListener(listener: TabPressListener): NavigationSubscription;
106
78
 
107
79
  dispatch(sceneId: string, action: string, params?: DispatchParams): Promise<DispatchResult>;
108
80
  addInterceptor(interceptor: NavigationInterceptor): NavigationSubscription;
109
- registerSceneExitHandler(
110
- sceneId: string,
111
- handler: SceneExitHandler,
112
- ): NavigationSubscription;
113
- addBeforeRemoveListener(
114
- sceneId: string,
115
- listener: BeforeRemoveListener,
116
- options?: BeforeRemoveOptions,
117
- ): NavigationSubscription;
118
- closeScene<T extends ResultMap = ResultMap>(
119
- sceneId: string,
120
- options?: CloseSelfOptions<T>,
121
- ): Promise<DispatchResult>;
122
- reveal(target: RevealTarget, options?: RevealOptions): Promise<RevealResult>;
123
81
  setResult<T extends ResultMap = ResultMap>(
124
82
  sceneId: string,
125
83
  resultCode: number,
126
84
  data?: T | null,
127
85
  ): void;
128
86
  result<T = unknown>(sceneId: string, requestCode: number): Promise<NavigationResult<T>>;
129
- setRoot(layout: BuildInLayout | Layout, options?: SetRootOptions): Promise<DispatchResult>;
87
+ setRoot(layout: Layout, options?: SetRootOptions): Promise<DispatchResult>;
130
88
  addRootLayoutUpdateListener(
131
89
  willSetRoot?: RootLayoutListener,
132
90
  didSetRoot?: RootLayoutListener,
@@ -136,56 +94,45 @@ export interface Navigation {
136
94
  findSceneIdByModuleName(moduleName: RouteName): Promise<string | null>;
137
95
  isStackRoot(sceneId: string): Promise<boolean>;
138
96
  currentRoute(): Promise<Route | null>;
139
- routeGraph(): Promise<RouteGraph[]>;
140
97
  navigationSnapshot(): Promise<NavigationSnapshot>;
98
+ addBeforeRemoveListener(
99
+ sceneId: string,
100
+ listener: BeforeRemoveListener,
101
+ options?: BeforeRemoveOptions,
102
+ ): NavigationSubscription;
103
+ isSceneCovered(sceneId: string): Promise<boolean>;
104
+ /** @deprecated Use (await navigationSnapshot()).layers instead. */
105
+ routeGraph(): Promise<RouteGraph[]>;
141
106
 
142
107
  setDefaultOptions(options: DefaultOptions): void;
143
108
  updateOptions(sceneId: string, options: NavigationOption): void;
144
- updateTabBar(options: TabBarStyle): void;
145
109
  updateTabBar(sceneId: string, options: TabBarStyle): void;
146
- setTabItem(item: TabItemInfo | TabItemInfo[]): void;
147
110
  setTabItem(sceneId: string, item: TabItemInfo | TabItemInfo[]): void;
148
111
  setMenuInteractive(sceneId: string, enabled: boolean): Promise<DispatchResult>;
149
112
  }
150
113
 
151
114
  class NavigationImpl implements Navigation {
152
115
  constructor() {
153
- leaveGuardHandler.setDispatcher((sceneId, action, params) =>
154
- dispatchHandler.dispatch(sceneId, action, params),
155
- );
156
- dispatchHandler.addMiddleware(leaveGuardHandler.middleware);
157
- dispatchHandler.addMiddleware(dispatchHandler.interceptorMiddleware);
158
- dispatchHandler.addMiddleware(presentationCoordinator.middleware);
116
+ leaveGuardHandler.setDispatcher((sceneId, action, params) => dispatchHandler.dispatch(sceneId, action, params));
117
+ leaveGuardHandler.handleNativeBeforeRemoveRequests();
159
118
  layoutHandler.handleRootLayoutChange(() => {
160
- lifecycleHandler.onRootReset();
161
119
  leaveGuardHandler.onRootReset();
162
- presentationCoordinator.onRootReset();
163
120
  resultHandler.invalidateAll('root_reset');
164
121
  });
165
122
  lifecycleHandler.handleSceneLifecycle(sceneId => {
166
123
  leaveGuardHandler.onSceneDestroyed(sceneId);
167
- presentationCoordinator.onSceneDestroyed(sceneId);
168
124
  resultHandler.invalidateResultEventListener(sceneId, 'unmounted');
169
125
  store.removeNavigator(sceneId);
170
126
  });
171
127
  resultHandler.handleComponentResult();
172
- leaveGuardHandler.handleNativeBeforeRemoveRequests();
173
- tabEventHandler.handleTabPress();
174
128
  }
175
129
 
176
130
  private hoc?: HOC;
177
131
  private registerEnded = false;
178
- private registeredNames = new Set<string>();
179
- private registeredPaths = new Map<string, string>();
180
- private routeLoaders = new Map<string, () => React.ComponentType<any>>();
181
- private revealQueue: Promise<unknown> = Promise.resolve();
182
132
 
183
133
  startRegisterComponent(hoc?: HOC) {
184
134
  this.hoc = hoc;
185
135
  this.registerEnded = false;
186
- this.registeredNames.clear();
187
- this.registeredPaths.clear();
188
- this.routeLoaders.clear();
189
136
  clearRouteConfigs();
190
137
  NativeNavigation.startRegisterReactComponent();
191
138
  }
@@ -196,78 +143,25 @@ class NavigationImpl implements Navigation {
196
143
  return;
197
144
  }
198
145
  this.registerEnded = true;
199
- if (__DEV__) {
200
- this.validateRouteDependencies();
201
- }
202
146
  bumpRouteConfigVersion();
203
147
  NativeNavigation.endRegisterReactComponent();
204
148
  }
205
149
 
206
- private validateRouteDependencies() {
207
- const configs = getRouteConfigs();
208
- const state = new Map<string, 'visiting' | 'visited'>();
209
- const stack: string[] = [];
210
-
211
- const visit = (name: string) => {
212
- state.set(name, 'visiting');
213
- stack.push(name);
214
- const dependency = configs.get(name)?.dependency;
215
- if (dependency) {
216
- const depState = state.get(dependency);
217
- if (depState === 'visiting') {
218
- const cycleStart = stack.indexOf(dependency);
219
- const cycle = [...stack.slice(cycleStart), dependency].join(' -> ');
220
- console.warn(`[hybrid-navigation] Circular route dependency detected: ${cycle}`);
221
- } else if (!depState && configs.has(dependency)) {
222
- visit(dependency);
223
- }
224
- }
225
- stack.pop();
226
- state.set(name, 'visited');
227
- };
228
-
229
- for (const name of configs.keys()) {
230
- if (!state.has(name)) {
231
- visit(name);
232
- }
233
- }
234
- }
235
-
236
150
  registerComponent<Name extends RouteName = RouteName>(
237
151
  appKey: Name,
238
152
  getComponentFunc: ComponentProvider,
239
153
  routeConfig?: RouteConfig,
240
154
  ) {
241
- if (__DEV__) {
242
- if (this.registeredNames.has(appKey)) {
243
- console.warn(
244
- `[hybrid-navigation] Duplicate route registration for "${appKey}". Later registration overrides earlier one.`,
245
- );
246
- }
247
- if (routeConfig?.path) {
248
- const existing = this.registeredPaths.get(routeConfig.path);
249
- if (existing && existing !== appKey) {
250
- console.warn(
251
- `[hybrid-navigation] Duplicate route path "${routeConfig.path}" registered by "${appKey}" and "${existing}".`,
252
- );
253
- }
254
- this.registeredPaths.set(routeConfig.path, appKey);
255
- }
256
- }
257
- this.registeredNames.add(appKey);
258
-
259
155
  if (routeConfig && (routeConfig.path || routeConfig.dependency || routeConfig.mode)) {
260
156
  setRouteConfig(appKey, routeConfig);
261
157
  }
262
158
 
263
159
  const hoc = this.hoc;
264
160
 
265
- const isLazy = routeConfig?.lazy === true;
266
-
267
- if (isLazy) {
268
- NativeNavigation.registerReactComponent(appKey, routeConfig?.options ?? {});
161
+ if (routeConfig?.options !== undefined || routeConfig?.lazy === true) {
162
+ NativeNavigation.registerReactComponent(appKey, routeConfig.options ?? {});
269
163
  let CachedRootComponent: React.ComponentType<any> | undefined;
270
- const loader = () => {
164
+ AppRegistry.registerComponent(appKey, () => {
271
165
  if (!CachedRootComponent) {
272
166
  let WrappedComponent = getComponentFunc();
273
167
  if (__DEV__ && (WrappedComponent as any)?.navigationItem) {
@@ -281,57 +175,22 @@ class NavigationImpl implements Navigation {
281
175
  CachedRootComponent = withNavigation(appKey)(WrappedComponent);
282
176
  }
283
177
  return CachedRootComponent;
284
- };
285
- this.routeLoaders.set(appKey, loader);
286
- AppRegistry.registerComponent(appKey, loader);
178
+ });
287
179
  return;
288
180
  }
289
181
 
290
- const RawComponent = getComponentFunc();
291
- let WrappedComponent = RawComponent;
182
+ let WrappedComponent = getComponentFunc();
292
183
  if (hoc) {
293
184
  WrappedComponent = hoc(WrappedComponent);
294
185
  }
295
186
 
296
- const options: object =
297
- routeConfig?.options ??
298
- (RawComponent as any)?.navigationItem ??
299
- (WrappedComponent as any)?.navigationItem ??
300
- {};
187
+ const options: object = (WrappedComponent as any).navigationItem || {};
301
188
  NativeNavigation.registerReactComponent(appKey, options);
302
189
 
303
190
  const RootComponent = withNavigation(appKey)(WrappedComponent);
304
191
  AppRegistry.registerComponent(appKey, () => RootComponent);
305
192
  }
306
193
 
307
- registerRoutes(routes: readonly RouteDefinition<any, any, any>[]) {
308
- for (const route of routes) {
309
- const hasConfig =
310
- route.path !== undefined ||
311
- route.dependency !== undefined ||
312
- route.mode !== undefined ||
313
- route.options !== undefined ||
314
- route.lazy !== undefined;
315
- const config: RouteConfig | undefined = hasConfig
316
- ? {
317
- path: route.path,
318
- dependency: route.dependency as RouteName | undefined,
319
- mode: route.mode,
320
- options: route.options,
321
- lazy: route.lazy,
322
- }
323
- : undefined;
324
- this.registerComponent(route.name as RouteName, route.load, config);
325
- }
326
- }
327
-
328
- preloadRoute(moduleName: RouteName) {
329
- const loader = this.routeLoaders.get(moduleName);
330
- if (loader) {
331
- loader();
332
- }
333
- }
334
-
335
194
  addLifecycleListener(
336
195
  sceneId: string,
337
196
  listener: SceneLifecycleListener,
@@ -353,36 +212,9 @@ class NavigationImpl implements Navigation {
353
212
  return lifecycleHandler.getSceneState(sceneId);
354
213
  }
355
214
 
356
- getSceneInfo(sceneId: string): SceneInfo | undefined {
357
- return lifecycleHandler.getSceneInfo(sceneId);
358
- }
359
-
360
- addGlobalVisibilityEventListener(
361
- listener: GlobalVisibilityEventListener,
362
- ): NavigationSubscription {
363
- lifecycleHandler.addGlobalVisibilityEventListener(listener);
364
- return {
365
- remove: () => lifecycleHandler.removeGlobalVisibilityEventListener(listener),
366
- };
367
- }
368
-
369
- addVisibilityEventListener(
370
- sceneId: string,
371
- listener: VisibilityEventListener,
372
- ): NavigationSubscription {
373
- lifecycleHandler.addVisibilityEventListener(sceneId, listener);
374
- return {
375
- remove: () => lifecycleHandler.removeVisibilityEventListener(sceneId, listener),
376
- };
377
- }
378
-
379
- addTabPressListener(listener: TabPressListener): NavigationSubscription {
380
- const remove = tabEventHandler.addListener(listener);
381
- return { remove };
382
- }
383
215
 
384
216
  dispatch(sceneId: string, action: string, params: DispatchParams = {}) {
385
- return dispatchHandler.dispatch(sceneId, action, params);
217
+ return dispatchHandler.dispatch(sceneId, action, { ...params, [BEFORE_REMOVE_SOURCE_PROP]: 'external' });
386
218
  }
387
219
 
388
220
  addInterceptor(interceptor: NavigationInterceptor): NavigationSubscription {
@@ -392,46 +224,6 @@ class NavigationImpl implements Navigation {
392
224
  };
393
225
  }
394
226
 
395
- registerSceneExitHandler(
396
- sceneId: string,
397
- handler: SceneExitHandler,
398
- ): NavigationSubscription {
399
- const remove = presentationCoordinator.registerExitHandler(sceneId, handler);
400
- return { remove };
401
- }
402
-
403
- addBeforeRemoveListener(
404
- sceneId: string,
405
- listener: BeforeRemoveListener,
406
- options?: BeforeRemoveOptions,
407
- ): NavigationSubscription {
408
- const remove = leaveGuardHandler.addListener(sceneId, listener, options);
409
- return { remove };
410
- }
411
-
412
- closeScene<T extends ResultMap = ResultMap>(
413
- sceneId: string,
414
- options?: CloseSelfOptions<T>,
415
- ): Promise<DispatchResult> {
416
- return this.dispatch(sceneId, 'close', {
417
- target: 'self',
418
- onCovered: options?.onCovered,
419
- ...(options?.result !== undefined ? { result: options.result } : {}),
420
- });
421
- }
422
-
423
- reveal(target: RevealTarget, options?: RevealOptions): Promise<RevealResult> {
424
- const task = this.revealQueue.then(
425
- () => executeReveal(this, target, options),
426
- () => executeReveal(this, target, options),
427
- );
428
- this.revealQueue = task.then(
429
- () => undefined,
430
- () => undefined,
431
- );
432
- return task;
433
- }
434
-
435
227
  setResult = <T extends ResultMap = ResultMap>(
436
228
  sceneId: string,
437
229
  resultCode: number,
@@ -444,7 +236,7 @@ class NavigationImpl implements Navigation {
444
236
  return resultHandler.waitResult<T>(sceneId, requestCode);
445
237
  }
446
238
 
447
- setRoot(layout: BuildInLayout | Layout, options?: SetRootOptions) {
239
+ setRoot(layout: Layout, options?: SetRootOptions) {
448
240
  return layoutHandler.setRoot(layout, options);
449
241
  }
450
242
 
@@ -472,14 +264,32 @@ class NavigationImpl implements Navigation {
472
264
  return NativeNavigation.currentRoute();
473
265
  }
474
266
 
475
- routeGraph(): Promise<RouteGraph[]> {
476
- return NativeNavigation.routeGraph();
477
- }
478
-
479
267
  navigationSnapshot(): Promise<NavigationSnapshot> {
480
268
  return NativeNavigation.navigationSnapshot();
481
269
  }
482
270
 
271
+ addBeforeRemoveListener(
272
+ sceneId: string,
273
+ listener: BeforeRemoveListener,
274
+ options?: BeforeRemoveOptions,
275
+ ): NavigationSubscription {
276
+ const remove = leaveGuardHandler.addListener(sceneId, listener, options);
277
+ return { remove };
278
+ }
279
+
280
+ async isSceneCovered(sceneId: string): Promise<boolean> {
281
+ try {
282
+ const { layers } = await NativeNavigation.navigationSnapshot();
283
+ return isSceneCoveredInLayers(layers, sceneId, 'visual');
284
+ } catch {
285
+ return false;
286
+ }
287
+ }
288
+
289
+ async routeGraph(): Promise<RouteGraph[]> {
290
+ return (await NativeNavigation.navigationSnapshot()).layers;
291
+ }
292
+
483
293
  setDefaultOptions(options: DefaultOptions) {
484
294
  NativeNavigation.setDefaultOptions(options);
485
295
  }
@@ -488,31 +298,13 @@ class NavigationImpl implements Navigation {
488
298
  NativeNavigation.updateOptions(sceneId, options);
489
299
  }
490
300
 
491
- updateTabBar(options: TabBarStyle): void;
492
- updateTabBar(sceneId: string, options: TabBarStyle): void;
493
- updateTabBar(sceneIdOrOptions: string | TabBarStyle, options?: TabBarStyle) {
494
- if (typeof sceneIdOrOptions === 'string') {
495
- NativeNavigation.updateTabBar(sceneIdOrOptions, options ?? {});
496
- } else {
497
- NativeNavigation.updateTabBar('', sceneIdOrOptions);
498
- }
301
+ updateTabBar(sceneId: string, options: TabBarStyle) {
302
+ NativeNavigation.updateTabBar(sceneId, options);
499
303
  }
500
304
 
501
- setTabItem(item: TabItemInfo | TabItemInfo[]): void;
502
- setTabItem(sceneId: string, item: TabItemInfo | TabItemInfo[]): void;
503
- setTabItem(
504
- sceneIdOrItem: string | TabItemInfo | TabItemInfo[],
505
- item?: TabItemInfo | TabItemInfo[],
506
- ) {
507
- if (typeof sceneIdOrItem === 'string') {
508
- const items = Array.isArray(item) ? item : item ? [item] : [];
509
- if (items.length > 0) {
510
- NativeNavigation.setTabItem(sceneIdOrItem, items);
511
- }
512
- } else {
513
- const items = Array.isArray(sceneIdOrItem) ? sceneIdOrItem : [sceneIdOrItem];
514
- NativeNavigation.setTabItem('', items);
515
- }
305
+ setTabItem(sceneId: string, item: TabItemInfo | TabItemInfo[]) {
306
+ const items = Array.isArray(item) ? item : [item];
307
+ NativeNavigation.setTabItem(sceneId, items);
516
308
  }
517
309
 
518
310
  setMenuInteractive(sceneId: string, enabled: boolean) {
@@ -1,23 +1,15 @@
1
1
  import React, { useEffect } from 'react';
2
2
  import type { NavigationItem } from './Options';
3
- import type {
4
- HasTypedRoutes,
5
- InitialMetrics,
6
- RegisteredRouteName,
7
- RouteName,
8
- RoutePropsOf,
9
- } from './Route';
3
+ import type { HasTypedRoutes, InitialMetrics, RouteName, RoutePropsOf } from './Route';
10
4
  import { Navigator, NavigationContext } from './Navigator';
11
5
  import NativeNavigation from './NativeNavigation';
12
6
 
13
7
  export type NavigationProps<Name extends RouteName = RouteName> = (HasTypedRoutes extends true
14
- ? [Name] extends [RegisteredRouteName]
15
- ? RoutePropsOf<Name>
16
- : Record<string, any>
8
+ ? RoutePropsOf<Name>
17
9
  : Record<string, any>) & {
18
10
  navigator: Navigator<Name>;
19
- sceneId: string;
20
11
  initialMetrics?: InitialMetrics;
12
+ sceneId: string;
21
13
  };
22
14
 
23
15
  interface NativeProps {
package/src/Navigator.ts CHANGED
@@ -7,7 +7,6 @@ import { lifecycleHandler } from './handler/LifecycleEventHandler';
7
7
  import { RESULT_BLOCK, resultHandler } from './handler/ResultEventHandler';
8
8
  import type { NavigationOption, TabBarStyle, TabItemInfo } from './Options';
9
9
  import type {
10
- BuildInLayout,
11
10
  CloseSelfOptions,
12
11
  DispatchResult,
13
12
  Layout,
@@ -17,7 +16,6 @@ import type {
17
16
  SceneLifecycleState,
18
17
  ScreenPropsArgs,
19
18
  SetResultData,
20
- Visibility,
21
19
  } from './Route';
22
20
 
23
21
  export const NavigationContext = createContext<Navigator<any> | null>(null);
@@ -55,17 +53,6 @@ export class Navigator<CurrentRoute extends RouteName = RouteName> {
55
53
 
56
54
  constructor(public sceneId: string, public moduleName?: string) {}
57
55
 
58
- get visibility(): Visibility {
59
- const state = lifecycleHandler.getSceneState(this.sceneId);
60
- if (state === 'visible') {
61
- return 'visible';
62
- }
63
- if (state === 'invisible' || state === 'destroyed') {
64
- return 'invisible';
65
- }
66
- return 'pending';
67
- }
68
-
69
56
  get lifecycleState(): SceneLifecycleState {
70
57
  return lifecycleHandler.getSceneState(this.sceneId);
71
58
  }
@@ -76,13 +63,10 @@ export class Navigator<CurrentRoute extends RouteName = RouteName> {
76
63
  warnOnFailure?: (code: DispatchResult['code']) => string,
77
64
  ): Promise<NavigationResult<T>> => {
78
65
  const requestCode = --tagGenerator;
79
- const { promise: resultPromise, cancel } = resultHandler.prepareResult<T>(
80
- this.sceneId,
81
- requestCode,
82
- );
83
- let dispatchResult: DispatchResult;
66
+ const { promise, cancel } = resultHandler.prepareResult<T>(this.sceneId, requestCode);
67
+ let result: DispatchResult;
84
68
  try {
85
- dispatchResult = await dispatchHandler.dispatch(this.sceneId, action, {
69
+ result = await dispatchHandler.dispatch(this.sceneId, action, {
86
70
  ...params,
87
71
  requestCode,
88
72
  });
@@ -90,18 +74,14 @@ export class Navigator<CurrentRoute extends RouteName = RouteName> {
90
74
  cancel();
91
75
  throw error;
92
76
  }
93
- if (!dispatchResult.ok) {
77
+ if (!result.ok) {
94
78
  cancel();
95
79
  if (warnOnFailure) {
96
- console.warn(warnOnFailure(dispatchResult.code));
80
+ console.warn(warnOnFailure(result.code));
97
81
  }
98
- return {
99
- status: 'blocked',
100
- code: RESULT_BLOCK,
101
- reason: dispatchResult.code,
102
- };
82
+ return { status: 'blocked', code: RESULT_BLOCK, reason: result.code };
103
83
  }
104
- return resultPromise;
84
+ return promise;
105
85
  };
106
86
 
107
87
  push = async <Name extends RouteName = RouteName, T = RouteResultOf<Name>>(
@@ -119,7 +99,7 @@ export class Navigator<CurrentRoute extends RouteName = RouteName> {
119
99
  };
120
100
 
121
101
  pushLayout = async <T = unknown>(
122
- layout: BuildInLayout | Layout,
102
+ layout: Layout,
123
103
  ): Promise<NavigationResult<T>> => {
124
104
  return this.dispatchForResult<T>('pushLayout', {
125
105
  from: this.moduleName,
@@ -177,7 +157,7 @@ export class Navigator<CurrentRoute extends RouteName = RouteName> {
177
157
  };
178
158
 
179
159
  presentLayout = async <T = unknown>(
180
- layout: BuildInLayout | Layout,
160
+ layout: Layout,
181
161
  ): Promise<NavigationResult<T>> => {
182
162
  return this.dispatchForResult<T>(
183
163
  'presentLayout',
@@ -192,16 +172,12 @@ export class Navigator<CurrentRoute extends RouteName = RouteName> {
192
172
  dismiss = (): Promise<DispatchResult> =>
193
173
  dispatchHandler.dispatch(this.sceneId, 'dismiss', { from: this.moduleName });
194
174
 
195
- closeSelf = (
196
- options?: CloseSelfOptions<SetResultData<CurrentRoute>>,
197
- ): Promise<DispatchResult> => {
198
- return dispatchHandler.dispatch(this.sceneId, 'close', {
175
+ closeSelf = (options?: CloseSelfOptions<SetResultData<CurrentRoute>>): Promise<DispatchResult> =>
176
+ dispatchHandler.dispatch(this.sceneId, 'close', {
199
177
  from: this.moduleName,
200
- target: 'self',
201
- onCovered: options?.onCovered,
202
- ...(options?.result !== undefined ? { result: options.result } : {}),
178
+ ...(options?.bypassBeforeRemove && { bypassBeforeRemove: true }),
179
+ ...(options?.result && { result: resultHandler.normalizePendingResult(options.result) }),
203
180
  });
204
- };
205
181
 
206
182
  showModal = async <Name extends RouteName = RouteName, T = RouteResultOf<Name>>(
207
183
  moduleName: Name,
@@ -222,7 +198,7 @@ export class Navigator<CurrentRoute extends RouteName = RouteName> {
222
198
  };
223
199
 
224
200
  showModalLayout = async <T = unknown>(
225
- layout: BuildInLayout | Layout,
201
+ layout: Layout,
226
202
  ): Promise<NavigationResult<T>> => {
227
203
  return this.dispatchForResult<T>(
228
204
  'showModalLayout',
@@ -247,16 +223,10 @@ export class Navigator<CurrentRoute extends RouteName = RouteName> {
247
223
  popToRoot: boolean = false,
248
224
  ): Promise<DispatchResult> => {
249
225
  const from = await NativeNavigation.currentTab(this.sceneId);
250
- if (typeof target === 'string') {
251
- return dispatchHandler.dispatch(this.sceneId, 'switchTab', {
252
- from,
253
- tabKey: target,
254
- popToRoot,
255
- });
256
- }
257
226
  return dispatchHandler.dispatch(this.sceneId, 'switchTab', {
258
227
  from,
259
228
  to: target,
229
+ ...(typeof target === 'string' && { tabKey: target }),
260
230
  popToRoot,
261
231
  });
262
232
  };
package/src/Options.ts CHANGED
@@ -44,8 +44,7 @@ export interface ShadowImage {
44
44
 
45
45
  export interface DefaultOptions {
46
46
  screenBackgroundColor?: Color; // 页面背景,默认是白色
47
- modalBackgroundColor?: Color; // 弹层默认背景,默认透明
48
- injectInitialMetrics?: boolean; // 是否为同向全屏场景注入首帧安全区 initialMetrics(默认 false)
47
+ injectInitialMetrics?: boolean; // 同向全屏场景的首帧安全区,默认关闭
49
48
  prewarmPush?: boolean; // 是否对有动画的同向全屏主栈 push 启用离屏 Fabric 首帧预热(默认 false)
50
49
  statusBarStyle?: BarStyle; // 状态栏样式,可选项有 `BarStyleLightContent` 和 `BarStyleDarkContent`
51
50
  navigationBarColorAndroid?: Color; // 底部虚拟键背景颜色,仅对 Android 8.0 以上版本生效
@@ -67,7 +66,7 @@ export type TabBarStyle = Pick<
67
66
  | 'tabBarItemNormalColor'
68
67
  >;
69
68
 
70
- export interface TabItemInfoBase {
69
+ interface TabItemInfoBase {
71
70
  title?: string;
72
71
  badge?: {
73
72
  text?: string;