hybrid-navigation 4.1.0 → 5.1.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 (177) hide show
  1. package/HybridNavigation.podspec +9 -1
  2. package/README.md +12 -4
  3. package/android/build.gradle +4 -4
  4. package/android/proguard-rules.pro +0 -14
  5. package/android/src/main/java/com/navigation/androidx/AwesomeActivity.java +116 -27
  6. package/android/src/main/java/com/navigation/androidx/AwesomeFragment.java +162 -13
  7. package/android/src/main/java/com/navigation/androidx/DefaultTabBarProvider.java +2 -2
  8. package/android/src/main/java/com/navigation/androidx/DrawableUtils.java +83 -18
  9. package/android/src/main/java/com/navigation/androidx/DrawerFragment.java +185 -25
  10. package/android/src/main/java/com/navigation/androidx/FragmentHelper.java +18 -0
  11. package/android/src/main/java/com/navigation/androidx/HybridDrawerLayout.java +563 -143
  12. package/android/src/main/java/com/navigation/androidx/LifecycleDelegate.java +39 -12
  13. package/android/src/main/java/com/navigation/androidx/PresentableActivity.java +3 -0
  14. package/android/src/main/java/com/navigation/androidx/PresentationDelegate.java +10 -7
  15. package/android/src/main/java/com/navigation/androidx/StackDelegate.java +29 -9
  16. package/android/src/main/java/com/navigation/androidx/StackFragment.java +31 -3
  17. package/android/src/main/java/com/navigation/androidx/SystemUI.java +59 -90
  18. package/android/src/main/java/com/navigation/androidx/TabBar.java +21 -2
  19. package/android/src/main/java/com/navigation/androidx/TabBarFragment.java +74 -7
  20. package/android/src/main/java/com/navigation/androidx/TabBarItem.java +4 -0
  21. package/android/src/main/java/com/navigation/androidx/TabView.java +11 -2
  22. package/android/src/main/java/com/reactnative/hybridnavigation/DispatchResult.java +71 -0
  23. package/android/src/main/java/com/reactnative/hybridnavigation/Garden.java +18 -10
  24. package/android/src/main/java/com/reactnative/hybridnavigation/GlobalStyle.java +34 -2
  25. package/android/src/main/java/com/reactnative/hybridnavigation/HBDRootView.java +20 -10
  26. package/android/src/main/java/com/reactnative/hybridnavigation/HybridFragment.java +179 -13
  27. package/android/src/main/java/com/reactnative/hybridnavigation/HybridNavigationPackage.java +13 -24
  28. package/android/src/main/java/com/reactnative/hybridnavigation/NativeEvent.java +5 -1
  29. package/android/src/main/java/com/reactnative/hybridnavigation/NativeNavigation.java +283 -183
  30. package/android/src/main/java/com/reactnative/hybridnavigation/NavigationCommandQueue.java +507 -0
  31. package/android/src/main/java/com/reactnative/hybridnavigation/Navigator.java +4 -1
  32. package/android/src/main/java/com/reactnative/hybridnavigation/OrientationCoordinator.java +162 -0
  33. package/android/src/main/java/com/reactnative/hybridnavigation/ReactAppCompatActivity.java +103 -66
  34. package/android/src/main/java/com/reactnative/hybridnavigation/ReactFragment.java +421 -92
  35. package/android/src/main/java/com/reactnative/hybridnavigation/ReactManager.java +227 -78
  36. package/android/src/main/java/com/reactnative/hybridnavigation/ReactStackFragment.java +6 -6
  37. package/android/src/main/java/com/reactnative/hybridnavigation/ReactTabBarFragment.java +26 -0
  38. package/android/src/main/java/com/reactnative/hybridnavigation/ReactTabBarProvider.java +8 -1
  39. package/android/src/main/java/com/reactnative/hybridnavigation/SceneLifecycleState.java +21 -0
  40. package/android/src/main/java/com/reactnative/hybridnavigation/SceneLifecycleStateMachine.java +116 -0
  41. package/android/src/main/java/com/reactnative/hybridnavigation/ScreenOrientation.java +42 -0
  42. package/android/src/main/java/com/reactnative/hybridnavigation/Utils.java +17 -0
  43. package/android/src/main/java/com/reactnative/hybridnavigation/navigator/DrawerNavigator.java +14 -6
  44. package/android/src/main/java/com/reactnative/hybridnavigation/navigator/ScreenNavigator.java +188 -26
  45. package/android/src/main/java/com/reactnative/hybridnavigation/navigator/StackNavigator.java +67 -16
  46. package/android/src/main/java/com/reactnative/hybridnavigation/navigator/TabNavigator.java +42 -14
  47. package/dist/DeepLink.d.ts +3 -2
  48. package/dist/DeepLink.js +37 -14
  49. package/dist/NativeEvent.d.ts +17 -10
  50. package/dist/NativeNavigation.d.ts +24 -18
  51. package/dist/Navigation.d.ts +34 -19
  52. package/dist/Navigation.js +225 -90
  53. package/dist/NavigationComponentWrap.d.ts +5 -3
  54. package/dist/NavigationComponentWrap.js +3 -6
  55. package/dist/Navigator.d.ts +32 -42
  56. package/dist/Navigator.js +117 -91
  57. package/dist/Options.d.ts +15 -3
  58. package/dist/Route.d.ts +149 -17
  59. package/dist/defineRoutes.d.ts +19 -0
  60. package/dist/defineRoutes.js +6 -0
  61. package/dist/handler/DispatchCommandHandler.d.ts +25 -6
  62. package/dist/handler/DispatchCommandHandler.js +99 -17
  63. package/dist/handler/LayoutCommandHandler.d.ts +8 -7
  64. package/dist/handler/LayoutCommandHandler.js +24 -15
  65. package/dist/handler/LeaveGuardHandler.d.ts +17 -0
  66. package/dist/handler/LeaveGuardHandler.js +350 -0
  67. package/dist/handler/LifecycleEventHandler.d.ts +30 -0
  68. package/dist/handler/LifecycleEventHandler.js +169 -0
  69. package/dist/handler/PresentationCoordinator.d.ts +24 -0
  70. package/dist/handler/PresentationCoordinator.js +449 -0
  71. package/dist/handler/ResultEventHandler.d.ts +21 -9
  72. package/dist/handler/ResultEventHandler.js +96 -13
  73. package/dist/handler/TabEventHandler.d.ts +8 -0
  74. package/dist/handler/TabEventHandler.js +25 -0
  75. package/dist/hooks.d.ts +7 -2
  76. package/dist/hooks.js +46 -4
  77. package/dist/index.d.ts +24 -0
  78. package/dist/index.js +3 -2
  79. package/dist/reveal.d.ts +46 -0
  80. package/dist/reveal.js +431 -0
  81. package/dist/routeConfigRegistry.d.ts +6 -0
  82. package/dist/routeConfigRegistry.js +19 -0
  83. package/dist/routeGraph.d.ts +12 -0
  84. package/dist/routeGraph.js +68 -0
  85. package/dist/router/drawer.d.ts +2 -2
  86. package/dist/router/drawer.js +6 -3
  87. package/dist/router/index.d.ts +8 -5
  88. package/dist/router/index.js +106 -58
  89. package/dist/router/screen.d.ts +2 -2
  90. package/dist/router/screen.js +1 -1
  91. package/dist/router/stack.d.ts +2 -2
  92. package/dist/router/stack.js +59 -30
  93. package/dist/router/tabs.d.ts +3 -2
  94. package/dist/router/tabs.js +4 -1
  95. package/dist/store.d.ts +4 -2
  96. package/dist/store.js +4 -0
  97. package/dist/testing.d.ts +79 -0
  98. package/dist/testing.js +251 -0
  99. package/ios/HBDGlobalStyle.h +29 -0
  100. package/ios/{GlobalStyle.mm → HBDGlobalStyle.mm} +43 -29
  101. package/ios/HybridNavigation.h +5 -1
  102. package/ios/Navigator/HBDDrawerNavigator.mm +24 -8
  103. package/ios/Navigator/HBDNavigatorRegistry.mm +1 -0
  104. package/ios/Navigator/HBDScreenNavigator.mm +278 -58
  105. package/ios/Navigator/HBDStackNavigator.mm +152 -61
  106. package/ios/Navigator/HBDTabNavigator.mm +60 -14
  107. package/ios/React/HBDNativeNavigation.mm +162 -123
  108. package/ios/React/HBDNavigator.h +37 -5
  109. package/ios/React/HBDOrientationCoordinator.h +29 -0
  110. package/ios/React/HBDOrientationCoordinator.mm +309 -0
  111. package/ios/React/HBDReactBridgeManager.h +46 -20
  112. package/ios/React/HBDReactBridgeManager.mm +496 -108
  113. package/ios/React/HBDSceneLifecycleStateMachine.h +38 -0
  114. package/ios/React/HBDSceneLifecycleStateMachine.mm +180 -0
  115. package/ios/View/HBDRootView.h +2 -0
  116. package/ios/View/HBDRootView.mm +148 -0
  117. package/ios/View/UIImage+WithBadge.h +3 -3
  118. package/ios/View/UIImage+WithBadge.mm +10 -15
  119. package/ios/ViewController/HBDDrawerController.mm +126 -5
  120. package/ios/ViewController/HBDNavigationController.mm +31 -4
  121. package/ios/ViewController/HBDReactViewController.h +6 -0
  122. package/ios/ViewController/HBDReactViewController.mm +220 -106
  123. package/ios/ViewController/HBDTabBarController.h +15 -0
  124. package/ios/ViewController/HBDTabBarController.mm +227 -45
  125. package/ios/ViewController/HBDTabBarRenderer.h +28 -0
  126. package/ios/ViewController/HBDViewController.h +11 -3
  127. package/ios/ViewController/HBDViewController.mm +209 -159
  128. package/ios/ViewController/UINavigationController+HBD.h +2 -2
  129. package/ios/ViewController/UINavigationController+HBD.mm +3 -3
  130. package/ios/ViewController/UIViewController+HBD.h +18 -10
  131. package/ios/ViewController/UIViewController+HBD.mm +90 -65
  132. package/package.json +3 -1
  133. package/src/DeepLink.ts +41 -14
  134. package/src/NativeEvent.ts +18 -11
  135. package/src/NativeNavigation.ts +29 -31
  136. package/src/Navigation.ts +377 -120
  137. package/src/NavigationComponentWrap.tsx +18 -9
  138. package/src/Navigator.ts +219 -152
  139. package/src/Options.ts +12 -3
  140. package/src/Route.ts +228 -17
  141. package/src/defineRoutes.ts +46 -0
  142. package/src/handler/DispatchCommandHandler.ts +154 -21
  143. package/src/handler/LayoutCommandHandler.ts +40 -17
  144. package/src/handler/LeaveGuardHandler.ts +436 -0
  145. package/src/handler/LifecycleEventHandler.ts +219 -0
  146. package/src/handler/PresentationCoordinator.ts +560 -0
  147. package/src/handler/ResultEventHandler.ts +126 -19
  148. package/src/handler/TabEventHandler.ts +31 -0
  149. package/src/hooks.ts +75 -7
  150. package/src/index.ts +37 -2
  151. package/src/reveal.ts +565 -0
  152. package/src/routeConfigRegistry.ts +26 -0
  153. package/src/routeGraph.ts +87 -0
  154. package/src/router/drawer.ts +8 -5
  155. package/src/router/index.ts +139 -63
  156. package/src/router/screen.ts +3 -3
  157. package/src/router/stack.ts +80 -29
  158. package/src/router/tabs.ts +16 -7
  159. package/src/store.ts +6 -1
  160. package/src/testing.ts +395 -0
  161. package/testing.d.ts +1 -0
  162. package/testing.js +3 -0
  163. package/android/src/main/java/androidx/fragment/app/InternalFragment.java +0 -22
  164. package/android/src/main/java/com/navigation/androidx/SystemUI30.java +0 -87
  165. package/android/src/main/java/com/reactnative/hybridnavigation/NativeGarden.java +0 -176
  166. package/android/src/main/java/com/reactnative/hybridnavigation/ReactTabBar.java +0 -130
  167. package/android/src/main/res/layout/nav_tab_bar_react.xml +0 -32
  168. package/android/src/main/res/layout/nav_tab_bar_react_indeterminate.xml +0 -32
  169. package/dist/NativeGarden.d.ts +0 -13
  170. package/dist/NativeGarden.js +0 -2
  171. package/dist/handler/VisibilityEventHandler.d.ts +0 -13
  172. package/dist/handler/VisibilityEventHandler.js +0 -39
  173. package/ios/GlobalStyle.h +0 -15
  174. package/ios/React/HBDNativeGarden.h +0 -10
  175. package/ios/React/HBDNativeGarden.mm +0 -111
  176. package/src/NativeGarden.ts +0 -16
  177. package/src/handler/VisibilityEventHandler.ts +0 -52
@@ -1,7 +1,52 @@
1
1
  import NativeNavigation from '../NativeNavigation';
2
+ import { resultHandler } from './ResultEventHandler';
3
+ const INTERCEPT_CHECKED_PROP = '__hbdInterceptChecked';
4
+ const INTERNAL_DISPATCH_PROPS = [
5
+ INTERCEPT_CHECKED_PROP,
6
+ 'onCovered',
7
+ 'bypassBeforeRemove',
8
+ '__hbdBypassBeforeRemove',
9
+ '__hbdApprovedBeforeRemoveScenes',
10
+ ];
11
+ const VALID_DISPATCH_CODES = new Set([
12
+ 'ok',
13
+ 'intercepted',
14
+ 'scene_not_found',
15
+ 'already_presenting',
16
+ 'covered',
17
+ 'not_ready',
18
+ 'superseded',
19
+ 'invalid_action',
20
+ 'host_unavailable',
21
+ ]);
22
+ export function normalizeDispatchResult(wire) {
23
+ if (!wire) {
24
+ return { ok: false, code: 'invalid_action' };
25
+ }
26
+ const code = VALID_DISPATCH_CODES.has(wire.code)
27
+ ? wire.code
28
+ : wire.ok
29
+ ? 'ok'
30
+ : 'invalid_action';
31
+ const result = { ok: Boolean(wire.ok), code };
32
+ if (wire.message) {
33
+ result.message = wire.message;
34
+ }
35
+ if (wire.sceneId) {
36
+ result.sceneId = wire.sceneId;
37
+ }
38
+ return result;
39
+ }
2
40
  export default class DispatchCommandHandler {
3
- interceptor = () => false;
4
- async dispatch(sceneId, action, params) {
41
+ interceptors = [];
42
+ systemMiddlewares = [];
43
+ addMiddleware(middleware) {
44
+ this.systemMiddlewares.push(middleware);
45
+ }
46
+ removeMiddleware(middleware) {
47
+ this.systemMiddlewares = this.systemMiddlewares.filter(item => item !== middleware);
48
+ }
49
+ interceptorMiddleware = async (sceneId, action, params, next) => {
5
50
  const { from, to, props } = params;
6
51
  const intercepted = await this.intercept(action, {
7
52
  sceneId,
@@ -10,25 +55,62 @@ export default class DispatchCommandHandler {
10
55
  props,
11
56
  });
12
57
  if (intercepted) {
13
- return false;
58
+ return { ok: false, code: 'intercepted' };
14
59
  }
15
- return new Promise(resolve => {
16
- NativeNavigation.dispatch(sceneId, action, params, (_, result) => {
17
- resolve(result);
60
+ return next(sceneId, action, { ...params, [INTERCEPT_CHECKED_PROP]: true });
61
+ };
62
+ async dispatchRaw(sceneId, action, params) {
63
+ if (!params[INTERCEPT_CHECKED_PROP]) {
64
+ const { from, to, props } = params;
65
+ const intercepted = await this.intercept(action, {
66
+ sceneId,
67
+ from,
68
+ to,
69
+ props,
18
70
  });
19
- });
20
- }
21
- intercept(action, params) {
22
- if (!this.interceptor) {
23
- return Promise.resolve(false);
71
+ if (intercepted) {
72
+ return { ok: false, code: 'intercepted' };
73
+ }
74
+ }
75
+ const nativeParams = { ...params };
76
+ for (const key of INTERNAL_DISPATCH_PROPS) {
77
+ delete nativeParams[key];
24
78
  }
25
- const result = this.interceptor(action, params);
26
- if (result instanceof Promise) {
27
- return result;
79
+ if (nativeParams.result !== undefined) {
80
+ nativeParams.result = resultHandler.normalizePendingResult(nativeParams.result);
81
+ }
82
+ const wire = await NativeNavigation.dispatch(sceneId, action, nativeParams);
83
+ return normalizeDispatchResult(wire);
84
+ }
85
+ async dispatch(sceneId, action, params) {
86
+ const chain = this.systemMiddlewares.slice();
87
+ const invoke = (cursor, currentSceneId, currentAction, currentParams) => {
88
+ if (cursor < chain.length) {
89
+ const middleware = chain[cursor];
90
+ return middleware(currentSceneId, currentAction, currentParams, (s, a, p) => invoke(cursor + 1, s, a, p));
91
+ }
92
+ return this.dispatchRaw(currentSceneId, currentAction, currentParams);
93
+ };
94
+ return invoke(0, sceneId, action, params);
95
+ }
96
+ async intercept(action, params) {
97
+ for (const interceptor of this.interceptors.slice()) {
98
+ const result = interceptor(action, params);
99
+ const intercepted = result instanceof Promise ? await result : result;
100
+ if (intercepted) {
101
+ return true;
102
+ }
28
103
  }
29
- return Promise.resolve(result);
104
+ return false;
105
+ }
106
+ addInterceptor(interceptor) {
107
+ this.interceptors.push(interceptor);
108
+ }
109
+ removeInterceptor(interceptor) {
110
+ this.interceptors = this.interceptors.filter(item => item !== interceptor);
30
111
  }
31
- setInterceptor(interceptor) {
32
- this.interceptor = interceptor;
112
+ clearInterceptors() {
113
+ this.interceptors = [];
33
114
  }
34
115
  }
116
+ export const dispatchHandler = new DispatchCommandHandler();
@@ -1,9 +1,10 @@
1
- import { BuildInLayout, Layout } from '../Route';
1
+ import type { BuildInLayout, DispatchResult, Layout, SetRootOptions } from '../Route';
2
+ export type RootLayoutListener = () => void;
2
3
  export default class LayoutCommandHandler {
3
- private willSetRoot;
4
- private didSetRoot;
5
- constructor();
6
- handleRootLayoutChange(): void;
7
- setRoot(layout: BuildInLayout | Layout, sticky?: boolean): Promise<boolean>;
8
- setRootLayoutUpdateListener(willSetRoot?: () => void, didSetRoot?: () => void): void;
4
+ private listeners;
5
+ handleRootLayoutChange(onWillSetRoot?: () => void): void;
6
+ setRoot(layout: BuildInLayout | Layout, options?: SetRootOptions): Promise<DispatchResult>;
7
+ addRootLayoutUpdateListener(willSetRoot?: RootLayoutListener, didSetRoot?: RootLayoutListener): () => void;
8
+ clearListeners(): void;
9
9
  }
10
+ export declare const layoutHandler: LayoutCommandHandler;
@@ -1,26 +1,35 @@
1
1
  import NativeEvent from '../NativeEvent';
2
2
  import NativeNavigation from '../NativeNavigation';
3
+ import { normalizeDispatchResult } from './DispatchCommandHandler';
3
4
  export default class LayoutCommandHandler {
4
- willSetRoot = () => { };
5
- didSetRoot = () => { };
6
- constructor() { }
7
- handleRootLayoutChange() {
5
+ listeners = [];
6
+ handleRootLayoutChange(onWillSetRoot) {
8
7
  NativeEvent.willSetRoot(() => {
9
- this.willSetRoot();
8
+ onWillSetRoot?.();
9
+ for (const listener of this.listeners.slice()) {
10
+ listener.willSetRoot?.();
11
+ }
10
12
  });
11
13
  NativeEvent.didSetRoot(() => {
12
- this.didSetRoot();
14
+ for (const listener of this.listeners.slice()) {
15
+ listener.didSetRoot?.();
16
+ }
13
17
  });
14
18
  }
15
- setRoot(layout, sticky = false) {
16
- return new Promise(resolve => {
17
- NativeNavigation.setRoot(layout, sticky, (_, success) => {
18
- resolve(success);
19
- });
20
- });
19
+ async setRoot(layout, options) {
20
+ const sticky = Boolean(options?.stickyAndroid);
21
+ const wire = await NativeNavigation.setRoot(layout, sticky);
22
+ return normalizeDispatchResult(wire);
23
+ }
24
+ addRootLayoutUpdateListener(willSetRoot, didSetRoot) {
25
+ const record = { willSetRoot, didSetRoot };
26
+ this.listeners.push(record);
27
+ return () => {
28
+ this.listeners = this.listeners.filter(item => item !== record);
29
+ };
21
30
  }
22
- setRootLayoutUpdateListener(willSetRoot = () => { }, didSetRoot = () => { }) {
23
- this.willSetRoot = willSetRoot;
24
- this.didSetRoot = didSetRoot;
31
+ clearListeners() {
32
+ this.listeners = [];
25
33
  }
26
34
  }
35
+ export const layoutHandler = new LayoutCommandHandler();
@@ -0,0 +1,17 @@
1
+ import type { BeforeRemoveListener, BeforeRemoveOptions, DispatchResult, RouteGraph } from '../Route';
2
+ import type { DispatchParams } from './DispatchCommandHandler';
3
+ export declare function collectRemovedStackScenes(layers: RouteGraph[], sceneId: string, action: 'popTo' | 'popToRoot', targetModuleOrSceneId?: string, inclusive?: boolean): string[];
4
+ export default class LeaveGuardHandler {
5
+ private listeners;
6
+ private nativeEnabledScenes;
7
+ private rootGeneration;
8
+ private dispatchFn?;
9
+ setDispatcher(dispatcher: (sceneId: string, action: string, params: DispatchParams) => Promise<DispatchResult>): void;
10
+ handleNativeBeforeRemoveRequests(): void;
11
+ private syncNativeEnabled;
12
+ addListener(sceneId: string, listener: BeforeRemoveListener, options?: BeforeRemoveOptions): () => void;
13
+ middleware: (sceneId: string, action: string, params: DispatchParams, next: (sceneId: string, action: string, params: DispatchParams) => Promise<DispatchResult>) => Promise<DispatchResult>;
14
+ onSceneDestroyed(sceneId: string): void;
15
+ onRootReset(): void;
16
+ }
17
+ export declare const leaveGuardHandler: LeaveGuardHandler;
@@ -0,0 +1,350 @@
1
+ import NativeEvent from '../NativeEvent';
2
+ import NativeNavigation from '../NativeNavigation';
3
+ import { containsScene, findSceneInGraph } from '../routeGraph';
4
+ import { lifecycleHandler } from './LifecycleEventHandler';
5
+ const GUARDED_ACTIONS = new Set([
6
+ 'pop',
7
+ 'popTo',
8
+ 'popToRoot',
9
+ 'dismiss',
10
+ 'hideModal',
11
+ 'close',
12
+ 'redirectTo',
13
+ ]);
14
+ const BYPASS_PROP = '__hbdBypassBeforeRemove';
15
+ const APPROVED_SCENES_PROP = '__hbdApprovedBeforeRemoveScenes';
16
+ function collectScreenIds(graph, out) {
17
+ if (graph.layout === 'screen') {
18
+ out.push(graph.sceneId);
19
+ return;
20
+ }
21
+ if (!graph.children) {
22
+ return;
23
+ }
24
+ for (let i = graph.children.length - 1; i >= 0; i--) {
25
+ collectScreenIds(graph.children[i], out);
26
+ }
27
+ }
28
+ function findContainingStack(graph, sceneId) {
29
+ if (graph.layout === 'stack' && Array.isArray(graph.children)) {
30
+ for (const child of graph.children) {
31
+ const nested = findContainingStack(child, sceneId);
32
+ if (nested) {
33
+ return nested;
34
+ }
35
+ }
36
+ if (graph.children.some(child => containsScene(child, sceneId))) {
37
+ return graph;
38
+ }
39
+ }
40
+ if (!graph.children) {
41
+ return null;
42
+ }
43
+ for (const child of graph.children) {
44
+ const found = findContainingStack(child, sceneId);
45
+ if (found) {
46
+ return found;
47
+ }
48
+ }
49
+ return null;
50
+ }
51
+ export function collectRemovedStackScenes(layers, sceneId, action, targetModuleOrSceneId, inclusive = false) {
52
+ let stack = null;
53
+ for (let i = layers.length - 1; i >= 0; i--) {
54
+ stack = findContainingStack(layers[i], sceneId);
55
+ if (stack) {
56
+ break;
57
+ }
58
+ }
59
+ if (!stack || !Array.isArray(stack.children) || stack.children.length <= 1) {
60
+ return [sceneId];
61
+ }
62
+ const fromIndex = stack.children.findIndex(child => containsScene(child, sceneId));
63
+ if (fromIndex <= 0) {
64
+ return [sceneId];
65
+ }
66
+ let stopIndex = 0;
67
+ if (action === 'popTo') {
68
+ if (!targetModuleOrSceneId) {
69
+ return [sceneId];
70
+ }
71
+ const targetIndex = stack.children.findIndex(child => child.sceneId === targetModuleOrSceneId ||
72
+ containsScene(child, targetModuleOrSceneId) ||
73
+ Boolean(findSceneInGraph(child, node => node.moduleName === targetModuleOrSceneId)));
74
+ if (targetIndex < 0 || targetIndex >= fromIndex) {
75
+ return [sceneId];
76
+ }
77
+ // Match native iOS/Android behavior: inclusive && targetIndex > 0 pops targetIndex itself.
78
+ stopIndex = inclusive && targetIndex > 0 ? targetIndex - 1 : targetIndex;
79
+ }
80
+ const removed = [];
81
+ for (let i = fromIndex; i > stopIndex; i--) {
82
+ collectScreenIds(stack.children[i], removed);
83
+ }
84
+ return removed.length > 0 ? removed : [sceneId];
85
+ }
86
+ function collectPresentedAboveScenes(layers, sceneId) {
87
+ if (!Array.isArray(layers) || layers.length <= 1) {
88
+ return [];
89
+ }
90
+ const index = layers.findIndex(layer => containsScene(layer, sceneId));
91
+ if (index < 0 || index >= layers.length - 1) {
92
+ return [];
93
+ }
94
+ const removed = [];
95
+ for (let i = layers.length - 1; i > index; i--) {
96
+ collectScreenIds(layers[i], removed);
97
+ }
98
+ return removed;
99
+ }
100
+ function matchesAction(entry, action, sceneId) {
101
+ if (!entry.actions) {
102
+ return true;
103
+ }
104
+ if (entry.actions.has(action)) {
105
+ return true;
106
+ }
107
+ if (action === 'close' && sceneId) {
108
+ const mode = lifecycleHandler.getSceneInfo(sceneId)?.mode;
109
+ if (mode === 'modal' && entry.actions.has('hideModal')) {
110
+ return true;
111
+ }
112
+ if (mode === 'present' && entry.actions.has('dismiss')) {
113
+ return true;
114
+ }
115
+ if (mode === 'normal' && entry.actions.has('pop')) {
116
+ return true;
117
+ }
118
+ }
119
+ return false;
120
+ }
121
+ function runGuardedChain(sceneId, action, matching, executeFinalProceed) {
122
+ let finalPromise = null;
123
+ const runFinal = () => {
124
+ if (!finalPromise) {
125
+ finalPromise = executeFinalProceed();
126
+ }
127
+ return finalPromise;
128
+ };
129
+ const runAt = async (index) => {
130
+ if (index >= matching.length) {
131
+ return runFinal();
132
+ }
133
+ let stepProceedPromise = null;
134
+ const event = {
135
+ sceneId,
136
+ action,
137
+ proceed: () => {
138
+ if (!stepProceedPromise) {
139
+ stepProceedPromise = runAt(index + 1);
140
+ }
141
+ return stepProceedPromise;
142
+ },
143
+ };
144
+ await matching[index].listener(event);
145
+ if (stepProceedPromise) {
146
+ return await stepProceedPromise;
147
+ }
148
+ return {
149
+ ok: false,
150
+ code: 'intercepted',
151
+ sceneId,
152
+ message: 'Navigation prevented by beforeRemove guard.',
153
+ };
154
+ };
155
+ return runAt(0);
156
+ }
157
+ export default class LeaveGuardHandler {
158
+ listeners = new Map();
159
+ nativeEnabledScenes = new Set();
160
+ rootGeneration = 0;
161
+ dispatchFn;
162
+ setDispatcher(dispatcher) {
163
+ this.dispatchFn = dispatcher;
164
+ }
165
+ handleNativeBeforeRemoveRequests() {
166
+ NativeEvent.onBeforeRemoveRequest(({ sceneId, action }) => {
167
+ const generation = this.rootGeneration;
168
+ const leaveAction = (action || 'back');
169
+ const proceedClose = async () => {
170
+ if (generation !== this.rootGeneration ||
171
+ lifecycleHandler.getSceneState(sceneId) === 'destroyed') {
172
+ return {
173
+ ok: false,
174
+ code: 'superseded',
175
+ message: 'Scene or root changed before proceed.',
176
+ };
177
+ }
178
+ if (!this.dispatchFn) {
179
+ return { ok: false, code: 'not_ready' };
180
+ }
181
+ return this.dispatchFn(sceneId, 'close', {
182
+ target: 'self',
183
+ [BYPASS_PROP]: true,
184
+ });
185
+ };
186
+ const set = this.listeners.get(sceneId);
187
+ const matching = set
188
+ ? Array.from(set).filter(entry => matchesAction(entry, leaveAction))
189
+ : [];
190
+ if (matching.length === 0) {
191
+ // 异步标志位尚未同步到原生时若刚好触发侧滑/返回键,重新同步关掉原生标志并直接放行退场,避免吞掉返回操作。
192
+ this.syncNativeEnabled(sceneId);
193
+ if (!this.nativeEnabledScenes.has(sceneId)) {
194
+ NativeNavigation.setBeforeRemoveEnabled(sceneId, false);
195
+ }
196
+ void proceedClose();
197
+ return;
198
+ }
199
+ runGuardedChain(sceneId, leaveAction, matching, proceedClose);
200
+ });
201
+ }
202
+ syncNativeEnabled(sceneId) {
203
+ const set = this.listeners.get(sceneId);
204
+ const shouldEnable = Boolean(set) &&
205
+ Array.from(set).some(entry => matchesAction(entry, 'back'));
206
+ const isEnabled = this.nativeEnabledScenes.has(sceneId);
207
+ if (shouldEnable === isEnabled) {
208
+ return;
209
+ }
210
+ if (shouldEnable) {
211
+ this.nativeEnabledScenes.add(sceneId);
212
+ NativeNavigation.setBeforeRemoveEnabled(sceneId, true);
213
+ }
214
+ else {
215
+ this.nativeEnabledScenes.delete(sceneId);
216
+ if (lifecycleHandler.getSceneState(sceneId) !== 'destroyed') {
217
+ NativeNavigation.setBeforeRemoveEnabled(sceneId, false);
218
+ }
219
+ }
220
+ }
221
+ addListener(sceneId, listener, options) {
222
+ let set = this.listeners.get(sceneId);
223
+ if (!set) {
224
+ set = new Set();
225
+ this.listeners.set(sceneId, set);
226
+ }
227
+ const entry = {
228
+ listener,
229
+ actions: options?.actions ? new Set(options.actions) : undefined,
230
+ };
231
+ set.add(entry);
232
+ this.syncNativeEnabled(sceneId);
233
+ return () => {
234
+ const current = this.listeners.get(sceneId);
235
+ if (!current) {
236
+ return;
237
+ }
238
+ current.delete(entry);
239
+ if (current.size === 0) {
240
+ this.listeners.delete(sceneId);
241
+ }
242
+ this.syncNativeEnabled(sceneId);
243
+ };
244
+ }
245
+ middleware = async (sceneId, action, params, next) => {
246
+ if (params[BYPASS_PROP] || params.bypassBeforeRemove) {
247
+ const cleanParams = { ...params };
248
+ delete cleanParams[BYPASS_PROP];
249
+ delete cleanParams.bypassBeforeRemove;
250
+ return next(sceneId, action, cleanParams);
251
+ }
252
+ if (!GUARDED_ACTIONS.has(action) || this.listeners.size === 0) {
253
+ return next(sceneId, action, params);
254
+ }
255
+ const leaveAction = action;
256
+ const approved = params[APPROVED_SCENES_PROP] instanceof Set ? params[APPROVED_SCENES_PROP] : new Set();
257
+ let candidateSceneIds = [sceneId];
258
+ if (leaveAction === 'popTo' || leaveAction === 'popToRoot') {
259
+ try {
260
+ const layers = await NativeNavigation.routeGraph();
261
+ if (Array.isArray(layers) && layers.length > 0) {
262
+ candidateSceneIds = collectRemovedStackScenes(layers, sceneId, leaveAction, typeof params.moduleName === 'string'
263
+ ? params.moduleName
264
+ : typeof params.to === 'string'
265
+ ? params.to
266
+ : undefined, Boolean(params.inclusive));
267
+ }
268
+ }
269
+ catch {
270
+ candidateSceneIds = [sceneId];
271
+ }
272
+ }
273
+ else if ((leaveAction === 'dismiss' || leaveAction === 'hideModal') &&
274
+ params.target !== 'self') {
275
+ try {
276
+ const layers = await NativeNavigation.routeGraph();
277
+ const presentedAbove = collectPresentedAboveScenes(layers, sceneId);
278
+ if (presentedAbove.length > 0) {
279
+ candidateSceneIds = presentedAbove;
280
+ }
281
+ else if (params.target === 'presented') {
282
+ candidateSceneIds = [];
283
+ }
284
+ }
285
+ catch {
286
+ candidateSceneIds = params.target === 'presented' ? [] : [sceneId];
287
+ }
288
+ }
289
+ let guardedSceneId = null;
290
+ let matching = [];
291
+ for (const candidateId of candidateSceneIds) {
292
+ if (approved.has(candidateId)) {
293
+ continue;
294
+ }
295
+ const set = this.listeners.get(candidateId);
296
+ if (!set || set.size === 0) {
297
+ continue;
298
+ }
299
+ const hits = Array.from(set).filter(entry => matchesAction(entry, leaveAction, candidateId));
300
+ if (hits.length > 0) {
301
+ guardedSceneId = candidateId;
302
+ matching = hits;
303
+ break;
304
+ }
305
+ }
306
+ if (!guardedSceneId || matching.length === 0) {
307
+ const cleanParams = { ...params };
308
+ delete cleanParams[APPROVED_SCENES_PROP];
309
+ return next(sceneId, action, cleanParams);
310
+ }
311
+ const isMultiScene = candidateSceneIds.length > 1;
312
+ const generation = this.rootGeneration;
313
+ const targetGuardedSceneId = guardedSceneId;
314
+ return runGuardedChain(targetGuardedSceneId, leaveAction, matching, async () => {
315
+ if (generation !== this.rootGeneration ||
316
+ lifecycleHandler.getSceneState(targetGuardedSceneId) === 'destroyed') {
317
+ return {
318
+ ok: false,
319
+ code: 'superseded',
320
+ message: 'Scene or root changed before proceed.',
321
+ };
322
+ }
323
+ if (!this.dispatchFn) {
324
+ return { ok: false, code: 'not_ready' };
325
+ }
326
+ if (isMultiScene) {
327
+ const nextApproved = new Set(approved);
328
+ nextApproved.add(targetGuardedSceneId);
329
+ return this.dispatchFn(sceneId, action, {
330
+ ...params,
331
+ [APPROVED_SCENES_PROP]: nextApproved,
332
+ });
333
+ }
334
+ return this.dispatchFn(sceneId, action, {
335
+ ...params,
336
+ [BYPASS_PROP]: true,
337
+ });
338
+ });
339
+ };
340
+ onSceneDestroyed(sceneId) {
341
+ this.listeners.delete(sceneId);
342
+ this.nativeEnabledScenes.delete(sceneId);
343
+ }
344
+ onRootReset() {
345
+ this.rootGeneration += 1;
346
+ this.listeners.clear();
347
+ this.nativeEnabledScenes.clear();
348
+ }
349
+ }
350
+ export const leaveGuardHandler = new LeaveGuardHandler();
@@ -0,0 +1,30 @@
1
+ import type { SceneInfo, SceneLifecycleState, Visibility } from '../Route';
2
+ export type SceneLifecycleListener = (state: SceneLifecycleState, prevState: SceneLifecycleState) => void;
3
+ export type GlobalSceneLifecycleListener = (sceneId: string, state: SceneLifecycleState, prevState: SceneLifecycleState, info?: SceneInfo) => void;
4
+ export type VisibilityEventListener = (visibility: Visibility) => void;
5
+ export type GlobalVisibilityEventListener = (sceneId: string, visibility: Visibility, info?: SceneInfo) => void;
6
+ export default class LifecycleEventHandler {
7
+ private sceneStates;
8
+ private sceneInfos;
9
+ private destroyedScenes;
10
+ private lifecycleListeners;
11
+ private globalLifecycleListeners;
12
+ private visibilityListeners;
13
+ private globalVisibilityListeners;
14
+ handleSceneLifecycle(onSceneDestroyed?: (sceneId: string) => void): void;
15
+ getSceneInfo(sceneId: string): SceneInfo | undefined;
16
+ onRootReset(): void;
17
+ resetForTesting(): void;
18
+ private markSceneDestroyed;
19
+ getSceneState(sceneId: string): SceneLifecycleState;
20
+ cleanupScene(sceneId: string): void;
21
+ addLifecycleListener(sceneId: string, listener: SceneLifecycleListener): void;
22
+ removeLifecycleListener(sceneId: string, listener: SceneLifecycleListener): void;
23
+ addGlobalLifecycleListener(listener: GlobalSceneLifecycleListener): void;
24
+ removeGlobalLifecycleListener(listener: GlobalSceneLifecycleListener): void;
25
+ addGlobalVisibilityEventListener(listener: GlobalVisibilityEventListener): void;
26
+ removeGlobalVisibilityEventListener(listener: GlobalVisibilityEventListener): void;
27
+ addVisibilityEventListener(sceneId: string, listener: VisibilityEventListener): void;
28
+ removeVisibilityEventListener(sceneId: string, listener: VisibilityEventListener): void;
29
+ }
30
+ export declare const lifecycleHandler: LifecycleEventHandler;