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