hybrid-navigation 5.0.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 (107) hide show
  1. package/android/src/main/java/com/navigation/androidx/AwesomeActivity.java +2 -4
  2. package/android/src/main/java/com/navigation/androidx/AwesomeFragment.java +48 -2
  3. package/android/src/main/java/com/navigation/androidx/DefaultTabBarProvider.java +2 -2
  4. package/android/src/main/java/com/navigation/androidx/FragmentHelper.java +7 -0
  5. package/android/src/main/java/com/navigation/androidx/PresentationDelegate.java +2 -4
  6. package/android/src/main/java/com/navigation/androidx/StackFragment.java +3 -0
  7. package/android/src/main/java/com/navigation/androidx/TabBarFragment.java +50 -0
  8. package/android/src/main/java/com/navigation/androidx/TabBarItem.java +4 -0
  9. package/android/src/main/java/com/reactnative/hybridnavigation/DispatchResult.java +16 -4
  10. package/android/src/main/java/com/reactnative/hybridnavigation/GlobalStyle.java +19 -0
  11. package/android/src/main/java/com/reactnative/hybridnavigation/HBDRootView.java +17 -2
  12. package/android/src/main/java/com/reactnative/hybridnavigation/HybridFragment.java +66 -7
  13. package/android/src/main/java/com/reactnative/hybridnavigation/NativeNavigation.java +116 -18
  14. package/android/src/main/java/com/reactnative/hybridnavigation/NavigationCommandQueue.java +8 -0
  15. package/android/src/main/java/com/reactnative/hybridnavigation/ReactFragment.java +376 -8
  16. package/android/src/main/java/com/reactnative/hybridnavigation/ReactManager.java +72 -2
  17. package/android/src/main/java/com/reactnative/hybridnavigation/ReactTabBarFragment.java +26 -0
  18. package/android/src/main/java/com/reactnative/hybridnavigation/navigator/ScreenNavigator.java +134 -12
  19. package/android/src/main/java/com/reactnative/hybridnavigation/navigator/StackNavigator.java +35 -3
  20. package/android/src/main/java/com/reactnative/hybridnavigation/navigator/TabNavigator.java +29 -4
  21. package/dist/NativeEvent.d.ts +15 -0
  22. package/dist/NativeNavigation.d.ts +8 -0
  23. package/dist/Navigation.d.ts +14 -1
  24. package/dist/Navigation.js +153 -12
  25. package/dist/NavigationComponentWrap.d.ts +3 -2
  26. package/dist/Navigator.d.ts +3 -2
  27. package/dist/Navigator.js +28 -9
  28. package/dist/Options.d.ts +12 -2
  29. package/dist/Route.d.ts +70 -5
  30. package/dist/defineRoutes.d.ts +19 -0
  31. package/dist/defineRoutes.js +6 -0
  32. package/dist/handler/DispatchCommandHandler.d.ts +16 -1
  33. package/dist/handler/DispatchCommandHandler.js +63 -5
  34. package/dist/handler/LayoutCommandHandler.d.ts +1 -0
  35. package/dist/handler/LayoutCommandHandler.js +3 -0
  36. package/dist/handler/LeaveGuardHandler.d.ts +17 -0
  37. package/dist/handler/LeaveGuardHandler.js +350 -0
  38. package/dist/handler/LifecycleEventHandler.d.ts +7 -3
  39. package/dist/handler/LifecycleEventHandler.js +36 -3
  40. package/dist/handler/PresentationCoordinator.d.ts +24 -0
  41. package/dist/handler/PresentationCoordinator.js +449 -0
  42. package/dist/handler/ResultEventHandler.d.ts +7 -0
  43. package/dist/handler/ResultEventHandler.js +13 -5
  44. package/dist/handler/TabEventHandler.d.ts +8 -0
  45. package/dist/handler/TabEventHandler.js +25 -0
  46. package/dist/hooks.d.ts +3 -1
  47. package/dist/hooks.js +20 -0
  48. package/dist/index.d.ts +11 -0
  49. package/dist/index.js +3 -0
  50. package/dist/reveal.d.ts +46 -0
  51. package/dist/reveal.js +431 -0
  52. package/dist/routeGraph.d.ts +12 -0
  53. package/dist/routeGraph.js +68 -0
  54. package/dist/router/index.js +5 -41
  55. package/dist/router/tabs.d.ts +1 -0
  56. package/dist/store.d.ts +2 -0
  57. package/dist/store.js +4 -0
  58. package/dist/testing.d.ts +79 -0
  59. package/dist/testing.js +251 -0
  60. package/ios/HBDGlobalStyle.h +3 -0
  61. package/ios/HBDGlobalStyle.mm +13 -0
  62. package/ios/HybridNavigation.h +1 -0
  63. package/ios/Navigator/HBDScreenNavigator.mm +177 -11
  64. package/ios/Navigator/HBDStackNavigator.mm +50 -7
  65. package/ios/Navigator/HBDTabNavigator.mm +33 -5
  66. package/ios/React/HBDNativeNavigation.mm +73 -12
  67. package/ios/React/HBDNavigator.h +8 -0
  68. package/ios/React/HBDOrientationCoordinator.h +2 -0
  69. package/ios/React/HBDOrientationCoordinator.mm +27 -0
  70. package/ios/React/HBDReactBridgeManager.h +10 -0
  71. package/ios/React/HBDReactBridgeManager.mm +48 -2
  72. package/ios/ViewController/HBDNavigationController.mm +14 -0
  73. package/ios/ViewController/HBDReactViewController.h +6 -0
  74. package/ios/ViewController/HBDReactViewController.mm +177 -1
  75. package/ios/ViewController/HBDTabBarController.h +11 -0
  76. package/ios/ViewController/HBDTabBarController.mm +170 -3
  77. package/ios/ViewController/HBDTabBarRenderer.h +28 -0
  78. package/ios/ViewController/HBDViewController.h +1 -0
  79. package/ios/ViewController/HBDViewController.mm +57 -9
  80. package/ios/ViewController/UIViewController+HBD.h +1 -0
  81. package/ios/ViewController/UIViewController+HBD.mm +13 -4
  82. package/package.json +3 -1
  83. package/src/NativeEvent.ts +17 -0
  84. package/src/NativeNavigation.ts +9 -0
  85. package/src/Navigation.ts +228 -12
  86. package/src/NavigationComponentWrap.tsx +11 -2
  87. package/src/Navigator.ts +38 -9
  88. package/src/Options.ts +9 -2
  89. package/src/Route.ts +102 -6
  90. package/src/defineRoutes.ts +46 -0
  91. package/src/handler/DispatchCommandHandler.ts +110 -11
  92. package/src/handler/LayoutCommandHandler.ts +4 -0
  93. package/src/handler/LeaveGuardHandler.ts +436 -0
  94. package/src/handler/LifecycleEventHandler.ts +85 -38
  95. package/src/handler/PresentationCoordinator.ts +560 -0
  96. package/src/handler/ResultEventHandler.ts +23 -9
  97. package/src/handler/TabEventHandler.ts +31 -0
  98. package/src/hooks.ts +37 -1
  99. package/src/index.ts +11 -0
  100. package/src/reveal.ts +565 -0
  101. package/src/routeGraph.ts +87 -0
  102. package/src/router/index.ts +5 -40
  103. package/src/router/tabs.ts +1 -0
  104. package/src/store.ts +5 -0
  105. package/src/testing.ts +395 -0
  106. package/testing.d.ts +1 -0
  107. package/testing.js +3 -0
@@ -0,0 +1,436 @@
1
+ import NativeEvent from '../NativeEvent';
2
+ import NativeNavigation from '../NativeNavigation';
3
+ import type {
4
+ BeforeRemoveEvent,
5
+ BeforeRemoveListener,
6
+ BeforeRemoveOptions,
7
+ DispatchResult,
8
+ LeaveAction,
9
+ RouteGraph,
10
+ } from '../Route';
11
+ import { containsScene, findSceneInGraph } from '../routeGraph';
12
+ import type { DispatchParams } from './DispatchCommandHandler';
13
+ import { lifecycleHandler } from './LifecycleEventHandler';
14
+
15
+ const GUARDED_ACTIONS = new Set<LeaveAction>([
16
+ 'pop',
17
+ 'popTo',
18
+ 'popToRoot',
19
+ 'dismiss',
20
+ 'hideModal',
21
+ 'close',
22
+ 'redirectTo',
23
+ ]);
24
+
25
+ const BYPASS_PROP = '__hbdBypassBeforeRemove';
26
+ const APPROVED_SCENES_PROP = '__hbdApprovedBeforeRemoveScenes';
27
+
28
+ function collectScreenIds(graph: RouteGraph, out: string[]): void {
29
+ if (graph.layout === 'screen') {
30
+ out.push(graph.sceneId);
31
+ return;
32
+ }
33
+ if (!graph.children) {
34
+ return;
35
+ }
36
+ for (let i = graph.children.length - 1; i >= 0; i--) {
37
+ collectScreenIds(graph.children[i], out);
38
+ }
39
+ }
40
+
41
+ function findContainingStack(graph: RouteGraph, sceneId: string): RouteGraph | null {
42
+ if (graph.layout === 'stack' && Array.isArray(graph.children)) {
43
+ for (const child of graph.children) {
44
+ const nested = findContainingStack(child, sceneId);
45
+ if (nested) {
46
+ return nested;
47
+ }
48
+ }
49
+ if (graph.children.some(child => containsScene(child, sceneId))) {
50
+ return graph;
51
+ }
52
+ }
53
+ if (!graph.children) {
54
+ return null;
55
+ }
56
+ for (const child of graph.children) {
57
+ const found = findContainingStack(child, sceneId);
58
+ if (found) {
59
+ return found;
60
+ }
61
+ }
62
+ return null;
63
+ }
64
+
65
+ export function collectRemovedStackScenes(
66
+ layers: RouteGraph[],
67
+ sceneId: string,
68
+ action: 'popTo' | 'popToRoot',
69
+ targetModuleOrSceneId?: string,
70
+ inclusive: boolean = false,
71
+ ): string[] {
72
+ let stack: RouteGraph | null = null;
73
+ for (let i = layers.length - 1; i >= 0; i--) {
74
+ stack = findContainingStack(layers[i], sceneId);
75
+ if (stack) {
76
+ break;
77
+ }
78
+ }
79
+ if (!stack || !Array.isArray(stack.children) || stack.children.length <= 1) {
80
+ return [sceneId];
81
+ }
82
+ const fromIndex = stack.children.findIndex(child => containsScene(child, sceneId));
83
+ if (fromIndex <= 0) {
84
+ return [sceneId];
85
+ }
86
+ let stopIndex = 0;
87
+ if (action === 'popTo') {
88
+ if (!targetModuleOrSceneId) {
89
+ return [sceneId];
90
+ }
91
+ const targetIndex = stack.children.findIndex(
92
+ child =>
93
+ child.sceneId === targetModuleOrSceneId ||
94
+ containsScene(child, targetModuleOrSceneId) ||
95
+ Boolean(findSceneInGraph(child, node => node.moduleName === targetModuleOrSceneId)),
96
+ );
97
+ if (targetIndex < 0 || targetIndex >= fromIndex) {
98
+ return [sceneId];
99
+ }
100
+ // Match native iOS/Android behavior: inclusive && targetIndex > 0 pops targetIndex itself.
101
+ stopIndex = inclusive && targetIndex > 0 ? targetIndex - 1 : targetIndex;
102
+ }
103
+ const removed: string[] = [];
104
+ for (let i = fromIndex; i > stopIndex; i--) {
105
+ collectScreenIds(stack.children[i], removed);
106
+ }
107
+ return removed.length > 0 ? removed : [sceneId];
108
+ }
109
+
110
+ function collectPresentedAboveScenes(layers: RouteGraph[], sceneId: string): string[] {
111
+ if (!Array.isArray(layers) || layers.length <= 1) {
112
+ return [];
113
+ }
114
+ const index = layers.findIndex(layer => containsScene(layer, sceneId));
115
+ if (index < 0 || index >= layers.length - 1) {
116
+ return [];
117
+ }
118
+ const removed: string[] = [];
119
+ for (let i = layers.length - 1; i > index; i--) {
120
+ collectScreenIds(layers[i], removed);
121
+ }
122
+ return removed;
123
+ }
124
+
125
+ interface BeforeRemoveEntry {
126
+ listener: BeforeRemoveListener;
127
+ actions?: Set<LeaveAction>;
128
+ }
129
+
130
+ function matchesAction(entry: BeforeRemoveEntry, action: LeaveAction, sceneId?: string): boolean {
131
+ if (!entry.actions) {
132
+ return true;
133
+ }
134
+ if (entry.actions.has(action)) {
135
+ return true;
136
+ }
137
+ if (action === 'close' && sceneId) {
138
+ const mode = lifecycleHandler.getSceneInfo(sceneId)?.mode;
139
+ if (mode === 'modal' && entry.actions.has('hideModal')) {
140
+ return true;
141
+ }
142
+ if (mode === 'present' && entry.actions.has('dismiss')) {
143
+ return true;
144
+ }
145
+ if (mode === 'normal' && entry.actions.has('pop')) {
146
+ return true;
147
+ }
148
+ }
149
+ return false;
150
+ }
151
+
152
+ function runGuardedChain(
153
+ sceneId: string,
154
+ action: LeaveAction,
155
+ matching: BeforeRemoveEntry[],
156
+ executeFinalProceed: () => Promise<DispatchResult>,
157
+ ): Promise<DispatchResult> {
158
+ let finalPromise: Promise<DispatchResult> | null = null;
159
+ const runFinal = (): Promise<DispatchResult> => {
160
+ if (!finalPromise) {
161
+ finalPromise = executeFinalProceed();
162
+ }
163
+ return finalPromise;
164
+ };
165
+
166
+ const runAt = async (index: number): Promise<DispatchResult> => {
167
+ if (index >= matching.length) {
168
+ return runFinal();
169
+ }
170
+ let stepProceedPromise: Promise<DispatchResult> | null = null;
171
+ const event: BeforeRemoveEvent = {
172
+ sceneId,
173
+ action,
174
+ proceed: () => {
175
+ if (!stepProceedPromise) {
176
+ stepProceedPromise = runAt(index + 1);
177
+ }
178
+ return stepProceedPromise;
179
+ },
180
+ };
181
+ await matching[index].listener(event);
182
+ if (stepProceedPromise) {
183
+ return await stepProceedPromise;
184
+ }
185
+ return {
186
+ ok: false,
187
+ code: 'intercepted',
188
+ sceneId,
189
+ message: 'Navigation prevented by beforeRemove guard.',
190
+ };
191
+ };
192
+
193
+ return runAt(0);
194
+ }
195
+
196
+ export default class LeaveGuardHandler {
197
+ private listeners = new Map<string, Set<BeforeRemoveEntry>>();
198
+ private nativeEnabledScenes = new Set<string>();
199
+ private rootGeneration = 0;
200
+ private dispatchFn?: (
201
+ sceneId: string,
202
+ action: string,
203
+ params: DispatchParams,
204
+ ) => Promise<DispatchResult>;
205
+
206
+ setDispatcher(
207
+ dispatcher: (
208
+ sceneId: string,
209
+ action: string,
210
+ params: DispatchParams,
211
+ ) => Promise<DispatchResult>,
212
+ ) {
213
+ this.dispatchFn = dispatcher;
214
+ }
215
+
216
+ handleNativeBeforeRemoveRequests() {
217
+ NativeEvent.onBeforeRemoveRequest(({ sceneId, action }) => {
218
+ const generation = this.rootGeneration;
219
+ const leaveAction = (action || 'back') as LeaveAction;
220
+ const proceedClose = async (): Promise<DispatchResult> => {
221
+ if (
222
+ generation !== this.rootGeneration ||
223
+ lifecycleHandler.getSceneState(sceneId) === 'destroyed'
224
+ ) {
225
+ return {
226
+ ok: false,
227
+ code: 'superseded',
228
+ message: 'Scene or root changed before proceed.',
229
+ };
230
+ }
231
+ if (!this.dispatchFn) {
232
+ return { ok: false, code: 'not_ready' };
233
+ }
234
+ return this.dispatchFn(sceneId, 'close', {
235
+ target: 'self',
236
+ [BYPASS_PROP]: true,
237
+ });
238
+ };
239
+
240
+ const set = this.listeners.get(sceneId);
241
+ const matching = set
242
+ ? Array.from(set).filter(entry => matchesAction(entry, leaveAction))
243
+ : [];
244
+ if (matching.length === 0) {
245
+ // 异步标志位尚未同步到原生时若刚好触发侧滑/返回键,重新同步关掉原生标志并直接放行退场,避免吞掉返回操作。
246
+ this.syncNativeEnabled(sceneId);
247
+ if (!this.nativeEnabledScenes.has(sceneId)) {
248
+ NativeNavigation.setBeforeRemoveEnabled(sceneId, false);
249
+ }
250
+ void proceedClose();
251
+ return;
252
+ }
253
+ runGuardedChain(sceneId, leaveAction, matching, proceedClose);
254
+ });
255
+ }
256
+
257
+ private syncNativeEnabled(sceneId: string) {
258
+ const set = this.listeners.get(sceneId);
259
+ const shouldEnable =
260
+ Boolean(set) &&
261
+ Array.from(set!).some(entry => matchesAction(entry, 'back'));
262
+ const isEnabled = this.nativeEnabledScenes.has(sceneId);
263
+ if (shouldEnable === isEnabled) {
264
+ return;
265
+ }
266
+ if (shouldEnable) {
267
+ this.nativeEnabledScenes.add(sceneId);
268
+ NativeNavigation.setBeforeRemoveEnabled(sceneId, true);
269
+ } else {
270
+ this.nativeEnabledScenes.delete(sceneId);
271
+ if (lifecycleHandler.getSceneState(sceneId) !== 'destroyed') {
272
+ NativeNavigation.setBeforeRemoveEnabled(sceneId, false);
273
+ }
274
+ }
275
+ }
276
+
277
+ addListener(
278
+ sceneId: string,
279
+ listener: BeforeRemoveListener,
280
+ options?: BeforeRemoveOptions,
281
+ ): () => void {
282
+ let set = this.listeners.get(sceneId);
283
+ if (!set) {
284
+ set = new Set();
285
+ this.listeners.set(sceneId, set);
286
+ }
287
+ const entry: BeforeRemoveEntry = {
288
+ listener,
289
+ actions: options?.actions ? new Set(options.actions) : undefined,
290
+ };
291
+ set.add(entry);
292
+ this.syncNativeEnabled(sceneId);
293
+
294
+ return () => {
295
+ const current = this.listeners.get(sceneId);
296
+ if (!current) {
297
+ return;
298
+ }
299
+ current.delete(entry);
300
+ if (current.size === 0) {
301
+ this.listeners.delete(sceneId);
302
+ }
303
+ this.syncNativeEnabled(sceneId);
304
+ };
305
+ }
306
+
307
+ middleware = async (
308
+ sceneId: string,
309
+ action: string,
310
+ params: DispatchParams,
311
+ next: (sceneId: string, action: string, params: DispatchParams) => Promise<DispatchResult>,
312
+ ): Promise<DispatchResult> => {
313
+ if (params[BYPASS_PROP] || params.bypassBeforeRemove) {
314
+ const cleanParams: DispatchParams = { ...params };
315
+ delete cleanParams[BYPASS_PROP];
316
+ delete cleanParams.bypassBeforeRemove;
317
+ return next(sceneId, action, cleanParams);
318
+ }
319
+
320
+ if (!GUARDED_ACTIONS.has(action as LeaveAction) || this.listeners.size === 0) {
321
+ return next(sceneId, action, params);
322
+ }
323
+
324
+ const leaveAction = action as LeaveAction;
325
+ const approved: Set<string> =
326
+ params[APPROVED_SCENES_PROP] instanceof Set ? params[APPROVED_SCENES_PROP] : new Set();
327
+
328
+ let candidateSceneIds: string[] = [sceneId];
329
+ if (leaveAction === 'popTo' || leaveAction === 'popToRoot') {
330
+ try {
331
+ const layers = await NativeNavigation.routeGraph();
332
+ if (Array.isArray(layers) && layers.length > 0) {
333
+ candidateSceneIds = collectRemovedStackScenes(
334
+ layers,
335
+ sceneId,
336
+ leaveAction,
337
+ typeof params.moduleName === 'string'
338
+ ? params.moduleName
339
+ : typeof params.to === 'string'
340
+ ? params.to
341
+ : undefined,
342
+ Boolean(params.inclusive),
343
+ );
344
+ }
345
+ } catch {
346
+ candidateSceneIds = [sceneId];
347
+ }
348
+ } else if (
349
+ (leaveAction === 'dismiss' || leaveAction === 'hideModal') &&
350
+ params.target !== 'self'
351
+ ) {
352
+ try {
353
+ const layers = await NativeNavigation.routeGraph();
354
+ const presentedAbove = collectPresentedAboveScenes(layers, sceneId);
355
+ if (presentedAbove.length > 0) {
356
+ candidateSceneIds = presentedAbove;
357
+ } else if (params.target === 'presented') {
358
+ candidateSceneIds = [];
359
+ }
360
+ } catch {
361
+ candidateSceneIds = params.target === 'presented' ? [] : [sceneId];
362
+ }
363
+ }
364
+
365
+ let guardedSceneId: string | null = null;
366
+ let matching: BeforeRemoveEntry[] = [];
367
+ for (const candidateId of candidateSceneIds) {
368
+ if (approved.has(candidateId)) {
369
+ continue;
370
+ }
371
+ const set = this.listeners.get(candidateId);
372
+ if (!set || set.size === 0) {
373
+ continue;
374
+ }
375
+ const hits = Array.from(set).filter(entry =>
376
+ matchesAction(entry, leaveAction, candidateId),
377
+ );
378
+ if (hits.length > 0) {
379
+ guardedSceneId = candidateId;
380
+ matching = hits;
381
+ break;
382
+ }
383
+ }
384
+
385
+ if (!guardedSceneId || matching.length === 0) {
386
+ const cleanParams: DispatchParams = { ...params };
387
+ delete cleanParams[APPROVED_SCENES_PROP];
388
+ return next(sceneId, action, cleanParams);
389
+ }
390
+
391
+ const isMultiScene = candidateSceneIds.length > 1;
392
+ const generation = this.rootGeneration;
393
+ const targetGuardedSceneId = guardedSceneId;
394
+
395
+ return runGuardedChain(targetGuardedSceneId, leaveAction, matching, async () => {
396
+ if (
397
+ generation !== this.rootGeneration ||
398
+ lifecycleHandler.getSceneState(targetGuardedSceneId) === 'destroyed'
399
+ ) {
400
+ return {
401
+ ok: false,
402
+ code: 'superseded',
403
+ message: 'Scene or root changed before proceed.',
404
+ };
405
+ }
406
+ if (!this.dispatchFn) {
407
+ return { ok: false, code: 'not_ready' };
408
+ }
409
+ if (isMultiScene) {
410
+ const nextApproved = new Set(approved);
411
+ nextApproved.add(targetGuardedSceneId);
412
+ return this.dispatchFn(sceneId, action, {
413
+ ...params,
414
+ [APPROVED_SCENES_PROP]: nextApproved,
415
+ });
416
+ }
417
+ return this.dispatchFn(sceneId, action, {
418
+ ...params,
419
+ [BYPASS_PROP]: true,
420
+ });
421
+ });
422
+ };
423
+
424
+ onSceneDestroyed(sceneId: string) {
425
+ this.listeners.delete(sceneId);
426
+ this.nativeEnabledScenes.delete(sceneId);
427
+ }
428
+
429
+ onRootReset() {
430
+ this.rootGeneration += 1;
431
+ this.listeners.clear();
432
+ this.nativeEnabledScenes.clear();
433
+ }
434
+ }
435
+
436
+ export const leaveGuardHandler = new LeaveGuardHandler();
@@ -1,4 +1,4 @@
1
- import type { SceneLifecycleState, Visibility } from '../Route';
1
+ import type { LayoutMode, SceneInfo, SceneLifecycleState, Visibility } from '../Route';
2
2
  import NativeEvent from '../NativeEvent';
3
3
 
4
4
  export type SceneLifecycleListener = (
@@ -9,9 +9,14 @@ export type GlobalSceneLifecycleListener = (
9
9
  sceneId: string,
10
10
  state: SceneLifecycleState,
11
11
  prevState: SceneLifecycleState,
12
+ info?: SceneInfo,
12
13
  ) => void;
13
14
  export type VisibilityEventListener = (visibility: Visibility) => void;
14
- export type GlobalVisibilityEventListener = (sceneId: string, visibility: Visibility) => void;
15
+ export type GlobalVisibilityEventListener = (
16
+ sceneId: string,
17
+ visibility: Visibility,
18
+ info?: SceneInfo,
19
+ ) => void;
15
20
 
16
21
  const VALID_STATES = new Set<SceneLifecycleState>([
17
22
  'created',
@@ -20,6 +25,8 @@ const VALID_STATES = new Set<SceneLifecycleState>([
20
25
  'destroyed',
21
26
  ]);
22
27
 
28
+ const VALID_MODES = new Set<LayoutMode>(['modal', 'present', 'normal']);
29
+
23
30
  function normalizeLifecycleState(raw: string): SceneLifecycleState {
24
31
  if (VALID_STATES.has(raw as SceneLifecycleState)) {
25
32
  return raw as SceneLifecycleState;
@@ -27,10 +34,18 @@ function normalizeLifecycleState(raw: string): SceneLifecycleState {
27
34
  return 'created';
28
35
  }
29
36
 
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
+
30
44
  const MAX_DESTROYED_SCENES = 2000;
31
45
 
32
46
  export default class LifecycleEventHandler {
33
47
  private sceneStates = new Map<string, SceneLifecycleState>();
48
+ private sceneInfos = new Map<string, SceneInfo>();
34
49
  private destroyedScenes = new Set<string>();
35
50
  private lifecycleListeners: Record<string, SceneLifecycleListener[]> = {};
36
51
  private globalLifecycleListeners: GlobalSceneLifecycleListener[] = [];
@@ -38,50 +53,81 @@ export default class LifecycleEventHandler {
38
53
  private globalVisibilityListeners: GlobalVisibilityEventListener[] = [];
39
54
 
40
55
  handleSceneLifecycle(onSceneDestroyed?: (sceneId: string) => void) {
41
- NativeEvent.onSceneStateChange(({ sceneId, state, prevState }) => {
42
- const next = normalizeLifecycleState(state);
43
- const prev = normalizeLifecycleState(prevState);
56
+ NativeEvent.onSceneStateChange(
57
+ ({ sceneId, state, prevState, moduleName, mode, presentingId }) => {
58
+ const next = normalizeLifecycleState(state);
59
+ const prev = normalizeLifecycleState(prevState);
44
60
 
45
- if (next !== 'destroyed') {
46
- this.destroyedScenes.delete(sceneId);
47
- }
48
- this.sceneStates.set(sceneId, next);
49
-
50
- const visibilityChange: Visibility | null =
51
- next === 'visible'
52
- ? 'visible'
53
- : prev === 'visible' && (next === 'invisible' || next === 'destroyed')
54
- ? 'invisible'
55
- : null;
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
+ }
56
89
 
57
- if (visibilityChange) {
58
- this.globalVisibilityListeners
90
+ this.globalLifecycleListeners
59
91
  .slice()
60
- .forEach(listener => listener(sceneId, visibilityChange));
61
- }
92
+ .forEach(listener => listener(sceneId, next, prev, info));
62
93
 
63
- this.globalLifecycleListeners
64
- .slice()
65
- .forEach(listener => listener(sceneId, next, prev));
94
+ const sceneListeners = this.lifecycleListeners[sceneId];
95
+ if (sceneListeners) {
96
+ sceneListeners.slice().forEach(listener => listener(next, prev));
97
+ }
66
98
 
67
- const sceneListeners = this.lifecycleListeners[sceneId];
68
- if (sceneListeners) {
69
- sceneListeners.slice().forEach(listener => listener(next, prev));
70
- }
99
+ if (visibilityChange) {
100
+ const visListeners = this.visibilityListeners[sceneId];
101
+ if (visListeners) {
102
+ visListeners.slice().forEach(listener => listener(visibilityChange));
103
+ }
104
+ }
71
105
 
72
- if (visibilityChange) {
73
- const visListeners = this.visibilityListeners[sceneId];
74
- if (visListeners) {
75
- visListeners.slice().forEach(listener => listener(visibilityChange));
106
+ if (next === 'destroyed') {
107
+ this.markSceneDestroyed(sceneId);
108
+ onSceneDestroyed?.(sceneId);
109
+ this.cleanupScene(sceneId);
76
110
  }
77
- }
111
+ },
112
+ );
113
+ }
78
114
 
79
- if (next === 'destroyed') {
80
- this.markSceneDestroyed(sceneId);
81
- onSceneDestroyed?.(sceneId);
82
- this.cleanupScene(sceneId);
83
- }
84
- });
115
+ getSceneInfo(sceneId: string): SceneInfo | undefined {
116
+ return this.sceneInfos.get(sceneId);
117
+ }
118
+
119
+ onRootReset() {
120
+ // Lifecycle state maps are updated by native destroyed events.
121
+ }
122
+
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 = [];
85
131
  }
86
132
 
87
133
  private markSceneDestroyed(sceneId: string) {
@@ -108,6 +154,7 @@ export default class LifecycleEventHandler {
108
154
 
109
155
  cleanupScene(sceneId: string) {
110
156
  this.sceneStates.delete(sceneId);
157
+ this.sceneInfos.delete(sceneId);
111
158
  delete this.lifecycleListeners[sceneId];
112
159
  delete this.visibilityListeners[sceneId];
113
160
  }