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,560 @@
1
+ import NativeNavigation from '../NativeNavigation';
2
+ import type {
3
+ CloseTarget,
4
+ CoveredPolicy,
5
+ DispatchResult,
6
+ RouteGraph,
7
+ SceneExitHandler,
8
+ SceneLifecycleState,
9
+ } from '../Route';
10
+ import { containsScene, findSceneInGraph } from '../routeGraph';
11
+ import type { DispatchParams } from './DispatchCommandHandler';
12
+ import { lifecycleHandler } from './LifecycleEventHandler';
13
+
14
+ const PRESENT_ACTIONS = new Set([
15
+ 'showModal',
16
+ 'showModalLayout',
17
+ 'present',
18
+ 'presentLayout',
19
+ ]);
20
+
21
+ const CLOSE_ACTIONS = new Set(['hideModal', 'dismiss', 'close']);
22
+
23
+ const DEFAULT_EXIT_TIMEOUT_MS = 1200;
24
+ const MAX_IMMEDIATE_COVERED_RETRIES = 1;
25
+
26
+ function findInnermostContainingStack(graph: RouteGraph, sceneId: string): RouteGraph | null {
27
+ if (graph.layout === 'stack' && Array.isArray(graph.children)) {
28
+ for (const child of graph.children) {
29
+ if (!containsScene(child, sceneId)) {
30
+ continue;
31
+ }
32
+ const nested = findInnermostContainingStack(child, sceneId);
33
+ return nested ?? graph;
34
+ }
35
+ return null;
36
+ }
37
+ if (!Array.isArray(graph.children)) {
38
+ return null;
39
+ }
40
+ for (const child of graph.children) {
41
+ if (containsScene(child, sceneId)) {
42
+ return findInnermostContainingStack(child, sceneId);
43
+ }
44
+ }
45
+ return null;
46
+ }
47
+
48
+ function isSceneCoveredInStack(graph: RouteGraph, sceneId: string): boolean {
49
+ const stack = findInnermostContainingStack(graph, sceneId);
50
+ if (!stack || !Array.isArray(stack.children) || stack.children.length <= 1) {
51
+ return false;
52
+ }
53
+ const childIndex = stack.children.findIndex(child => containsScene(child, sceneId));
54
+ return childIndex >= 0 && childIndex < stack.children.length - 1;
55
+ }
56
+
57
+ interface PendingCloseEntry {
58
+ promise: Promise<DispatchResult>;
59
+ deferred: boolean;
60
+ nativeParams: DispatchParams;
61
+ }
62
+
63
+ interface DeferredUncoverResult {
64
+ uncovered: boolean;
65
+ waited: boolean;
66
+ }
67
+
68
+ export default class PresentationCoordinator {
69
+ private exitHandlers = new Map<string, SceneExitHandler>();
70
+ private closingScenes = new Map<string, PendingCloseEntry>();
71
+ private presentingOwnerByChild = new Map<string, string>();
72
+ private uncoveredWaiters = new Map<string, Set<(result: DeferredUncoverResult) => void>>();
73
+
74
+ registerExitHandler(sceneId: string, handler: SceneExitHandler): () => void {
75
+ this.exitHandlers.set(sceneId, handler);
76
+ return () => {
77
+ if (this.exitHandlers.get(sceneId) === handler) {
78
+ this.exitHandlers.delete(sceneId);
79
+ }
80
+ };
81
+ }
82
+
83
+ hasExitHandler(sceneId: string): boolean {
84
+ return this.exitHandlers.has(sceneId);
85
+ }
86
+
87
+ async isSceneCovered(sceneId: string, includeStackCover: boolean = false): Promise<boolean> {
88
+ try {
89
+ const layers: RouteGraph[] = await NativeNavigation.routeGraph();
90
+ if (!Array.isArray(layers) || layers.length === 0) {
91
+ return false;
92
+ }
93
+ const index = layers.findIndex(layer => containsScene(layer, sceneId));
94
+ if (index < 0) {
95
+ return false;
96
+ }
97
+ if (index < layers.length - 1) {
98
+ return true;
99
+ }
100
+ return includeStackCover ? isSceneCoveredInStack(layers[index], sceneId) : false;
101
+ } catch {
102
+ return false;
103
+ }
104
+ }
105
+
106
+ private async findTopmostDirectPresentedExitSceneId(sceneId: string): Promise<string | null> {
107
+ try {
108
+ const layers: RouteGraph[] = await NativeNavigation.routeGraph();
109
+ if (!Array.isArray(layers) || layers.length <= 1) {
110
+ return null;
111
+ }
112
+ const index = layers.findIndex(layer => containsScene(layer, sceneId));
113
+ if (index < 0 || index + 1 !== layers.length - 1) {
114
+ return null;
115
+ }
116
+ const presentedLayer = layers[index + 1];
117
+ if (presentedLayer.layout === 'screen') {
118
+ return this.exitHandlers.has(presentedLayer.sceneId) ? presentedLayer.sceneId : null;
119
+ }
120
+ const match = findSceneInGraph(presentedLayer, node => this.exitHandlers.has(node.sceneId));
121
+ return match?.sceneId ?? null;
122
+ } catch {
123
+ return null;
124
+ }
125
+ }
126
+
127
+ private waitUntilUncovered(
128
+ sceneId: string,
129
+ includeStackCover: boolean,
130
+ ): Promise<DeferredUncoverResult> {
131
+ if (lifecycleHandler.getSceneState(sceneId) === 'destroyed') {
132
+ return Promise.resolve({ uncovered: false, waited: false });
133
+ }
134
+ return new Promise<DeferredUncoverResult>(resolve => {
135
+ let settled = false;
136
+ let waited = false;
137
+ let checkVersion = 0;
138
+ let checking = false;
139
+ let recheckQueued = false;
140
+
141
+ const finish = (result: DeferredUncoverResult) => {
142
+ if (settled) {
143
+ return;
144
+ }
145
+ settled = true;
146
+ lifecycleHandler.removeGlobalLifecycleListener(onGlobalLifecycle);
147
+ const waiters = this.uncoveredWaiters.get(sceneId);
148
+ if (waiters) {
149
+ waiters.delete(finish);
150
+ if (waiters.size === 0) {
151
+ this.uncoveredWaiters.delete(sceneId);
152
+ }
153
+ }
154
+ resolve(result);
155
+ };
156
+
157
+ const runCheck = () => {
158
+ if (settled) {
159
+ return;
160
+ }
161
+ if (lifecycleHandler.getSceneState(sceneId) === 'destroyed') {
162
+ finish({ uncovered: false, waited });
163
+ return;
164
+ }
165
+ if (checking) {
166
+ recheckQueued = true;
167
+ return;
168
+ }
169
+ checking = true;
170
+ const version = ++checkVersion;
171
+ this.isSceneCovered(sceneId, includeStackCover).then(
172
+ covered => {
173
+ checking = false;
174
+ if (settled || version !== checkVersion) {
175
+ return;
176
+ }
177
+ if (lifecycleHandler.getSceneState(sceneId) === 'destroyed') {
178
+ finish({ uncovered: false, waited });
179
+ return;
180
+ }
181
+ if (!covered) {
182
+ finish({ uncovered: true, waited });
183
+ return;
184
+ }
185
+ waited = true;
186
+ if (recheckQueued) {
187
+ recheckQueued = false;
188
+ runCheck();
189
+ }
190
+ },
191
+ () => {
192
+ checking = false;
193
+ if (settled) {
194
+ return;
195
+ }
196
+ waited = true;
197
+ if (recheckQueued) {
198
+ recheckQueued = false;
199
+ runCheck();
200
+ }
201
+ },
202
+ );
203
+ };
204
+
205
+ const onGlobalLifecycle = (changedSceneId: string, nextState: SceneLifecycleState) => {
206
+ if (settled) {
207
+ return;
208
+ }
209
+ if (changedSceneId === sceneId) {
210
+ if (nextState === 'destroyed') {
211
+ finish({ uncovered: false, waited });
212
+ } else if (nextState === 'visible') {
213
+ waited = true;
214
+ runCheck();
215
+ }
216
+ return;
217
+ }
218
+ // iOS OverFullScreen modal dismissal emits invisible/destroyed on the upper scene
219
+ // without re-emitting visible on the underlying scene.
220
+ if (nextState === 'invisible' || nextState === 'destroyed') {
221
+ waited = true;
222
+ runCheck();
223
+ }
224
+ };
225
+
226
+ let waiters = this.uncoveredWaiters.get(sceneId);
227
+ if (!waiters) {
228
+ waiters = new Set();
229
+ this.uncoveredWaiters.set(sceneId, waiters);
230
+ }
231
+ waiters.add(finish);
232
+ lifecycleHandler.addGlobalLifecycleListener(onGlobalLifecycle);
233
+ // Re-check right after subscribing so an uncover during the prior async check is never missed.
234
+ runCheck();
235
+ });
236
+ }
237
+
238
+ private async awaitPendingClosesForOwner(ownerSceneId: string): Promise<void> {
239
+ if (this.closingScenes.size === 0) {
240
+ return;
241
+ }
242
+ const pending: Promise<unknown>[] = [];
243
+ for (const [closingSceneId, entry] of this.closingScenes) {
244
+ if (entry.deferred || closingSceneId === ownerSceneId) {
245
+ continue;
246
+ }
247
+ const presentingOwner =
248
+ lifecycleHandler.getSceneInfo(closingSceneId)?.presentingId ??
249
+ this.presentingOwnerByChild.get(closingSceneId);
250
+ if (!presentingOwner || presentingOwner !== ownerSceneId) {
251
+ continue;
252
+ }
253
+ pending.push(entry.promise.catch(() => undefined));
254
+ }
255
+ if (pending.length > 0) {
256
+ await Promise.all(pending);
257
+ }
258
+ }
259
+
260
+ private runExitWithTimeout(handler: SceneExitHandler): Promise<void> {
261
+ const timeoutMs = handler.exitTimeoutMs ?? DEFAULT_EXIT_TIMEOUT_MS;
262
+ let result: void | Promise<void>;
263
+ try {
264
+ result = handler.onExit();
265
+ } catch (error) {
266
+ return Promise.reject(error);
267
+ }
268
+ if (!result || typeof (result as Promise<void>).then !== 'function') {
269
+ return Promise.resolve();
270
+ }
271
+ if (timeoutMs <= 0) {
272
+ return result;
273
+ }
274
+ return new Promise<void>((resolve, reject) => {
275
+ let settled = false;
276
+ const timer = setTimeout(() => {
277
+ if (settled) {
278
+ return;
279
+ }
280
+ settled = true;
281
+ reject(new Error(`Scene exit handler timed out after ${timeoutMs}ms.`));
282
+ }, timeoutMs);
283
+ (timer as { unref?: () => void }).unref?.();
284
+ Promise.resolve(result).then(
285
+ () => {
286
+ if (settled) {
287
+ return;
288
+ }
289
+ settled = true;
290
+ clearTimeout(timer);
291
+ resolve();
292
+ },
293
+ error => {
294
+ if (settled) {
295
+ return;
296
+ }
297
+ settled = true;
298
+ clearTimeout(timer);
299
+ reject(error);
300
+ },
301
+ );
302
+ });
303
+ }
304
+
305
+ middleware = async (
306
+ sceneId: string,
307
+ action: string,
308
+ params: DispatchParams,
309
+ next: (sceneId: string, action: string, params: DispatchParams) => Promise<DispatchResult>,
310
+ ): Promise<DispatchResult> => {
311
+ if (PRESENT_ACTIONS.has(action)) {
312
+ return this.handleOpen(sceneId, action, params, next);
313
+ }
314
+ if (CLOSE_ACTIONS.has(action)) {
315
+ return this.handleClose(sceneId, action, params, next);
316
+ }
317
+ return next(sceneId, action, params);
318
+ };
319
+
320
+ private async handleOpen(
321
+ ownerSceneId: string,
322
+ action: string,
323
+ params: DispatchParams,
324
+ next: (sceneId: string, action: string, params: DispatchParams) => Promise<DispatchResult>,
325
+ ): Promise<DispatchResult> {
326
+ await this.awaitPendingClosesForOwner(ownerSceneId);
327
+ const result = await next(ownerSceneId, action, params);
328
+ if (result.ok && result.sceneId) {
329
+ this.presentingOwnerByChild.set(result.sceneId, ownerSceneId);
330
+ }
331
+ return result;
332
+ }
333
+
334
+ private async handleClose(
335
+ sceneId: string,
336
+ action: string,
337
+ params: DispatchParams,
338
+ next: (sceneId: string, action: string, params: DispatchParams) => Promise<DispatchResult>,
339
+ ): Promise<DispatchResult> {
340
+ const explicitTarget: CloseTarget | undefined =
341
+ params.target ?? (action === 'close' ? 'self' : undefined);
342
+ const onCovered: CoveredPolicy = params.onCovered ?? 'fail';
343
+
344
+ if (explicitTarget === 'presented') {
345
+ const childSceneId = await this.findTopmostDirectPresentedExitSceneId(sceneId);
346
+ if (childSceneId) {
347
+ const childMode = lifecycleHandler.getSceneInfo(childSceneId)?.mode;
348
+ const childAction = childMode === 'present' ? 'dismiss' : 'hideModal';
349
+ return this.runDeduplicatedSelfClose(
350
+ childSceneId,
351
+ childAction,
352
+ { ...params, target: 'self' },
353
+ onCovered,
354
+ next,
355
+ );
356
+ }
357
+ return next(sceneId, action, params);
358
+ }
359
+
360
+ if (explicitTarget === 'self') {
361
+ return this.runDeduplicatedSelfClose(sceneId, action, params, onCovered, next);
362
+ }
363
+
364
+ // Legacy hideModal() / dismiss() without explicit target:
365
+ // If covered by a presented child, keep v5.0 native semantics (dismiss child).
366
+ // Only coordinate self exit when not covered and an exit handler is registered.
367
+ if (this.exitHandlers.has(sceneId) && !(await this.isSceneCovered(sceneId, false))) {
368
+ return this.runDeduplicatedSelfClose(sceneId, action, params, onCovered, next);
369
+ }
370
+
371
+ return next(sceneId, action, params);
372
+ }
373
+
374
+ private async runDeduplicatedSelfClose(
375
+ sceneId: string,
376
+ action: string,
377
+ params: DispatchParams,
378
+ onCovered: CoveredPolicy,
379
+ next: (sceneId: string, action: string, params: DispatchParams) => Promise<DispatchResult>,
380
+ ): Promise<DispatchResult> {
381
+ const existing = this.closingScenes.get(sceneId);
382
+ if (existing) {
383
+ if (existing.deferred) {
384
+ if (onCovered === 'fail') {
385
+ return {
386
+ ok: false,
387
+ code: 'covered',
388
+ message: 'Target scene is covered by another scene.',
389
+ };
390
+ }
391
+ if (params.result !== undefined) {
392
+ existing.nativeParams = {
393
+ ...existing.nativeParams,
394
+ result: params.result,
395
+ };
396
+ }
397
+ }
398
+ return existing.promise;
399
+ }
400
+
401
+ const nativeParams: DispatchParams = { ...params, target: 'self' };
402
+ delete nativeParams.onCovered;
403
+
404
+ const entry: PendingCloseEntry = {
405
+ promise: Promise.resolve({ ok: false, code: 'invalid_action' }),
406
+ deferred: false,
407
+ nativeParams,
408
+ };
409
+ const pending = this.executeSelfClose(sceneId, action, onCovered, entry, next);
410
+ entry.promise = pending;
411
+ this.closingScenes.set(sceneId, entry);
412
+ try {
413
+ return await pending;
414
+ } finally {
415
+ if (this.closingScenes.get(sceneId) === entry) {
416
+ this.closingScenes.delete(sceneId);
417
+ }
418
+ }
419
+ }
420
+
421
+ private async waitForDeferredUncover(
422
+ sceneId: string,
423
+ entry: PendingCloseEntry,
424
+ includeStackCover: boolean,
425
+ ): Promise<DeferredUncoverResult> {
426
+ entry.deferred = true;
427
+ try {
428
+ return await this.waitUntilUncovered(sceneId, includeStackCover);
429
+ } finally {
430
+ entry.deferred = false;
431
+ }
432
+ }
433
+
434
+ private async executeSelfClose(
435
+ sceneId: string,
436
+ action: string,
437
+ onCovered: CoveredPolicy,
438
+ entry: PendingCloseEntry,
439
+ next: (sceneId: string, action: string, params: DispatchParams) => Promise<DispatchResult>,
440
+ ): Promise<DispatchResult> {
441
+ const includeStackCover = action === 'close';
442
+ let immediateCoveredRetries = 0;
443
+
444
+ while (true) {
445
+ if (lifecycleHandler.getSceneState(sceneId) === 'destroyed') {
446
+ return { ok: false, code: 'scene_not_found', message: 'Target scene is already destroyed.' };
447
+ }
448
+
449
+ if (await this.isSceneCovered(sceneId, includeStackCover)) {
450
+ if (onCovered === 'defer') {
451
+ const waitResult = await this.waitForDeferredUncover(
452
+ sceneId,
453
+ entry,
454
+ includeStackCover,
455
+ );
456
+ if (!waitResult.uncovered) {
457
+ return { ok: false, code: 'scene_not_found' };
458
+ }
459
+ immediateCoveredRetries = 0;
460
+ continue;
461
+ }
462
+ return {
463
+ ok: false,
464
+ code: 'covered',
465
+ message: 'Target scene is covered by another scene.',
466
+ };
467
+ }
468
+
469
+ const registration = this.exitHandlers.get(sceneId);
470
+ if (registration) {
471
+ try {
472
+ await this.runExitWithTimeout(registration);
473
+ } catch (error) {
474
+ registration.onRestore?.('exit_error');
475
+ return {
476
+ ok: false,
477
+ code: 'invalid_action',
478
+ message: error instanceof Error ? error.message : String(error),
479
+ };
480
+ }
481
+
482
+ if (await this.isSceneCovered(sceneId, includeStackCover)) {
483
+ registration.onRestore?.('covered');
484
+ if (onCovered === 'defer') {
485
+ const waitResult = await this.waitForDeferredUncover(
486
+ sceneId,
487
+ entry,
488
+ includeStackCover,
489
+ );
490
+ if (!waitResult.uncovered) {
491
+ return { ok: false, code: 'scene_not_found' };
492
+ }
493
+ immediateCoveredRetries = 0;
494
+ continue;
495
+ }
496
+ return {
497
+ ok: false,
498
+ code: 'covered',
499
+ message: 'Target scene became covered during exit.',
500
+ };
501
+ }
502
+ }
503
+
504
+ const result = await next(sceneId, action, entry.nativeParams);
505
+ if (result.ok) {
506
+ this.presentingOwnerByChild.delete(sceneId);
507
+ return result;
508
+ }
509
+
510
+ registration?.onRestore?.(result.code);
511
+ if (result.code === 'covered' && onCovered === 'defer') {
512
+ const waitResult = await this.waitForDeferredUncover(
513
+ sceneId,
514
+ entry,
515
+ includeStackCover,
516
+ );
517
+ if (!waitResult.uncovered) {
518
+ return { ok: false, code: 'scene_not_found' };
519
+ }
520
+ if (waitResult.waited) {
521
+ immediateCoveredRetries = 0;
522
+ } else {
523
+ immediateCoveredRetries += 1;
524
+ if (immediateCoveredRetries > MAX_IMMEDIATE_COVERED_RETRIES) {
525
+ return result;
526
+ }
527
+ }
528
+ continue;
529
+ }
530
+ return result;
531
+ }
532
+ }
533
+
534
+ onSceneDestroyed(sceneId: string) {
535
+ this.exitHandlers.delete(sceneId);
536
+ const waiters = this.uncoveredWaiters.get(sceneId);
537
+ if (waiters) {
538
+ this.uncoveredWaiters.delete(sceneId);
539
+ waiters.forEach(resolve => resolve({ uncovered: false, waited: false }));
540
+ }
541
+
542
+ this.presentingOwnerByChild.delete(sceneId);
543
+ for (const [childId, ownerId] of this.presentingOwnerByChild) {
544
+ if (ownerId === sceneId) {
545
+ this.presentingOwnerByChild.delete(childId);
546
+ }
547
+ }
548
+ }
549
+
550
+ onRootReset() {
551
+ for (const waiters of this.uncoveredWaiters.values()) {
552
+ waiters.forEach(resolve => resolve({ uncovered: false, waited: false }));
553
+ }
554
+ this.uncoveredWaiters.clear();
555
+ this.exitHandlers.clear();
556
+ this.presentingOwnerByChild.clear();
557
+ }
558
+ }
559
+
560
+ export const presentationCoordinator = new PresentationCoordinator();
@@ -129,22 +129,36 @@ export default class ResultEventHandler {
129
129
  return { promise, cancel };
130
130
  }
131
131
 
132
- setResult<T extends ResultMap = ResultMap>(
133
- sceneId: string,
134
- resultCode: number,
135
- data: T | null = null,
136
- ): void {
137
- if (resultCode < RESULT_OK) {
132
+ normalizePendingResult<T extends ResultMap = ResultMap>(pending: {
133
+ code: number;
134
+ data?: T | null;
135
+ }): { code: number; data: T | null } {
136
+ const { code } = pending;
137
+ const data = pending.data ?? null;
138
+ if (code < RESULT_OK) {
138
139
  console.warn(
139
140
  '`resultCode` can only be `RESULT_OK`, `RESULT_CANCEL` or an integer greater than 0.',
140
141
  );
141
142
  }
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
+ }
142
148
  if (data !== null && (typeof data !== 'object' || Array.isArray(data))) {
143
149
  console.warn('`setResult` data must be a plain object or null.');
144
- NativeNavigation.setResult(sceneId, resultCode, null);
145
- return;
150
+ return { code, data: null };
146
151
  }
147
- NativeNavigation.setResult(sceneId, resultCode, data);
152
+ return { code, data };
153
+ }
154
+
155
+ setResult<T extends ResultMap = ResultMap>(
156
+ sceneId: string,
157
+ resultCode: number,
158
+ data: T | null = null,
159
+ ): void {
160
+ const normalized = this.normalizePendingResult({ code: resultCode, data });
161
+ NativeNavigation.setResult(sceneId, normalized.code, normalized.data);
148
162
  }
149
163
 
150
164
  waitResult<T = unknown>(sceneId: string, requestCode: number): Promise<NavigationResult<T>> {
@@ -0,0 +1,31 @@
1
+ import NativeEvent from '../NativeEvent';
2
+ import type { TabPressEvent, TabPressListener } from '../Route';
3
+
4
+ export default class TabEventHandler {
5
+ private listeners: TabPressListener[] = [];
6
+
7
+ handleTabPress() {
8
+ NativeEvent.onTabPress(({ index, tabKey, previousIndex, reselected }) => {
9
+ const event: TabPressEvent = {
10
+ index,
11
+ tabKey,
12
+ previousIndex,
13
+ reselected: Boolean(reselected),
14
+ };
15
+ this.listeners.slice().forEach(listener => listener(event));
16
+ });
17
+ }
18
+
19
+ addListener(listener: TabPressListener): () => void {
20
+ this.listeners.push(listener);
21
+ return () => {
22
+ this.listeners = this.listeners.filter(item => item !== listener);
23
+ };
24
+ }
25
+
26
+ clearListeners() {
27
+ this.listeners = [];
28
+ }
29
+ }
30
+
31
+ export const tabEventHandler = new TabEventHandler();
package/src/hooks.ts CHANGED
@@ -1,7 +1,12 @@
1
1
  import { useContext, useEffect, useEffectEvent, useState } from 'react';
2
2
  import Navigation from './Navigation';
3
3
  import { Navigator, NavigationContext } from './Navigator';
4
- import type { RouteName, SceneLifecycleState } from './Route';
4
+ import type {
5
+ BeforeRemoveListener,
6
+ BeforeRemoveOptions,
7
+ RouteName,
8
+ SceneLifecycleState,
9
+ } from './Route';
5
10
 
6
11
  export type VisibleEffectCallback = () => void | (() => void);
7
12
 
@@ -34,6 +39,37 @@ export function useSceneState(): SceneLifecycleState {
34
39
  return state;
35
40
  }
36
41
 
42
+ export function useIsVisible(): boolean {
43
+ return useSceneState() === 'visible';
44
+ }
45
+
46
+ export function useBeforeRemove(
47
+ enabled: boolean,
48
+ onBeforeRemove: BeforeRemoveListener,
49
+ options?: BeforeRemoveOptions,
50
+ ) {
51
+ const navigator = useNavigator();
52
+ const eventCallback = useEffectEvent(onBeforeRemove);
53
+ const actionsKey = options?.actions ? options.actions.join(',') : '';
54
+
55
+ useEffect(() => {
56
+ if (!enabled) {
57
+ return;
58
+ }
59
+ const actions = actionsKey
60
+ ? (actionsKey.split(',') as NonNullable<BeforeRemoveOptions['actions']>)
61
+ : undefined;
62
+ const subscription = Navigation.addBeforeRemoveListener(
63
+ navigator.sceneId,
64
+ event => eventCallback(event),
65
+ actions ? { actions } : undefined,
66
+ );
67
+ return () => {
68
+ subscription.remove();
69
+ };
70
+ }, [enabled, navigator.sceneId, actionsKey]);
71
+ }
72
+
37
73
  export function useVisibleEffect(callback: VisibleEffectCallback) {
38
74
  const navigator = useNavigator();
39
75
  const event = useEffectEvent(callback);
package/src/index.ts CHANGED
@@ -6,6 +6,14 @@ 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
+
9
17
  /**
10
18
  * Augment this interface via `declare module 'hybrid-navigation'` to register
11
19
  * strongly typed custom container layouts alongside built-in layouts.
@@ -36,6 +44,9 @@ export type { NavigationProps } from './NavigationComponentWrap';
36
44
  export * from './Navigator';
37
45
  export * from './hooks';
38
46
  export * from './router';
47
+ export * from './routeGraph';
48
+ export * from './reveal';
49
+ export * from './defineRoutes';
39
50
  export * from './DeepLink';
40
51
  export * from './Route';
41
52
  export * from './Options';