hybrid-navigation 5.2.0 → 5.4.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 (103) 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 +72 -136
  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 -22
  17. package/dist/Navigation.js +33 -175
  18. package/dist/NavigationComponentWrap.d.ts +3 -3
  19. package/dist/Navigator.d.ts +4 -5
  20. package/dist/Navigator.js +13 -37
  21. package/dist/Options.d.ts +3 -2
  22. package/dist/Route.d.ts +10 -35
  23. package/dist/handler/DispatchCommandHandler.d.ts +3 -18
  24. package/dist/handler/DispatchCommandHandler.js +17 -64
  25. package/dist/handler/LayoutCommandHandler.d.ts +2 -3
  26. package/dist/handler/LayoutCommandHandler.js +0 -3
  27. package/dist/handler/LeaveGuardHandler.d.ts +3 -3
  28. package/dist/handler/LeaveGuardHandler.js +57 -32
  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 +0 -3
  36. package/dist/hooks.js +21 -46
  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/router/stack.d.ts +1 -1
  42. package/dist/sceneCoverage.d.ts +2 -0
  43. package/dist/sceneCoverage.js +25 -0
  44. package/dist/store.d.ts +0 -2
  45. package/dist/store.js +0 -4
  46. package/ios/HBDGlobalStyle.h +0 -1
  47. package/ios/HBDGlobalStyle.mm +0 -7
  48. package/ios/HybridNavigation.h +0 -1
  49. package/ios/Navigator/HBDScreenNavigator.mm +89 -237
  50. package/ios/Navigator/HBDStackNavigator.mm +3 -3
  51. package/ios/Navigator/HBDTabNavigator.mm +15 -30
  52. package/ios/React/HBDNativeNavigation.mm +4 -31
  53. package/ios/React/HBDReactBridgeManager.h +0 -5
  54. package/ios/React/HBDReactBridgeManager.mm +0 -17
  55. package/ios/ViewController/HBDReactViewController.h +3 -1
  56. package/ios/ViewController/HBDReactViewController.mm +42 -9
  57. package/ios/ViewController/HBDTabBarController.h +2 -11
  58. package/ios/ViewController/HBDTabBarController.mm +14 -170
  59. package/ios/ViewController/HBDViewController.h +0 -1
  60. package/ios/ViewController/HBDViewController.mm +9 -57
  61. package/ios/ViewController/UIViewController+HBD.h +2 -0
  62. package/ios/ViewController/UIViewController+HBD.mm +5 -0
  63. package/package.json +1 -3
  64. package/src/NativeEvent.ts +1 -12
  65. package/src/NativeNavigation.ts +8 -7
  66. package/src/Navigation.ts +50 -281
  67. package/src/NavigationComponentWrap.tsx +3 -11
  68. package/src/Navigator.ts +15 -46
  69. package/src/Options.ts +3 -3
  70. package/src/Route.ts +13 -56
  71. package/src/handler/DispatchCommandHandler.ts +29 -111
  72. package/src/handler/LayoutCommandHandler.ts +2 -6
  73. package/src/handler/LeaveGuardHandler.ts +65 -32
  74. package/src/handler/LifecycleEventHandler.ts +22 -122
  75. package/src/handler/ResultEventHandler.ts +0 -6
  76. package/src/handler/SceneExitHandler.ts +86 -0
  77. package/src/hooks.ts +18 -53
  78. package/src/index.ts +0 -14
  79. package/src/router/index.ts +45 -12
  80. package/src/router/stack.ts +1 -1
  81. package/src/sceneCoverage.ts +30 -0
  82. package/src/store.ts +0 -5
  83. package/dist/defineRoutes.d.ts +0 -19
  84. package/dist/defineRoutes.js +0 -6
  85. package/dist/handler/PresentationCoordinator.d.ts +0 -39
  86. package/dist/handler/PresentationCoordinator.js +0 -673
  87. package/dist/handler/TabEventHandler.d.ts +0 -8
  88. package/dist/handler/TabEventHandler.js +0 -25
  89. package/dist/reveal.d.ts +0 -46
  90. package/dist/reveal.js +0 -431
  91. package/dist/routeGraph.d.ts +0 -12
  92. package/dist/routeGraph.js +0 -68
  93. package/dist/testing.d.ts +0 -82
  94. package/dist/testing.js +0 -253
  95. package/ios/ViewController/HBDTabBarRenderer.h +0 -28
  96. package/src/defineRoutes.ts +0 -46
  97. package/src/handler/PresentationCoordinator.ts +0 -806
  98. package/src/handler/TabEventHandler.ts +0 -31
  99. package/src/reveal.ts +0 -565
  100. package/src/routeGraph.ts +0 -87
  101. package/src/testing.ts +0 -402
  102. package/testing.d.ts +0 -1
  103. package/testing.js +0 -3
@@ -1,4 +1,4 @@
1
- import type { LayoutMode, SceneInfo, SceneLifecycleState, Visibility } from '../Route';
1
+ import type { SceneLifecycleState } from '../Route';
2
2
  import NativeEvent from '../NativeEvent';
3
3
 
4
4
  export type SceneLifecycleListener = (
@@ -9,13 +9,6 @@ export type GlobalSceneLifecycleListener = (
9
9
  sceneId: string,
10
10
  state: SceneLifecycleState,
11
11
  prevState: SceneLifecycleState,
12
- info?: SceneInfo,
13
- ) => void;
14
- export type VisibilityEventListener = (visibility: Visibility) => void;
15
- export type GlobalVisibilityEventListener = (
16
- sceneId: string,
17
- visibility: Visibility,
18
- info?: SceneInfo,
19
12
  ) => void;
20
13
 
21
14
  const VALID_STATES = new Set<SceneLifecycleState>([
@@ -25,8 +18,6 @@ const VALID_STATES = new Set<SceneLifecycleState>([
25
18
  'destroyed',
26
19
  ]);
27
20
 
28
- const VALID_MODES = new Set<LayoutMode>(['modal', 'present', 'normal']);
29
-
30
21
  function normalizeLifecycleState(raw: string): SceneLifecycleState {
31
22
  if (VALID_STATES.has(raw as SceneLifecycleState)) {
32
23
  return raw as SceneLifecycleState;
@@ -34,100 +25,39 @@ function normalizeLifecycleState(raw: string): SceneLifecycleState {
34
25
  return 'created';
35
26
  }
36
27
 
37
- function normalizeLayoutMode(raw: string | undefined, fallback: LayoutMode = 'normal'): LayoutMode {
38
- if (raw && VALID_MODES.has(raw as LayoutMode)) {
39
- return raw as LayoutMode;
40
- }
41
- return fallback;
42
- }
43
-
44
28
  const MAX_DESTROYED_SCENES = 2000;
45
29
 
46
30
  export default class LifecycleEventHandler {
47
31
  private sceneStates = new Map<string, SceneLifecycleState>();
48
- private sceneInfos = new Map<string, SceneInfo>();
49
32
  private destroyedScenes = new Set<string>();
50
33
  private lifecycleListeners: Record<string, SceneLifecycleListener[]> = {};
51
34
  private globalLifecycleListeners: GlobalSceneLifecycleListener[] = [];
52
- private visibilityListeners: Record<string, VisibilityEventListener[]> = {};
53
- private globalVisibilityListeners: GlobalVisibilityEventListener[] = [];
54
35
 
55
36
  handleSceneLifecycle(onSceneDestroyed?: (sceneId: string) => void) {
56
- NativeEvent.onSceneStateChange(
57
- ({ sceneId, state, prevState, moduleName, mode, presentingId }) => {
58
- const next = normalizeLifecycleState(state);
59
- const prev = normalizeLifecycleState(prevState);
60
-
61
- if (next !== 'destroyed') {
62
- this.destroyedScenes.delete(sceneId);
63
- }
64
- this.sceneStates.set(sceneId, next);
65
-
66
- const prevInfo = this.sceneInfos.get(sceneId);
67
- const info: SceneInfo = {
68
- sceneId,
69
- moduleName: moduleName ?? prevInfo?.moduleName,
70
- mode: normalizeLayoutMode(mode, prevInfo?.mode ?? 'normal'),
71
- presentingId:
72
- presentingId !== undefined ? presentingId : (prevInfo?.presentingId ?? null),
73
- state: next,
74
- };
75
- this.sceneInfos.set(sceneId, info);
76
-
77
- const visibilityChange: Visibility | null =
78
- next === 'visible'
79
- ? 'visible'
80
- : prev === 'visible' && (next === 'invisible' || next === 'destroyed')
81
- ? 'invisible'
82
- : null;
83
-
84
- if (visibilityChange) {
85
- this.globalVisibilityListeners
86
- .slice()
87
- .forEach(listener => listener(sceneId, visibilityChange, info));
88
- }
89
-
90
- this.globalLifecycleListeners
91
- .slice()
92
- .forEach(listener => listener(sceneId, next, prev, info));
93
-
94
- const sceneListeners = this.lifecycleListeners[sceneId];
95
- if (sceneListeners) {
96
- sceneListeners.slice().forEach(listener => listener(next, prev));
97
- }
98
-
99
- if (visibilityChange) {
100
- const visListeners = this.visibilityListeners[sceneId];
101
- if (visListeners) {
102
- visListeners.slice().forEach(listener => listener(visibilityChange));
103
- }
104
- }
105
-
106
- if (next === 'destroyed') {
107
- this.markSceneDestroyed(sceneId);
108
- onSceneDestroyed?.(sceneId);
109
- this.cleanupScene(sceneId);
110
- }
111
- },
112
- );
113
- }
37
+ NativeEvent.onSceneStateChange(({ sceneId, state, prevState }) => {
38
+ const next = normalizeLifecycleState(state);
39
+ const prev = normalizeLifecycleState(prevState);
114
40
 
115
- getSceneInfo(sceneId: string): SceneInfo | undefined {
116
- return this.sceneInfos.get(sceneId);
117
- }
41
+ if (next !== 'destroyed') {
42
+ this.destroyedScenes.delete(sceneId);
43
+ }
44
+ this.sceneStates.set(sceneId, next);
118
45
 
119
- onRootReset() {
120
- // Lifecycle state maps are updated by native destroyed events.
121
- }
46
+ this.globalLifecycleListeners
47
+ .slice()
48
+ .forEach(listener => listener(sceneId, next, prev));
49
+
50
+ const sceneListeners = this.lifecycleListeners[sceneId];
51
+ if (sceneListeners) {
52
+ sceneListeners.slice().forEach(listener => listener(next, prev));
53
+ }
122
54
 
123
- resetForTesting() {
124
- this.sceneStates.clear();
125
- this.sceneInfos.clear();
126
- this.destroyedScenes.clear();
127
- this.lifecycleListeners = {};
128
- this.globalLifecycleListeners = [];
129
- this.visibilityListeners = {};
130
- this.globalVisibilityListeners = [];
55
+ if (next === 'destroyed') {
56
+ this.markSceneDestroyed(sceneId);
57
+ onSceneDestroyed?.(sceneId);
58
+ this.cleanupScene(sceneId);
59
+ }
60
+ });
131
61
  }
132
62
 
133
63
  private markSceneDestroyed(sceneId: string) {
@@ -154,9 +84,7 @@ export default class LifecycleEventHandler {
154
84
 
155
85
  cleanupScene(sceneId: string) {
156
86
  this.sceneStates.delete(sceneId);
157
- this.sceneInfos.delete(sceneId);
158
87
  delete this.lifecycleListeners[sceneId];
159
- delete this.visibilityListeners[sceneId];
160
88
  }
161
89
 
162
90
  addLifecycleListener(sceneId: string, listener: SceneLifecycleListener) {
@@ -186,34 +114,6 @@ export default class LifecycleEventHandler {
186
114
  removeGlobalLifecycleListener(listener: GlobalSceneLifecycleListener) {
187
115
  this.globalLifecycleListeners = this.globalLifecycleListeners.filter(l => l !== listener);
188
116
  }
189
-
190
- addGlobalVisibilityEventListener(listener: GlobalVisibilityEventListener) {
191
- this.globalVisibilityListeners.push(listener);
192
- }
193
-
194
- removeGlobalVisibilityEventListener(listener: GlobalVisibilityEventListener) {
195
- this.globalVisibilityListeners = this.globalVisibilityListeners.filter(l => l !== listener);
196
- }
197
-
198
- addVisibilityEventListener(sceneId: string, listener: VisibilityEventListener) {
199
- if (!this.visibilityListeners[sceneId]) {
200
- this.visibilityListeners[sceneId] = [];
201
- }
202
- this.visibilityListeners[sceneId].push(listener);
203
- }
204
-
205
- removeVisibilityEventListener(sceneId: string, listener: VisibilityEventListener) {
206
- const list = this.visibilityListeners[sceneId];
207
- if (!list) {
208
- return;
209
- }
210
- const next = list.filter(l => l !== listener);
211
- if (next.length > 0) {
212
- this.visibilityListeners[sceneId] = next;
213
- } else {
214
- delete this.visibilityListeners[sceneId];
215
- }
216
- }
217
117
  }
218
118
 
219
119
  export const lifecycleHandler = new LifecycleEventHandler();
@@ -4,7 +4,6 @@ import NativeNavigation from '../NativeNavigation';
4
4
 
5
5
  export const { RESULT_CANCEL, RESULT_OK, RESULT_BLOCK } = NativeNavigation.getConstants();
6
6
 
7
- export type ResultType = unknown;
8
7
  export type ResultEventListener<T = unknown> = (result: NavigationResult<T>) => void;
9
8
  export type PreparedResult<T = unknown> = {
10
9
  promise: Promise<NavigationResult<T>>;
@@ -140,11 +139,6 @@ export default class ResultEventHandler {
140
139
  '`resultCode` can only be `RESULT_OK`, `RESULT_CANCEL` or an integer greater than 0.',
141
140
  );
142
141
  }
143
- if (__DEV__ && code === RESULT_CANCEL && data !== null && data !== undefined) {
144
- console.warn(
145
- '[hybrid-navigation] `RESULT_CANCEL` ignores `data` because `RESULT_CANCEL` always resolves as `{ status: "cancel" }`. Pass `null` or use `RESULT_OK` to return data.',
146
- );
147
- }
148
142
  if (data !== null && (typeof data !== 'object' || Array.isArray(data))) {
149
143
  console.warn('`setResult` data must be a plain object or null.');
150
144
  return { code, data: null };
@@ -0,0 +1,86 @@
1
+ import NativeNavigation from '../NativeNavigation';
2
+ import { isSceneCoveredInLayers } from '../sceneCoverage';
3
+ import type {
4
+ DispatchResult,
5
+ SceneExitHandler,
6
+ SceneExitRestoreReason,
7
+ } from '../Route';
8
+
9
+ const handlers = new Map<string, SceneExitHandler[]>();
10
+ const pending = new Map<string, Promise<DispatchResult>>();
11
+
12
+ export function registerExitHandler(sceneId: string, handler: SceneExitHandler) {
13
+ const stack = handlers.get(sceneId) ?? [];
14
+ stack.push(handler);
15
+ handlers.set(sceneId, stack);
16
+ return () => {
17
+ const index = stack.indexOf(handler);
18
+ if (index >= 0) stack.splice(index, 1);
19
+ if (!stack.length && handlers.get(sceneId) === stack) handlers.delete(sceneId);
20
+ };
21
+ }
22
+
23
+ async function playExit(handler: SceneExitHandler) {
24
+ const animation = handler.onExit();
25
+ const timeout = handler.exitTimeoutMs ?? 1200;
26
+ if (!animation || timeout <= 0) return animation;
27
+
28
+ let timer: ReturnType<typeof setTimeout> | undefined;
29
+ try {
30
+ await Promise.race([
31
+ animation,
32
+ new Promise<void>((_, reject) => {
33
+ timer = setTimeout(
34
+ () => reject(new Error('Scene exit animation timed out.')),
35
+ timeout,
36
+ );
37
+ }),
38
+ ]);
39
+ } finally {
40
+ clearTimeout(timer);
41
+ }
42
+ }
43
+
44
+ async function exitAndClose(
45
+ sceneId: string,
46
+ handler: SceneExitHandler,
47
+ close: () => Promise<DispatchResult>,
48
+ ): Promise<DispatchResult> {
49
+ const { layers } = await NativeNavigation.navigationSnapshot();
50
+ if (isSceneCoveredInLayers(layers, sceneId, 'close')) return { ok: false, code: 'covered' };
51
+ if (!handlers.get(sceneId)?.includes(handler)) return close();
52
+
53
+ function restore(reason: SceneExitRestoreReason) {
54
+ if (handlers.get(sceneId)?.includes(handler)) handler.onRestore?.(reason);
55
+ }
56
+
57
+ try {
58
+ await playExit(handler);
59
+ } catch (error) {
60
+ restore('exit_error');
61
+ return { ok: false, code: 'invalid_action', message: String(error) };
62
+ }
63
+
64
+ // Native closeSelf rechecks coverage before committing the supplied result.
65
+ let result: DispatchResult;
66
+ try {
67
+ result = await close();
68
+ } catch (error) {
69
+ restore('invalid_action');
70
+ throw error;
71
+ }
72
+ if (!result.ok) restore(result.code);
73
+ return result;
74
+ }
75
+
76
+ export function closeWithExitHandler(sceneId: string, close: () => Promise<DispatchResult>) {
77
+ const existing = pending.get(sceneId);
78
+ if (existing) return existing;
79
+ const stack = handlers.get(sceneId);
80
+ const handler = stack?.[stack.length - 1];
81
+ if (!handler) return close();
82
+
83
+ const request = exitAndClose(sceneId, handler, close).finally(() => pending.delete(sceneId));
84
+ pending.set(sceneId, request);
85
+ return request;
86
+ }
package/src/hooks.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { useContext, useEffect, useEffectEvent, useLayoutEffect, useState } from 'react';
2
+ import { registerExitHandler } from './handler/SceneExitHandler';
2
3
  import Navigation from './Navigation';
3
4
  import { Navigator, NavigationContext } from './Navigator';
4
- import { presentationCoordinator } from './handler/PresentationCoordinator';
5
5
  import type {
6
6
  BeforeRemoveListener,
7
7
  BeforeRemoveOptions,
@@ -42,35 +42,6 @@ export function useSceneState(): SceneLifecycleState {
42
42
  return state;
43
43
  }
44
44
 
45
- export function useIsVisible(): boolean {
46
- return useSceneState() === 'visible';
47
- }
48
-
49
- export function useSceneCovered(): boolean {
50
- const navigator = useNavigator();
51
- const [covered, setCovered] = useState(() =>
52
- presentationCoordinator.isSceneVisuallyCoveredSync(navigator.sceneId),
53
- );
54
-
55
- useEffect(() => {
56
- setCovered(presentationCoordinator.isSceneVisuallyCoveredSync(navigator.sceneId));
57
- const subscription = Navigation.addSceneCoverageListener(navigator.sceneId, next => {
58
- setCovered(next);
59
- });
60
- return () => {
61
- subscription.remove();
62
- };
63
- }, [navigator.sceneId]);
64
-
65
- return covered;
66
- }
67
-
68
- export function useIsTopmost(): boolean {
69
- const state = useSceneState();
70
- const covered = useSceneCovered();
71
- return state === 'visible' && !covered;
72
- }
73
-
74
45
  export function useSceneExitHandler(handler: SceneExitHandler | null | undefined) {
75
46
  const navigator = useNavigator();
76
47
  const enabled = Boolean(handler);
@@ -84,14 +55,11 @@ export function useSceneExitHandler(handler: SceneExitHandler | null | undefined
84
55
  if (!enabled) {
85
56
  return;
86
57
  }
87
- const subscription = Navigation.registerSceneExitHandler(navigator.sceneId, {
58
+ return registerExitHandler(navigator.sceneId, {
88
59
  onExit: () => onExit(),
89
60
  onRestore: reason => onRestore(reason),
90
61
  ...(exitTimeoutMs !== undefined ? { exitTimeoutMs } : {}),
91
62
  });
92
- return () => {
93
- subscription.remove();
94
- };
95
63
  }, [enabled, exitTimeoutMs, navigator.sceneId]);
96
64
  }
97
65
 
@@ -102,15 +70,13 @@ export function useBeforeRemove(
102
70
  ) {
103
71
  const navigator = useNavigator();
104
72
  const eventCallback = useEffectEvent(onBeforeRemove);
105
- const actionsKey = options?.actions ? options.actions.join(',') : '';
73
+ const actionsKey = options?.actions?.join(',');
106
74
 
107
75
  useEffect(() => {
108
76
  if (!enabled) {
109
77
  return;
110
78
  }
111
- const actions = actionsKey
112
- ? (actionsKey.split(',') as NonNullable<BeforeRemoveOptions['actions']>)
113
- : undefined;
79
+ const actions = actionsKey?.split(',').filter(Boolean) as BeforeRemoveOptions['actions'];
114
80
  const subscription = Navigation.addBeforeRemoveListener(
115
81
  navigator.sceneId,
116
82
  event => eventCallback(event),
@@ -127,25 +93,24 @@ export function useVisibleEffect(callback: VisibleEffectCallback) {
127
93
  const event = useEffectEvent(callback);
128
94
 
129
95
  useEffect(() => {
130
- let destructor: (() => void) | void;
131
- if (navigator.visibility === 'visible') {
132
- destructor = event();
133
- }
134
- const subscription = Navigation.addVisibilityEventListener(navigator.sceneId, v => {
135
- if (v === 'visible') {
136
- destructor = event();
96
+ let active = false;
97
+ let cleanup: ReturnType<VisibleEffectCallback>;
98
+ function update(state: SceneLifecycleState) {
99
+ const visible = state === 'visible';
100
+ if (active === visible) return;
101
+ active = visible;
102
+ if (visible) {
103
+ cleanup = event();
137
104
  } else {
138
- if (typeof destructor === 'function') {
139
- destructor();
140
- }
141
- destructor = undefined;
105
+ if (typeof cleanup === 'function') cleanup();
106
+ cleanup = undefined;
142
107
  }
143
- });
108
+ }
109
+ const subscription = Navigation.addLifecycleListener(navigator.sceneId, update);
110
+ update(navigator.lifecycleState);
144
111
  return () => {
145
112
  subscription.remove();
146
- if (typeof destructor === 'function') {
147
- destructor();
148
- }
113
+ if (typeof cleanup === 'function') cleanup();
149
114
  };
150
115
  }, [navigator]);
151
116
  }
package/src/index.ts CHANGED
@@ -6,14 +6,6 @@ import navigation from './Navigation';
6
6
  */
7
7
  export interface NavigationParamList {}
8
8
 
9
- /**
10
- * Augment this interface via `declare module 'hybrid-navigation'` to configure
11
- * route type checking. Set `allowUnregisteredRoutes: true` during incremental
12
- * migration so declared routes get strict checking while unmigrated routes
13
- * remain accepted as `string & {}`.
14
- */
15
- export interface NavigationTypeConfig {}
16
-
17
9
  /**
18
10
  * Augment this interface via `declare module 'hybrid-navigation'` to register
19
11
  * strongly typed custom container layouts alongside built-in layouts.
@@ -27,16 +19,13 @@ export { RESULT_OK, RESULT_CANCEL, RESULT_BLOCK } from './Navigation';
27
19
  export type {
28
20
  DispatchParams,
29
21
  GlobalSceneLifecycleListener,
30
- GlobalVisibilityEventListener,
31
22
  HOC,
32
23
  InterceptParams,
33
24
  Navigation,
34
25
  NavigationInterceptor,
35
26
  NavigationSubscription,
36
- ResultType,
37
27
  RootLayoutListener,
38
28
  SceneLifecycleListener,
39
- VisibilityEventListener,
40
29
  } from './Navigation';
41
30
  export { withNavigationItem } from './NavigationComponentWrap';
42
31
  export type { NavigationProps } from './NavigationComponentWrap';
@@ -44,9 +33,6 @@ export type { NavigationProps } from './NavigationComponentWrap';
44
33
  export * from './Navigator';
45
34
  export * from './hooks';
46
35
  export * from './router';
47
- export * from './routeGraph';
48
- export * from './reveal';
49
- export * from './defineRoutes';
50
36
  export * from './DeepLink';
51
37
  export * from './Route';
52
38
  export * from './Options';
@@ -15,7 +15,6 @@ import { TabsRouteHandler } from './tabs';
15
15
  import { DrawerRouteHandler } from './drawer';
16
16
 
17
17
  import Navigation from '../Navigation';
18
- import { dismissPresentedLayers } from '../reveal';
19
18
  import type { NavigationSubscription } from '../Navigation';
20
19
  import { getRouteConfigs, getRouteConfigVersion } from '../routeConfigRegistry';
21
20
 
@@ -52,6 +51,10 @@ function queryParams(queryString: string) {
52
51
  }, {});
53
52
  }
54
53
 
54
+ function containsScene(node: RouteGraph, sceneId: string): boolean {
55
+ return node.sceneId === sceneId || !!node.children?.some(child => containsScene(child, sceneId));
56
+ }
57
+
55
58
  class Router {
56
59
  constructor() {
57
60
  this.registerRouteHandler('screen', new ScreenRouteHandler());
@@ -107,14 +110,12 @@ class Router {
107
110
  addInterceptor(interceptor: RouteInterceptor): NavigationSubscription {
108
111
  this.interceptors.push(interceptor);
109
112
  return {
110
- remove: () => this.removeInterceptor(interceptor),
113
+ remove: () => {
114
+ this.interceptors = this.interceptors.filter(item => item !== interceptor);
115
+ },
111
116
  };
112
117
  }
113
118
 
114
- removeInterceptor(interceptor: RouteInterceptor) {
115
- this.interceptors = this.interceptors.filter(item => item !== interceptor);
116
- }
117
-
118
119
  private async intercept(path: string) {
119
120
  for (const interceptor of this.interceptors.slice()) {
120
121
  const result = interceptor(path);
@@ -233,13 +234,38 @@ class Router {
233
234
  };
234
235
  }
235
236
 
236
- const dismissed = await dismissPresentedLayers(Navigation);
237
- if (!dismissed.ok) {
238
- return dismissed;
237
+ let snapshot = await Navigation.navigationSnapshot();
238
+ const generation = snapshot.rootGeneration;
239
+ const dismissed = new Set<string>();
240
+ const maxSteps = Math.max(snapshot.layers.length, 12);
241
+
242
+ while (snapshot.layers.length > 1) {
243
+ const layer = snapshot.layers[snapshot.layers.length - 1];
244
+ const current = snapshot.currentRoute;
245
+ if (!current || !containsScene(layer, current.sceneId)) {
246
+ return { ok: false, code: 'scene_not_found', message: 'Current scene is not in the top layer.' };
247
+ }
248
+ if (dismissed.has(layer.sceneId) || dismissed.size >= maxSteps) {
249
+ return { ok: false, code: 'invalid_action', message: 'Unable to finish dismissing presented layers.' };
250
+ }
251
+ if (layer.mode !== 'modal' && layer.mode !== 'present') {
252
+ return { ok: false, code: 'invalid_action', message: `Unexpected layer mode: ${layer.mode}` };
253
+ }
254
+
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
+ });
261
+ snapshot = await Navigation.navigationSnapshot();
262
+ if (snapshot.rootGeneration !== generation) {
263
+ return { ok: false, code: 'superseded', message: 'Root layout changed during navigation.' };
264
+ }
265
+ if (!result.ok) return result;
239
266
  }
240
- const graphArray = dismissed.layers;
241
267
 
242
- const routeGraph = graphArray[0];
268
+ const routeGraph = snapshot.layers[0];
243
269
  if (!routeGraph) {
244
270
  return {
245
271
  ok: false,
@@ -253,7 +279,14 @@ class Router {
253
279
  return routeResult;
254
280
  }
255
281
 
256
- const currentRoute = await Navigation.currentRoute();
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.' };
288
+ }
289
+ const currentRoute = snapshot.currentRoute;
257
290
  if (!currentRoute) {
258
291
  return {
259
292
  ok: false,
@@ -1,5 +1,5 @@
1
1
  import { ScreenGraph, isScreenGraph, isTargetLocateIn } from './screen';
2
- import type {
2
+ import {
3
3
  DispatchResult,
4
4
  LayoutMode,
5
5
  RouteGraph,
@@ -0,0 +1,30 @@
1
+ import type { RouteGraph } from './Route';
2
+
3
+ export function isSceneCoveredInLayers(
4
+ layers: RouteGraph[],
5
+ sceneId: string,
6
+ purpose: 'visual' | 'close',
7
+ ): boolean {
8
+ // Siblings overlap in presentation layers and stacks. Only visual queries also consider tabs.
9
+ function find(nodes: RouteGraph[], frontIndex?: number): boolean | undefined {
10
+ for (const [index, node] of nodes.entries()) {
11
+ const coveredBySibling = frontIndex !== undefined && index !== frontIndex;
12
+ if (node.sceneId === sceneId) return coveredBySibling;
13
+ if (!node.children?.length) continue;
14
+
15
+ let frontChild: number | undefined;
16
+ if (node.layout === 'stack') {
17
+ frontChild = node.children.length - 1;
18
+ } else if (purpose === 'visual' && node.layout === 'tabs') {
19
+ if ('selectedIndex' in node && typeof node.selectedIndex === 'number') {
20
+ frontChild = node.selectedIndex;
21
+ }
22
+ }
23
+
24
+ const nested = find(node.children, frontChild);
25
+ if (nested !== undefined) return coveredBySibling || nested;
26
+ }
27
+ }
28
+
29
+ return find(layers, layers.length - 1) ?? false;
30
+ }
package/src/store.ts CHANGED
@@ -14,13 +14,8 @@ function getNavigator(sceneId: string) {
14
14
  return navigators.get(sceneId);
15
15
  }
16
16
 
17
- function clear() {
18
- navigators.clear();
19
- }
20
-
21
17
  export default {
22
18
  addNavigator,
23
19
  removeNavigator,
24
20
  getNavigator,
25
- clear,
26
21
  };
@@ -1,19 +0,0 @@
1
- import type { ComponentType } from 'react';
2
- import type { NavigationItem } from './Options';
3
- import type { RouteMode, ScreenRoute } from './Route';
4
- export interface RouteDefinition<Name extends string = string, Props extends Record<string, any> = Record<never, never>, Result = unknown> {
5
- readonly name: Name;
6
- readonly load: () => ComponentType<any>;
7
- readonly options?: NavigationItem;
8
- readonly path?: string;
9
- readonly dependency?: string;
10
- readonly mode?: RouteMode;
11
- readonly lazy?: boolean;
12
- readonly __types?: ScreenRoute<Props, Result>;
13
- }
14
- export type RouteDefinitionInput<Name extends string, Props extends Record<string, any> = Record<never, never>, Result = unknown> = Omit<RouteDefinition<Name, Props, Result>, '__types'>;
15
- export declare function defineRoute<const Name extends string, Props extends Record<string, any> = Record<never, never>, Result = unknown>(definition: RouteDefinitionInput<Name, Props, Result>): RouteDefinition<Name, Props, Result>;
16
- export declare function defineRoutes<const T extends readonly RouteDefinition<any, any, any>[]>(routes: T): T;
17
- export type RouteParamListOf<T extends readonly RouteDefinition<any, any, any>[]> = {
18
- [R in T[number] as R['name']]: R extends RouteDefinition<any, infer P, infer Res> ? ScreenRoute<P, Res> : ScreenRoute<Record<string, any>, unknown>;
19
- };
@@ -1,6 +0,0 @@
1
- export function defineRoute(definition) {
2
- return definition;
3
- }
4
- export function defineRoutes(routes) {
5
- return routes;
6
- }
@@ -1,39 +0,0 @@
1
- import type { DispatchResult, RouteGraph, SceneCoverageListener, SceneExitHandler } from '../Route';
2
- import type { DispatchParams } from './DispatchCommandHandler';
3
- export default class PresentationCoordinator {
4
- private exitHandlers;
5
- private closingScenes;
6
- private presentingOwnerByChild;
7
- private uncoveredWaiters;
8
- private coverageWatchers;
9
- private coverageLifecycleAttached;
10
- private coverageRefreshScheduled;
11
- private coverageChecking;
12
- private coverageRecheckQueued;
13
- private coverageCheckVersion;
14
- private lastKnownLayers;
15
- private onCoverageLifecycle;
16
- addCoverageListener(sceneId: string, listener: SceneCoverageListener): () => void;
17
- notifyRouteGraphChanged(layers?: RouteGraph[]): void;
18
- private scheduleCoverageRefresh;
19
- private flushCoverageRefresh;
20
- registerExitHandler(sceneId: string, handler: SceneExitHandler): () => void;
21
- private getTopExitHandler;
22
- hasExitHandler(sceneId: string): boolean;
23
- isSceneVisuallyCoveredSync(sceneId: string): boolean;
24
- isSceneVisuallyCovered(sceneId: string): Promise<boolean>;
25
- isSceneCovered(sceneId: string, includeStackCover?: boolean): Promise<boolean>;
26
- private findTopmostDirectPresentedExitSceneId;
27
- private waitUntilUncovered;
28
- private awaitPendingClosesForOwner;
29
- private runExitWithTimeout;
30
- middleware: (sceneId: string, action: string, params: DispatchParams, next: (sceneId: string, action: string, params: DispatchParams) => Promise<DispatchResult>) => Promise<DispatchResult>;
31
- private handleOpen;
32
- private handleClose;
33
- private runDeduplicatedSelfClose;
34
- private waitForDeferredUncover;
35
- private executeSelfClose;
36
- onSceneDestroyed(sceneId: string): void;
37
- onRootReset(): void;
38
- }
39
- export declare const presentationCoordinator: PresentationCoordinator;