hybrid-navigation 5.1.0 → 5.2.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.
- package/dist/Navigation.d.ts +3 -1
- package/dist/Navigation.js +13 -2
- package/dist/Navigator.js +1 -0
- package/dist/Route.d.ts +4 -0
- package/dist/handler/DispatchCommandHandler.d.ts +1 -0
- package/dist/handler/DispatchCommandHandler.js +2 -0
- package/dist/handler/LeaveGuardHandler.d.ts +1 -1
- package/dist/handler/LeaveGuardHandler.js +33 -16
- package/dist/handler/PresentationCoordinator.d.ts +16 -1
- package/dist/handler/PresentationCoordinator.js +246 -22
- package/dist/hooks.d.ts +4 -1
- package/dist/hooks.js +43 -1
- package/dist/router/stack.d.ts +1 -1
- package/dist/testing.d.ts +3 -0
- package/dist/testing.js +2 -0
- package/ios/Navigator/HBDScreenNavigator.mm +84 -11
- package/ios/ViewController/UIViewController+HBD.mm +4 -0
- package/package.json +1 -1
- package/src/Navigation.ts +26 -3
- package/src/Navigator.ts +2 -1
- package/src/Route.ts +6 -0
- package/src/handler/DispatchCommandHandler.ts +2 -0
- package/src/handler/LeaveGuardHandler.ts +35 -17
- package/src/handler/PresentationCoordinator.ts +269 -23
- package/src/hooks.ts +53 -1
- package/src/reveal.ts +2 -2
- package/src/router/stack.ts +1 -1
- package/src/testing.ts +7 -0
package/dist/hooks.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { useContext, useEffect, useEffectEvent, useState } from 'react';
|
|
1
|
+
import { useContext, useEffect, useEffectEvent, useLayoutEffect, useState } from 'react';
|
|
2
2
|
import Navigation from './Navigation';
|
|
3
3
|
import { NavigationContext } from './Navigator';
|
|
4
|
+
import { presentationCoordinator } from './handler/PresentationCoordinator';
|
|
4
5
|
export function useNavigator() {
|
|
5
6
|
const navigator = useContext(NavigationContext);
|
|
6
7
|
if (!navigator) {
|
|
@@ -25,6 +26,47 @@ export function useSceneState() {
|
|
|
25
26
|
export function useIsVisible() {
|
|
26
27
|
return useSceneState() === 'visible';
|
|
27
28
|
}
|
|
29
|
+
export function useSceneCovered() {
|
|
30
|
+
const navigator = useNavigator();
|
|
31
|
+
const [covered, setCovered] = useState(() => presentationCoordinator.isSceneVisuallyCoveredSync(navigator.sceneId));
|
|
32
|
+
useEffect(() => {
|
|
33
|
+
setCovered(presentationCoordinator.isSceneVisuallyCoveredSync(navigator.sceneId));
|
|
34
|
+
const subscription = Navigation.addSceneCoverageListener(navigator.sceneId, next => {
|
|
35
|
+
setCovered(next);
|
|
36
|
+
});
|
|
37
|
+
return () => {
|
|
38
|
+
subscription.remove();
|
|
39
|
+
};
|
|
40
|
+
}, [navigator.sceneId]);
|
|
41
|
+
return covered;
|
|
42
|
+
}
|
|
43
|
+
export function useIsTopmost() {
|
|
44
|
+
const state = useSceneState();
|
|
45
|
+
const covered = useSceneCovered();
|
|
46
|
+
return state === 'visible' && !covered;
|
|
47
|
+
}
|
|
48
|
+
export function useSceneExitHandler(handler) {
|
|
49
|
+
const navigator = useNavigator();
|
|
50
|
+
const enabled = Boolean(handler);
|
|
51
|
+
const exitTimeoutMs = handler?.exitTimeoutMs;
|
|
52
|
+
const onExit = useEffectEvent(() => handler?.onExit());
|
|
53
|
+
const onRestore = useEffectEvent((reason) => {
|
|
54
|
+
handler?.onRestore?.(reason);
|
|
55
|
+
});
|
|
56
|
+
useLayoutEffect(() => {
|
|
57
|
+
if (!enabled) {
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
const subscription = Navigation.registerSceneExitHandler(navigator.sceneId, {
|
|
61
|
+
onExit: () => onExit(),
|
|
62
|
+
onRestore: reason => onRestore(reason),
|
|
63
|
+
...(exitTimeoutMs !== undefined ? { exitTimeoutMs } : {}),
|
|
64
|
+
});
|
|
65
|
+
return () => {
|
|
66
|
+
subscription.remove();
|
|
67
|
+
};
|
|
68
|
+
}, [enabled, exitTimeoutMs, navigator.sceneId]);
|
|
69
|
+
}
|
|
28
70
|
export function useBeforeRemove(enabled, onBeforeRemove, options) {
|
|
29
71
|
const navigator = useNavigator();
|
|
30
72
|
const eventCallback = useEffectEvent(onBeforeRemove);
|
package/dist/router/stack.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { LayoutMode, RouteGraph, RouteHandler, RouteHandleResult, RouteInfo } from '../Route';
|
|
1
|
+
import type { LayoutMode, RouteGraph, RouteHandler, RouteHandleResult, RouteInfo } from '../Route';
|
|
2
2
|
export interface StackGraph extends RouteGraph {
|
|
3
3
|
layout: 'stack';
|
|
4
4
|
sceneId: string;
|
package/dist/testing.d.ts
CHANGED
|
@@ -58,6 +58,9 @@ export interface MockNavigationModule {
|
|
|
58
58
|
readonly useNavigator: typeof import('./hooks').useNavigator;
|
|
59
59
|
readonly useSceneState: typeof import('./hooks').useSceneState;
|
|
60
60
|
readonly useIsVisible: typeof import('./hooks').useIsVisible;
|
|
61
|
+
readonly useSceneCovered: typeof import('./hooks').useSceneCovered;
|
|
62
|
+
readonly useIsTopmost: typeof import('./hooks').useIsTopmost;
|
|
63
|
+
readonly useSceneExitHandler: typeof import('./hooks').useSceneExitHandler;
|
|
61
64
|
readonly useBeforeRemove: typeof import('./hooks').useBeforeRemove;
|
|
62
65
|
readonly useVisibleEffect: typeof import('./hooks').useVisibleEffect;
|
|
63
66
|
readonly withNavigationItem: typeof import('./NavigationComponentWrap').withNavigationItem;
|
package/dist/testing.js
CHANGED
|
@@ -153,6 +153,7 @@ export function createMockNavigation(options = {}) {
|
|
|
153
153
|
const { dispatchHandler, } = require('./handler/DispatchCommandHandler');
|
|
154
154
|
const { tabEventHandler, } = require('./handler/TabEventHandler');
|
|
155
155
|
const { layoutHandler, } = require('./handler/LayoutCommandHandler');
|
|
156
|
+
const { presentationCoordinator, } = require('./handler/PresentationCoordinator');
|
|
156
157
|
const storeMod = require('./store');
|
|
157
158
|
const navigation = navMod.default;
|
|
158
159
|
const defaultSceneId = options.defaultSceneId ?? 'scene-1';
|
|
@@ -186,6 +187,7 @@ export function createMockNavigation(options = {}) {
|
|
|
186
187
|
},
|
|
187
188
|
setRouteGraph(layers) {
|
|
188
189
|
activeState.routeGraph = layers;
|
|
190
|
+
presentationCoordinator.notifyRouteGraphChanged(layers);
|
|
189
191
|
},
|
|
190
192
|
setOnDispatch(fn) {
|
|
191
193
|
activeState.onDispatch = fn;
|
|
@@ -109,6 +109,38 @@
|
|
|
109
109
|
return vc.hbd_sceneId;
|
|
110
110
|
}
|
|
111
111
|
|
|
112
|
+
- (BOOL)waitForTransitionIfNeededForViewController:(nullable UIViewController *)vc
|
|
113
|
+
retryCount:(NSUInteger)retryCount
|
|
114
|
+
retry:(void (^)(NSUInteger nextRetryCount))retry {
|
|
115
|
+
if (!vc || (!vc.isBeingPresented && !vc.isBeingDismissed)) {
|
|
116
|
+
return NO;
|
|
117
|
+
}
|
|
118
|
+
HBDReactBridgeManager *manager = [HBDReactBridgeManager get];
|
|
119
|
+
NSUInteger commandId = manager.activeCommandId;
|
|
120
|
+
id<UIViewControllerTransitionCoordinator> coordinator = vc.transitionCoordinator;
|
|
121
|
+
if (coordinator) {
|
|
122
|
+
[coordinator animateAlongsideTransition:nil completion:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
|
|
123
|
+
dispatch_async(dispatch_get_main_queue(), ^{
|
|
124
|
+
if (commandId != 0 && manager.activeCommandId != commandId) {
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
retry(0);
|
|
128
|
+
});
|
|
129
|
+
}];
|
|
130
|
+
return YES;
|
|
131
|
+
}
|
|
132
|
+
if (retryCount >= 3) {
|
|
133
|
+
return NO;
|
|
134
|
+
}
|
|
135
|
+
dispatch_async(dispatch_get_main_queue(), ^{
|
|
136
|
+
if (commandId != 0 && manager.activeCommandId != commandId) {
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
retry(retryCount + 1);
|
|
140
|
+
});
|
|
141
|
+
return YES;
|
|
142
|
+
}
|
|
143
|
+
|
|
112
144
|
- (BOOL)waitForDismissalIfNeededForViewController:(UIViewController *)presenting retry:(dispatch_block_t)retry {
|
|
113
145
|
UIViewController *presented = presenting.presentedViewController;
|
|
114
146
|
if (!presented || !presented.isBeingDismissed) {
|
|
@@ -178,19 +210,20 @@
|
|
|
178
210
|
return host;
|
|
179
211
|
}
|
|
180
212
|
|
|
181
|
-
- (
|
|
213
|
+
- (BOOL)applyPendingResultIfPresentToViewController:(UIViewController *)vc extras:(NSDictionary *)extras {
|
|
182
214
|
NSDictionary *pendingResult = [extras[@"result"] isKindOfClass:[NSDictionary class]] ? extras[@"result"] : nil;
|
|
183
215
|
if (!pendingResult) {
|
|
184
|
-
return;
|
|
216
|
+
return NO;
|
|
185
217
|
}
|
|
186
218
|
id codeObj = pendingResult[@"code"];
|
|
187
219
|
if (![codeObj isKindOfClass:[NSNumber class]]) {
|
|
188
|
-
return;
|
|
220
|
+
return NO;
|
|
189
221
|
}
|
|
190
222
|
NSInteger resultCode = [codeObj integerValue];
|
|
191
223
|
id rawData = pendingResult[@"data"];
|
|
192
224
|
NSDictionary *resultData = [rawData isKindOfClass:[NSDictionary class]] ? rawData : nil;
|
|
193
225
|
[vc hbd_setResultCode:resultCode resultData:resultData];
|
|
226
|
+
return YES;
|
|
194
227
|
}
|
|
195
228
|
|
|
196
229
|
- (BOOL)viewController:(UIViewController *)viewController hasAncestorOrSelf:(nullable UIViewController *)ancestor {
|
|
@@ -206,9 +239,18 @@
|
|
|
206
239
|
}
|
|
207
240
|
|
|
208
241
|
- (void)handleCloseWithViewController:(UIViewController *)vc extras:(NSDictionary *)extras completion:(HBDDispatchCompletion)completion {
|
|
242
|
+
[self handleCloseWithViewController:vc extras:extras retryCount:0 completion:completion];
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
- (void)handleCloseWithViewController:(UIViewController *)vc extras:(NSDictionary *)extras retryCount:(NSUInteger)retryCount completion:(HBDDispatchCompletion)completion {
|
|
209
246
|
UIViewController *modalHost = [self modalHostViewControllerForViewController:vc];
|
|
210
247
|
if ([self waitForDismissalIfNeededForViewController:modalHost retry:^{
|
|
211
|
-
[self handleCloseWithViewController:vc extras:extras completion:completion];
|
|
248
|
+
[self handleCloseWithViewController:vc extras:extras retryCount:0 completion:completion];
|
|
249
|
+
}]) {
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
if ([self waitForTransitionIfNeededForViewController:modalHost retryCount:retryCount retry:^(NSUInteger nextRetryCount) {
|
|
253
|
+
[self handleCloseWithViewController:vc extras:extras retryCount:nextRetryCount completion:completion];
|
|
212
254
|
}]) {
|
|
213
255
|
return;
|
|
214
256
|
}
|
|
@@ -246,12 +288,22 @@
|
|
|
246
288
|
}
|
|
247
289
|
|
|
248
290
|
- (void)handleDismissWithViewController:(UIViewController *)vc extras:(NSDictionary *)extras completion:(HBDDispatchCompletion)completion {
|
|
291
|
+
[self handleDismissWithViewController:vc extras:extras retryCount:0 completion:completion];
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
- (void)handleDismissWithViewController:(UIViewController *)vc extras:(NSDictionary *)extras retryCount:(NSUInteger)retryCount completion:(HBDDispatchCompletion)completion {
|
|
249
295
|
UIViewController *modalHost = [self modalHostViewControllerForViewController:vc];
|
|
250
296
|
if ([self waitForDismissalIfNeededForViewController:modalHost retry:^{
|
|
251
|
-
[self handleDismissWithViewController:vc extras:extras completion:completion];
|
|
297
|
+
[self handleDismissWithViewController:vc extras:extras retryCount:0 completion:completion];
|
|
252
298
|
}]) {
|
|
253
299
|
return;
|
|
254
300
|
}
|
|
301
|
+
if ([self waitForTransitionIfNeededForViewController:modalHost retryCount:retryCount retry:^(NSUInteger nextRetryCount) {
|
|
302
|
+
[self handleDismissWithViewController:vc extras:extras retryCount:nextRetryCount completion:completion];
|
|
303
|
+
}]) {
|
|
304
|
+
return;
|
|
305
|
+
}
|
|
306
|
+
|
|
255
307
|
BOOL isModal = modalHost.modalPresentationStyle == UIModalPresentationOverFullScreen;
|
|
256
308
|
BOOL animated = !isModal;
|
|
257
309
|
if (!isModal && [vc isKindOfClass:[HBDViewController class]]) {
|
|
@@ -286,16 +338,37 @@
|
|
|
286
338
|
return;
|
|
287
339
|
}
|
|
288
340
|
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
341
|
+
BOOL dismissPresented = [target isEqualToString:@"presented"] || (target == nil && hasActivePresented);
|
|
342
|
+
if (dismissPresented && [self waitForTransitionIfNeededForViewController:presented retryCount:retryCount retry:^(NSUInteger nextRetryCount) {
|
|
343
|
+
[self handleDismissWithViewController:vc extras:extras retryCount:nextRetryCount completion:completion];
|
|
344
|
+
}]) {
|
|
345
|
+
return;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
UIViewController *dismissedTarget = dismissPresented ? presented : modalHost;
|
|
349
|
+
UIViewController *dismissReceiver = dismissPresented ? modalHost : ((presenting.presentedViewController == modalHost) ? presenting : modalHost);
|
|
350
|
+
UIViewController *revealed = dismissPresented ? modalHost : presenting;
|
|
351
|
+
BOOL dismissAnimated = dismissPresented ? (presented.modalPresentationStyle != UIModalPresentationOverFullScreen) : animated;
|
|
293
352
|
|
|
294
|
-
|
|
295
|
-
|
|
353
|
+
NSInteger previousResultCode = vc.hbd_resultCode;
|
|
354
|
+
NSDictionary *previousResultData = vc.hbd_resultData;
|
|
355
|
+
BOOL appliedPendingResult = NO;
|
|
356
|
+
if (!dismissPresented) {
|
|
357
|
+
appliedPendingResult = [self applyPendingResultIfPresentToViewController:vc extras:extras];
|
|
296
358
|
}
|
|
297
359
|
|
|
360
|
+
__weak UIViewController *weakDismissedTarget = dismissedTarget;
|
|
298
361
|
[dismissReceiver dismissViewControllerAnimated:dismissAnimated completion:^{
|
|
362
|
+
UIViewController *remainingTarget = weakDismissedTarget;
|
|
363
|
+
if (remainingTarget != nil && remainingTarget.presentingViewController != nil) {
|
|
364
|
+
if (appliedPendingResult) {
|
|
365
|
+
[vc hbd_setResultCode:previousResultCode resultData:previousResultData];
|
|
366
|
+
}
|
|
367
|
+
RCTLogWarn(@"[Navigation] dismissViewControllerAnimated completed but target scene is still presented. sceneId=%@", vc.hbd_sceneId);
|
|
368
|
+
NSString *code = remainingTarget.presentedViewController != nil ? HBDDispatchCodeCovered : HBDDispatchCodeNotReady;
|
|
369
|
+
completion(HBDDispatchResultFail(code, @"Target scene was not dismissed."));
|
|
370
|
+
return;
|
|
371
|
+
}
|
|
299
372
|
if (revealed != nil) {
|
|
300
373
|
HBDViewController *hbdvc = [[HBDReactBridgeManager get] primaryViewControllerWithViewController:revealed];
|
|
301
374
|
if (hbdvc != nil) {
|
|
@@ -79,6 +79,10 @@ static BOOL HBDIsManagedViewController(UIViewController * _Nullable vc) {
|
|
|
79
79
|
completion();
|
|
80
80
|
}
|
|
81
81
|
|
|
82
|
+
if (presented.presentingViewController != nil) {
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
|
|
82
86
|
BOOL consumed = presented.hbd_presentingSceneId.length > 0 &&
|
|
83
87
|
[UIViewController hbd_dispatchResult:presenting presented:presented sceneId:presented.hbd_presentingSceneId];
|
|
84
88
|
if (!consumed) {
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hybrid-navigation",
|
|
3
3
|
"description": "React Native Navigation that supports seamless navigation between Native and React.",
|
|
4
|
-
"version": "5.
|
|
4
|
+
"version": "5.2.0",
|
|
5
5
|
"main": "./dist/index",
|
|
6
6
|
"module": "./dist/index",
|
|
7
7
|
"types": "./dist/index.d.ts",
|
package/src/Navigation.ts
CHANGED
|
@@ -16,6 +16,7 @@ import type {
|
|
|
16
16
|
RouteConfig,
|
|
17
17
|
RouteGraph,
|
|
18
18
|
RouteName,
|
|
19
|
+
SceneCoverageListener,
|
|
19
20
|
SceneExitHandler,
|
|
20
21
|
SceneInfo,
|
|
21
22
|
SceneLifecycleState,
|
|
@@ -24,7 +25,7 @@ import type {
|
|
|
24
25
|
} from './Route';
|
|
25
26
|
|
|
26
27
|
import { layoutHandler, RootLayoutListener } from './handler/LayoutCommandHandler';
|
|
27
|
-
import { dispatchHandler } from './handler/DispatchCommandHandler';
|
|
28
|
+
import { BEFORE_REMOVE_SOURCE_PROP, dispatchHandler } from './handler/DispatchCommandHandler';
|
|
28
29
|
import { leaveGuardHandler } from './handler/LeaveGuardHandler';
|
|
29
30
|
import { presentationCoordinator } from './handler/PresentationCoordinator';
|
|
30
31
|
import { tabEventHandler } from './handler/TabEventHandler';
|
|
@@ -110,6 +111,11 @@ export interface Navigation {
|
|
|
110
111
|
sceneId: string,
|
|
111
112
|
handler: SceneExitHandler,
|
|
112
113
|
): NavigationSubscription;
|
|
114
|
+
addSceneCoverageListener(
|
|
115
|
+
sceneId: string,
|
|
116
|
+
listener: SceneCoverageListener,
|
|
117
|
+
): NavigationSubscription;
|
|
118
|
+
isSceneCovered(sceneId: string): Promise<boolean>;
|
|
113
119
|
addBeforeRemoveListener(
|
|
114
120
|
sceneId: string,
|
|
115
121
|
listener: BeforeRemoveListener,
|
|
@@ -382,7 +388,11 @@ class NavigationImpl implements Navigation {
|
|
|
382
388
|
}
|
|
383
389
|
|
|
384
390
|
dispatch(sceneId: string, action: string, params: DispatchParams = {}) {
|
|
385
|
-
|
|
391
|
+
const nextParams =
|
|
392
|
+
params[BEFORE_REMOVE_SOURCE_PROP] !== undefined
|
|
393
|
+
? params
|
|
394
|
+
: { ...params, [BEFORE_REMOVE_SOURCE_PROP]: 'external' };
|
|
395
|
+
return dispatchHandler.dispatch(sceneId, action, nextParams);
|
|
386
396
|
}
|
|
387
397
|
|
|
388
398
|
addInterceptor(interceptor: NavigationInterceptor): NavigationSubscription {
|
|
@@ -400,6 +410,18 @@ class NavigationImpl implements Navigation {
|
|
|
400
410
|
return { remove };
|
|
401
411
|
}
|
|
402
412
|
|
|
413
|
+
addSceneCoverageListener(
|
|
414
|
+
sceneId: string,
|
|
415
|
+
listener: SceneCoverageListener,
|
|
416
|
+
): NavigationSubscription {
|
|
417
|
+
const remove = presentationCoordinator.addCoverageListener(sceneId, listener);
|
|
418
|
+
return { remove };
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
isSceneCovered(sceneId: string): Promise<boolean> {
|
|
422
|
+
return presentationCoordinator.isSceneVisuallyCovered(sceneId);
|
|
423
|
+
}
|
|
424
|
+
|
|
403
425
|
addBeforeRemoveListener(
|
|
404
426
|
sceneId: string,
|
|
405
427
|
listener: BeforeRemoveListener,
|
|
@@ -414,8 +436,9 @@ class NavigationImpl implements Navigation {
|
|
|
414
436
|
options?: CloseSelfOptions<T>,
|
|
415
437
|
): Promise<DispatchResult> {
|
|
416
438
|
return this.dispatch(sceneId, 'close', {
|
|
417
|
-
target: 'self',
|
|
439
|
+
target: 'self' as const,
|
|
418
440
|
onCovered: options?.onCovered,
|
|
441
|
+
...(options?.bypassBeforeRemove ? { bypassBeforeRemove: true } : {}),
|
|
419
442
|
...(options?.result !== undefined ? { result: options.result } : {}),
|
|
420
443
|
});
|
|
421
444
|
}
|
package/src/Navigator.ts
CHANGED
|
@@ -197,8 +197,9 @@ export class Navigator<CurrentRoute extends RouteName = RouteName> {
|
|
|
197
197
|
): Promise<DispatchResult> => {
|
|
198
198
|
return dispatchHandler.dispatch(this.sceneId, 'close', {
|
|
199
199
|
from: this.moduleName,
|
|
200
|
-
target: 'self',
|
|
200
|
+
target: 'self' as const,
|
|
201
201
|
onCovered: options?.onCovered,
|
|
202
|
+
...(options?.bypassBeforeRemove ? { bypassBeforeRemove: true } : {}),
|
|
202
203
|
...(options?.result !== undefined ? { result: options.result } : {}),
|
|
203
204
|
});
|
|
204
205
|
};
|
package/src/Route.ts
CHANGED
|
@@ -50,6 +50,8 @@ export type CloseTarget = 'self' | 'presented';
|
|
|
50
50
|
|
|
51
51
|
export type CoveredPolicy = 'fail' | 'defer';
|
|
52
52
|
|
|
53
|
+
export type SceneCoverageListener = (covered: boolean) => void;
|
|
54
|
+
|
|
53
55
|
export type SceneExitRestoreReason = DispatchResultCode | 'exit_error';
|
|
54
56
|
|
|
55
57
|
export interface SceneExitHandler {
|
|
@@ -65,6 +67,7 @@ export interface CloseSelfOptions<T extends ResultMap = ResultMap> {
|
|
|
65
67
|
data?: T | null;
|
|
66
68
|
};
|
|
67
69
|
onCovered?: CoveredPolicy;
|
|
70
|
+
bypassBeforeRemove?: boolean;
|
|
68
71
|
}
|
|
69
72
|
|
|
70
73
|
export type LeaveAction =
|
|
@@ -77,9 +80,12 @@ export type LeaveAction =
|
|
|
77
80
|
| 'close'
|
|
78
81
|
| 'redirectTo';
|
|
79
82
|
|
|
83
|
+
export type BeforeRemoveSource = 'back' | 'self' | 'external';
|
|
84
|
+
|
|
80
85
|
export interface BeforeRemoveEvent {
|
|
81
86
|
sceneId: string;
|
|
82
87
|
action: LeaveAction;
|
|
88
|
+
source: BeforeRemoveSource;
|
|
83
89
|
proceed: () => Promise<DispatchResult>;
|
|
84
90
|
}
|
|
85
91
|
|
|
@@ -11,8 +11,10 @@ import type {
|
|
|
11
11
|
import { resultHandler } from './ResultEventHandler';
|
|
12
12
|
|
|
13
13
|
const INTERCEPT_CHECKED_PROP = '__hbdInterceptChecked';
|
|
14
|
+
export const BEFORE_REMOVE_SOURCE_PROP = '__hbdBeforeRemoveSource';
|
|
14
15
|
const INTERNAL_DISPATCH_PROPS = [
|
|
15
16
|
INTERCEPT_CHECKED_PROP,
|
|
17
|
+
BEFORE_REMOVE_SOURCE_PROP,
|
|
16
18
|
'onCovered',
|
|
17
19
|
'bypassBeforeRemove',
|
|
18
20
|
'__hbdBypassBeforeRemove',
|
|
@@ -4,12 +4,13 @@ import type {
|
|
|
4
4
|
BeforeRemoveEvent,
|
|
5
5
|
BeforeRemoveListener,
|
|
6
6
|
BeforeRemoveOptions,
|
|
7
|
+
BeforeRemoveSource,
|
|
7
8
|
DispatchResult,
|
|
8
9
|
LeaveAction,
|
|
9
10
|
RouteGraph,
|
|
10
11
|
} from '../Route';
|
|
11
12
|
import { containsScene, findSceneInGraph } from '../routeGraph';
|
|
12
|
-
import type
|
|
13
|
+
import { BEFORE_REMOVE_SOURCE_PROP, type DispatchParams } from './DispatchCommandHandler';
|
|
13
14
|
import { lifecycleHandler } from './LifecycleEventHandler';
|
|
14
15
|
|
|
15
16
|
const GUARDED_ACTIONS = new Set<LeaveAction>([
|
|
@@ -25,6 +26,10 @@ const GUARDED_ACTIONS = new Set<LeaveAction>([
|
|
|
25
26
|
const BYPASS_PROP = '__hbdBypassBeforeRemove';
|
|
26
27
|
const APPROVED_SCENES_PROP = '__hbdApprovedBeforeRemoveScenes';
|
|
27
28
|
|
|
29
|
+
function isBeforeRemoveSource(value: unknown): value is BeforeRemoveSource {
|
|
30
|
+
return value === 'back' || value === 'self' || value === 'external';
|
|
31
|
+
}
|
|
32
|
+
|
|
28
33
|
function collectScreenIds(graph: RouteGraph, out: string[]): void {
|
|
29
34
|
if (graph.layout === 'screen') {
|
|
30
35
|
out.push(graph.sceneId);
|
|
@@ -77,34 +82,39 @@ export function collectRemovedStackScenes(
|
|
|
77
82
|
}
|
|
78
83
|
}
|
|
79
84
|
if (!stack || !Array.isArray(stack.children) || stack.children.length <= 1) {
|
|
80
|
-
return [
|
|
81
|
-
}
|
|
82
|
-
const fromIndex = stack.children.findIndex(child => containsScene(child, sceneId));
|
|
83
|
-
if (fromIndex <= 0) {
|
|
84
|
-
return [sceneId];
|
|
85
|
+
return [];
|
|
85
86
|
}
|
|
87
|
+
const topIndex = stack.children.length - 1;
|
|
86
88
|
let stopIndex = 0;
|
|
87
89
|
if (action === 'popTo') {
|
|
88
90
|
if (!targetModuleOrSceneId) {
|
|
89
|
-
return [
|
|
91
|
+
return [];
|
|
90
92
|
}
|
|
91
|
-
|
|
92
|
-
|
|
93
|
+
// Match native iOS/Android top-down lookup; router.open dispatches popTo on the target sceneId,
|
|
94
|
+
// so removal always starts from the stack top (topIndex), not from the dispatching sceneId.
|
|
95
|
+
let targetIndex = -1;
|
|
96
|
+
for (let i = topIndex; i >= 0; i--) {
|
|
97
|
+
const child = stack.children[i];
|
|
98
|
+
if (
|
|
93
99
|
child.sceneId === targetModuleOrSceneId ||
|
|
94
100
|
containsScene(child, targetModuleOrSceneId) ||
|
|
95
|
-
Boolean(findSceneInGraph(child, node => node.moduleName === targetModuleOrSceneId))
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
101
|
+
Boolean(findSceneInGraph(child, node => node.moduleName === targetModuleOrSceneId))
|
|
102
|
+
) {
|
|
103
|
+
targetIndex = i;
|
|
104
|
+
break;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
if (targetIndex < 0) {
|
|
108
|
+
return [];
|
|
99
109
|
}
|
|
100
110
|
// Match native iOS/Android behavior: inclusive && targetIndex > 0 pops targetIndex itself.
|
|
101
111
|
stopIndex = inclusive && targetIndex > 0 ? targetIndex - 1 : targetIndex;
|
|
102
112
|
}
|
|
103
113
|
const removed: string[] = [];
|
|
104
|
-
for (let i =
|
|
114
|
+
for (let i = topIndex; i > stopIndex; i--) {
|
|
105
115
|
collectScreenIds(stack.children[i], removed);
|
|
106
116
|
}
|
|
107
|
-
return removed
|
|
117
|
+
return removed;
|
|
108
118
|
}
|
|
109
119
|
|
|
110
120
|
function collectPresentedAboveScenes(layers: RouteGraph[], sceneId: string): string[] {
|
|
@@ -152,6 +162,7 @@ function matchesAction(entry: BeforeRemoveEntry, action: LeaveAction, sceneId?:
|
|
|
152
162
|
function runGuardedChain(
|
|
153
163
|
sceneId: string,
|
|
154
164
|
action: LeaveAction,
|
|
165
|
+
source: BeforeRemoveSource,
|
|
155
166
|
matching: BeforeRemoveEntry[],
|
|
156
167
|
executeFinalProceed: () => Promise<DispatchResult>,
|
|
157
168
|
): Promise<DispatchResult> {
|
|
@@ -171,6 +182,7 @@ function runGuardedChain(
|
|
|
171
182
|
const event: BeforeRemoveEvent = {
|
|
172
183
|
sceneId,
|
|
173
184
|
action,
|
|
185
|
+
source,
|
|
174
186
|
proceed: () => {
|
|
175
187
|
if (!stepProceedPromise) {
|
|
176
188
|
stepProceedPromise = runAt(index + 1);
|
|
@@ -250,7 +262,7 @@ export default class LeaveGuardHandler {
|
|
|
250
262
|
void proceedClose();
|
|
251
263
|
return;
|
|
252
264
|
}
|
|
253
|
-
runGuardedChain(sceneId, leaveAction, matching, proceedClose);
|
|
265
|
+
runGuardedChain(sceneId, leaveAction, 'back', matching, proceedClose);
|
|
254
266
|
});
|
|
255
267
|
}
|
|
256
268
|
|
|
@@ -391,8 +403,14 @@ export default class LeaveGuardHandler {
|
|
|
391
403
|
const isMultiScene = candidateSceneIds.length > 1;
|
|
392
404
|
const generation = this.rootGeneration;
|
|
393
405
|
const targetGuardedSceneId = guardedSceneId;
|
|
406
|
+
const explicitSource = params[BEFORE_REMOVE_SOURCE_PROP];
|
|
407
|
+
const source: BeforeRemoveSource = isBeforeRemoveSource(explicitSource)
|
|
408
|
+
? explicitSource
|
|
409
|
+
: targetGuardedSceneId !== sceneId
|
|
410
|
+
? 'external'
|
|
411
|
+
: 'self';
|
|
394
412
|
|
|
395
|
-
return runGuardedChain(targetGuardedSceneId, leaveAction, matching, async () => {
|
|
413
|
+
return runGuardedChain(targetGuardedSceneId, leaveAction, source, matching, async () => {
|
|
396
414
|
if (
|
|
397
415
|
generation !== this.rootGeneration ||
|
|
398
416
|
lifecycleHandler.getSceneState(targetGuardedSceneId) === 'destroyed'
|