hybrid-navigation 4.1.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.
- package/HybridNavigation.podspec +9 -1
- package/README.md +12 -4
- package/android/build.gradle +4 -4
- package/android/proguard-rules.pro +0 -14
- package/android/src/main/java/com/navigation/androidx/AwesomeActivity.java +116 -27
- package/android/src/main/java/com/navigation/androidx/AwesomeFragment.java +162 -13
- package/android/src/main/java/com/navigation/androidx/DefaultTabBarProvider.java +2 -2
- package/android/src/main/java/com/navigation/androidx/DrawableUtils.java +83 -18
- package/android/src/main/java/com/navigation/androidx/DrawerFragment.java +185 -25
- package/android/src/main/java/com/navigation/androidx/FragmentHelper.java +18 -0
- package/android/src/main/java/com/navigation/androidx/HybridDrawerLayout.java +563 -143
- package/android/src/main/java/com/navigation/androidx/LifecycleDelegate.java +39 -12
- package/android/src/main/java/com/navigation/androidx/PresentableActivity.java +3 -0
- package/android/src/main/java/com/navigation/androidx/PresentationDelegate.java +10 -7
- package/android/src/main/java/com/navigation/androidx/StackDelegate.java +29 -9
- package/android/src/main/java/com/navigation/androidx/StackFragment.java +31 -3
- package/android/src/main/java/com/navigation/androidx/SystemUI.java +59 -90
- package/android/src/main/java/com/navigation/androidx/TabBar.java +21 -2
- package/android/src/main/java/com/navigation/androidx/TabBarFragment.java +74 -7
- package/android/src/main/java/com/navigation/androidx/TabBarItem.java +4 -0
- package/android/src/main/java/com/navigation/androidx/TabView.java +11 -2
- package/android/src/main/java/com/reactnative/hybridnavigation/DispatchResult.java +71 -0
- package/android/src/main/java/com/reactnative/hybridnavigation/Garden.java +18 -10
- package/android/src/main/java/com/reactnative/hybridnavigation/GlobalStyle.java +34 -2
- package/android/src/main/java/com/reactnative/hybridnavigation/HBDRootView.java +20 -10
- package/android/src/main/java/com/reactnative/hybridnavigation/HybridFragment.java +179 -13
- package/android/src/main/java/com/reactnative/hybridnavigation/HybridNavigationPackage.java +13 -24
- package/android/src/main/java/com/reactnative/hybridnavigation/NativeEvent.java +5 -1
- package/android/src/main/java/com/reactnative/hybridnavigation/NativeNavigation.java +283 -183
- package/android/src/main/java/com/reactnative/hybridnavigation/NavigationCommandQueue.java +507 -0
- package/android/src/main/java/com/reactnative/hybridnavigation/Navigator.java +4 -1
- package/android/src/main/java/com/reactnative/hybridnavigation/OrientationCoordinator.java +162 -0
- package/android/src/main/java/com/reactnative/hybridnavigation/ReactAppCompatActivity.java +103 -66
- package/android/src/main/java/com/reactnative/hybridnavigation/ReactFragment.java +421 -92
- package/android/src/main/java/com/reactnative/hybridnavigation/ReactManager.java +227 -78
- package/android/src/main/java/com/reactnative/hybridnavigation/ReactStackFragment.java +6 -6
- package/android/src/main/java/com/reactnative/hybridnavigation/ReactTabBarFragment.java +26 -0
- package/android/src/main/java/com/reactnative/hybridnavigation/ReactTabBarProvider.java +8 -1
- package/android/src/main/java/com/reactnative/hybridnavigation/SceneLifecycleState.java +21 -0
- package/android/src/main/java/com/reactnative/hybridnavigation/SceneLifecycleStateMachine.java +116 -0
- package/android/src/main/java/com/reactnative/hybridnavigation/ScreenOrientation.java +42 -0
- package/android/src/main/java/com/reactnative/hybridnavigation/Utils.java +17 -0
- package/android/src/main/java/com/reactnative/hybridnavigation/navigator/DrawerNavigator.java +14 -6
- package/android/src/main/java/com/reactnative/hybridnavigation/navigator/ScreenNavigator.java +188 -26
- package/android/src/main/java/com/reactnative/hybridnavigation/navigator/StackNavigator.java +67 -16
- package/android/src/main/java/com/reactnative/hybridnavigation/navigator/TabNavigator.java +42 -14
- package/dist/DeepLink.d.ts +3 -2
- package/dist/DeepLink.js +37 -14
- package/dist/NativeEvent.d.ts +17 -10
- package/dist/NativeNavigation.d.ts +24 -18
- package/dist/Navigation.d.ts +34 -19
- package/dist/Navigation.js +225 -90
- package/dist/NavigationComponentWrap.d.ts +5 -3
- package/dist/NavigationComponentWrap.js +3 -6
- package/dist/Navigator.d.ts +32 -42
- package/dist/Navigator.js +117 -91
- package/dist/Options.d.ts +15 -3
- package/dist/Route.d.ts +149 -17
- package/dist/defineRoutes.d.ts +19 -0
- package/dist/defineRoutes.js +6 -0
- package/dist/handler/DispatchCommandHandler.d.ts +25 -6
- package/dist/handler/DispatchCommandHandler.js +99 -17
- package/dist/handler/LayoutCommandHandler.d.ts +8 -7
- package/dist/handler/LayoutCommandHandler.js +24 -15
- package/dist/handler/LeaveGuardHandler.d.ts +17 -0
- package/dist/handler/LeaveGuardHandler.js +350 -0
- package/dist/handler/LifecycleEventHandler.d.ts +30 -0
- package/dist/handler/LifecycleEventHandler.js +169 -0
- package/dist/handler/PresentationCoordinator.d.ts +24 -0
- package/dist/handler/PresentationCoordinator.js +449 -0
- package/dist/handler/ResultEventHandler.d.ts +21 -9
- package/dist/handler/ResultEventHandler.js +96 -13
- package/dist/handler/TabEventHandler.d.ts +8 -0
- package/dist/handler/TabEventHandler.js +25 -0
- package/dist/hooks.d.ts +7 -2
- package/dist/hooks.js +46 -4
- package/dist/index.d.ts +24 -0
- package/dist/index.js +3 -2
- package/dist/reveal.d.ts +46 -0
- package/dist/reveal.js +431 -0
- package/dist/routeConfigRegistry.d.ts +6 -0
- package/dist/routeConfigRegistry.js +19 -0
- package/dist/routeGraph.d.ts +12 -0
- package/dist/routeGraph.js +68 -0
- package/dist/router/drawer.d.ts +2 -2
- package/dist/router/drawer.js +6 -3
- package/dist/router/index.d.ts +8 -5
- package/dist/router/index.js +106 -58
- package/dist/router/screen.d.ts +2 -2
- package/dist/router/screen.js +1 -1
- package/dist/router/stack.d.ts +2 -2
- package/dist/router/stack.js +59 -30
- package/dist/router/tabs.d.ts +3 -2
- package/dist/router/tabs.js +4 -1
- package/dist/store.d.ts +4 -2
- package/dist/store.js +4 -0
- package/dist/testing.d.ts +79 -0
- package/dist/testing.js +251 -0
- package/ios/HBDGlobalStyle.h +29 -0
- package/ios/{GlobalStyle.mm → HBDGlobalStyle.mm} +43 -29
- package/ios/HybridNavigation.h +5 -1
- package/ios/Navigator/HBDDrawerNavigator.mm +24 -8
- package/ios/Navigator/HBDNavigatorRegistry.mm +1 -0
- package/ios/Navigator/HBDScreenNavigator.mm +278 -58
- package/ios/Navigator/HBDStackNavigator.mm +152 -61
- package/ios/Navigator/HBDTabNavigator.mm +60 -14
- package/ios/React/HBDNativeNavigation.mm +162 -123
- package/ios/React/HBDNavigator.h +37 -5
- package/ios/React/HBDOrientationCoordinator.h +29 -0
- package/ios/React/HBDOrientationCoordinator.mm +309 -0
- package/ios/React/HBDReactBridgeManager.h +46 -20
- package/ios/React/HBDReactBridgeManager.mm +496 -108
- package/ios/React/HBDSceneLifecycleStateMachine.h +38 -0
- package/ios/React/HBDSceneLifecycleStateMachine.mm +180 -0
- package/ios/View/HBDRootView.h +2 -0
- package/ios/View/HBDRootView.mm +148 -0
- package/ios/View/UIImage+WithBadge.h +3 -3
- package/ios/View/UIImage+WithBadge.mm +10 -15
- package/ios/ViewController/HBDDrawerController.mm +126 -5
- package/ios/ViewController/HBDNavigationController.mm +31 -4
- package/ios/ViewController/HBDReactViewController.h +6 -0
- package/ios/ViewController/HBDReactViewController.mm +220 -106
- package/ios/ViewController/HBDTabBarController.h +15 -0
- package/ios/ViewController/HBDTabBarController.mm +227 -45
- package/ios/ViewController/HBDTabBarRenderer.h +28 -0
- package/ios/ViewController/HBDViewController.h +11 -3
- package/ios/ViewController/HBDViewController.mm +209 -159
- package/ios/ViewController/UINavigationController+HBD.h +2 -2
- package/ios/ViewController/UINavigationController+HBD.mm +3 -3
- package/ios/ViewController/UIViewController+HBD.h +18 -10
- package/ios/ViewController/UIViewController+HBD.mm +90 -65
- package/package.json +3 -1
- package/src/DeepLink.ts +41 -14
- package/src/NativeEvent.ts +18 -11
- package/src/NativeNavigation.ts +29 -31
- package/src/Navigation.ts +377 -120
- package/src/NavigationComponentWrap.tsx +18 -9
- package/src/Navigator.ts +219 -152
- package/src/Options.ts +12 -3
- package/src/Route.ts +228 -17
- package/src/defineRoutes.ts +46 -0
- package/src/handler/DispatchCommandHandler.ts +154 -21
- package/src/handler/LayoutCommandHandler.ts +40 -17
- package/src/handler/LeaveGuardHandler.ts +436 -0
- package/src/handler/LifecycleEventHandler.ts +219 -0
- package/src/handler/PresentationCoordinator.ts +560 -0
- package/src/handler/ResultEventHandler.ts +126 -19
- package/src/handler/TabEventHandler.ts +31 -0
- package/src/hooks.ts +75 -7
- package/src/index.ts +37 -2
- package/src/reveal.ts +565 -0
- package/src/routeConfigRegistry.ts +26 -0
- package/src/routeGraph.ts +87 -0
- package/src/router/drawer.ts +8 -5
- package/src/router/index.ts +139 -63
- package/src/router/screen.ts +3 -3
- package/src/router/stack.ts +80 -29
- package/src/router/tabs.ts +16 -7
- package/src/store.ts +6 -1
- package/src/testing.ts +395 -0
- package/testing.d.ts +1 -0
- package/testing.js +3 -0
- package/android/src/main/java/androidx/fragment/app/InternalFragment.java +0 -22
- package/android/src/main/java/com/navigation/androidx/SystemUI30.java +0 -87
- package/android/src/main/java/com/reactnative/hybridnavigation/NativeGarden.java +0 -176
- package/android/src/main/java/com/reactnative/hybridnavigation/ReactTabBar.java +0 -130
- package/android/src/main/res/layout/nav_tab_bar_react.xml +0 -32
- package/android/src/main/res/layout/nav_tab_bar_react_indeterminate.xml +0 -32
- package/dist/NativeGarden.d.ts +0 -13
- package/dist/NativeGarden.js +0 -2
- package/dist/handler/VisibilityEventHandler.d.ts +0 -13
- package/dist/handler/VisibilityEventHandler.js +0 -39
- package/ios/GlobalStyle.h +0 -15
- package/ios/React/HBDNativeGarden.h +0 -10
- package/ios/React/HBDNativeGarden.mm +0 -111
- package/src/NativeGarden.ts +0 -16
- package/src/handler/VisibilityEventHandler.ts +0 -52
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import NativeEvent from '../NativeEvent';
|
|
2
|
+
const VALID_STATES = new Set([
|
|
3
|
+
'created',
|
|
4
|
+
'visible',
|
|
5
|
+
'invisible',
|
|
6
|
+
'destroyed',
|
|
7
|
+
]);
|
|
8
|
+
const VALID_MODES = new Set(['modal', 'present', 'normal']);
|
|
9
|
+
function normalizeLifecycleState(raw) {
|
|
10
|
+
if (VALID_STATES.has(raw)) {
|
|
11
|
+
return raw;
|
|
12
|
+
}
|
|
13
|
+
return 'created';
|
|
14
|
+
}
|
|
15
|
+
function normalizeLayoutMode(raw, fallback = 'normal') {
|
|
16
|
+
if (raw && VALID_MODES.has(raw)) {
|
|
17
|
+
return raw;
|
|
18
|
+
}
|
|
19
|
+
return fallback;
|
|
20
|
+
}
|
|
21
|
+
const MAX_DESTROYED_SCENES = 2000;
|
|
22
|
+
export default class LifecycleEventHandler {
|
|
23
|
+
sceneStates = new Map();
|
|
24
|
+
sceneInfos = new Map();
|
|
25
|
+
destroyedScenes = new Set();
|
|
26
|
+
lifecycleListeners = {};
|
|
27
|
+
globalLifecycleListeners = [];
|
|
28
|
+
visibilityListeners = {};
|
|
29
|
+
globalVisibilityListeners = [];
|
|
30
|
+
handleSceneLifecycle(onSceneDestroyed) {
|
|
31
|
+
NativeEvent.onSceneStateChange(({ sceneId, state, prevState, moduleName, mode, presentingId }) => {
|
|
32
|
+
const next = normalizeLifecycleState(state);
|
|
33
|
+
const prev = normalizeLifecycleState(prevState);
|
|
34
|
+
if (next !== 'destroyed') {
|
|
35
|
+
this.destroyedScenes.delete(sceneId);
|
|
36
|
+
}
|
|
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);
|
|
47
|
+
const visibilityChange = next === 'visible'
|
|
48
|
+
? 'visible'
|
|
49
|
+
: prev === 'visible' && (next === 'invisible' || next === 'destroyed')
|
|
50
|
+
? 'invisible'
|
|
51
|
+
: null;
|
|
52
|
+
if (visibilityChange) {
|
|
53
|
+
this.globalVisibilityListeners
|
|
54
|
+
.slice()
|
|
55
|
+
.forEach(listener => listener(sceneId, visibilityChange, info));
|
|
56
|
+
}
|
|
57
|
+
this.globalLifecycleListeners
|
|
58
|
+
.slice()
|
|
59
|
+
.forEach(listener => listener(sceneId, next, prev, info));
|
|
60
|
+
const sceneListeners = this.lifecycleListeners[sceneId];
|
|
61
|
+
if (sceneListeners) {
|
|
62
|
+
sceneListeners.slice().forEach(listener => listener(next, prev));
|
|
63
|
+
}
|
|
64
|
+
if (visibilityChange) {
|
|
65
|
+
const visListeners = this.visibilityListeners[sceneId];
|
|
66
|
+
if (visListeners) {
|
|
67
|
+
visListeners.slice().forEach(listener => listener(visibilityChange));
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
if (next === 'destroyed') {
|
|
71
|
+
this.markSceneDestroyed(sceneId);
|
|
72
|
+
onSceneDestroyed?.(sceneId);
|
|
73
|
+
this.cleanupScene(sceneId);
|
|
74
|
+
}
|
|
75
|
+
});
|
|
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
|
+
}
|
|
92
|
+
markSceneDestroyed(sceneId) {
|
|
93
|
+
this.destroyedScenes.delete(sceneId);
|
|
94
|
+
this.destroyedScenes.add(sceneId);
|
|
95
|
+
if (this.destroyedScenes.size > MAX_DESTROYED_SCENES) {
|
|
96
|
+
const oldest = this.destroyedScenes.values().next().value;
|
|
97
|
+
if (oldest !== undefined) {
|
|
98
|
+
this.destroyedScenes.delete(oldest);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
getSceneState(sceneId) {
|
|
103
|
+
const activeState = this.sceneStates.get(sceneId);
|
|
104
|
+
if (activeState !== undefined) {
|
|
105
|
+
return activeState;
|
|
106
|
+
}
|
|
107
|
+
if (this.destroyedScenes.has(sceneId)) {
|
|
108
|
+
return 'destroyed';
|
|
109
|
+
}
|
|
110
|
+
return 'created';
|
|
111
|
+
}
|
|
112
|
+
cleanupScene(sceneId) {
|
|
113
|
+
this.sceneStates.delete(sceneId);
|
|
114
|
+
this.sceneInfos.delete(sceneId);
|
|
115
|
+
delete this.lifecycleListeners[sceneId];
|
|
116
|
+
delete this.visibilityListeners[sceneId];
|
|
117
|
+
}
|
|
118
|
+
addLifecycleListener(sceneId, listener) {
|
|
119
|
+
if (!this.lifecycleListeners[sceneId]) {
|
|
120
|
+
this.lifecycleListeners[sceneId] = [];
|
|
121
|
+
}
|
|
122
|
+
this.lifecycleListeners[sceneId].push(listener);
|
|
123
|
+
}
|
|
124
|
+
removeLifecycleListener(sceneId, listener) {
|
|
125
|
+
const list = this.lifecycleListeners[sceneId];
|
|
126
|
+
if (!list) {
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
const next = list.filter(l => l !== listener);
|
|
130
|
+
if (next.length > 0) {
|
|
131
|
+
this.lifecycleListeners[sceneId] = next;
|
|
132
|
+
}
|
|
133
|
+
else {
|
|
134
|
+
delete this.lifecycleListeners[sceneId];
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
addGlobalLifecycleListener(listener) {
|
|
138
|
+
this.globalLifecycleListeners.push(listener);
|
|
139
|
+
}
|
|
140
|
+
removeGlobalLifecycleListener(listener) {
|
|
141
|
+
this.globalLifecycleListeners = this.globalLifecycleListeners.filter(l => l !== listener);
|
|
142
|
+
}
|
|
143
|
+
addGlobalVisibilityEventListener(listener) {
|
|
144
|
+
this.globalVisibilityListeners.push(listener);
|
|
145
|
+
}
|
|
146
|
+
removeGlobalVisibilityEventListener(listener) {
|
|
147
|
+
this.globalVisibilityListeners = this.globalVisibilityListeners.filter(l => l !== listener);
|
|
148
|
+
}
|
|
149
|
+
addVisibilityEventListener(sceneId, listener) {
|
|
150
|
+
if (!this.visibilityListeners[sceneId]) {
|
|
151
|
+
this.visibilityListeners[sceneId] = [];
|
|
152
|
+
}
|
|
153
|
+
this.visibilityListeners[sceneId].push(listener);
|
|
154
|
+
}
|
|
155
|
+
removeVisibilityEventListener(sceneId, listener) {
|
|
156
|
+
const list = this.visibilityListeners[sceneId];
|
|
157
|
+
if (!list) {
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
const next = list.filter(l => l !== listener);
|
|
161
|
+
if (next.length > 0) {
|
|
162
|
+
this.visibilityListeners[sceneId] = next;
|
|
163
|
+
}
|
|
164
|
+
else {
|
|
165
|
+
delete this.visibilityListeners[sceneId];
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
export const lifecycleHandler = new LifecycleEventHandler();
|
|
@@ -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;
|
|
@@ -0,0 +1,449 @@
|
|
|
1
|
+
import NativeNavigation from '../NativeNavigation';
|
|
2
|
+
import { containsScene, findSceneInGraph } from '../routeGraph';
|
|
3
|
+
import { lifecycleHandler } from './LifecycleEventHandler';
|
|
4
|
+
const PRESENT_ACTIONS = new Set([
|
|
5
|
+
'showModal',
|
|
6
|
+
'showModalLayout',
|
|
7
|
+
'present',
|
|
8
|
+
'presentLayout',
|
|
9
|
+
]);
|
|
10
|
+
const CLOSE_ACTIONS = new Set(['hideModal', 'dismiss', 'close']);
|
|
11
|
+
const DEFAULT_EXIT_TIMEOUT_MS = 1200;
|
|
12
|
+
const MAX_IMMEDIATE_COVERED_RETRIES = 1;
|
|
13
|
+
function findInnermostContainingStack(graph, sceneId) {
|
|
14
|
+
if (graph.layout === 'stack' && Array.isArray(graph.children)) {
|
|
15
|
+
for (const child of graph.children) {
|
|
16
|
+
if (!containsScene(child, sceneId)) {
|
|
17
|
+
continue;
|
|
18
|
+
}
|
|
19
|
+
const nested = findInnermostContainingStack(child, sceneId);
|
|
20
|
+
return nested ?? graph;
|
|
21
|
+
}
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
if (!Array.isArray(graph.children)) {
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
for (const child of graph.children) {
|
|
28
|
+
if (containsScene(child, sceneId)) {
|
|
29
|
+
return findInnermostContainingStack(child, sceneId);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
function isSceneCoveredInStack(graph, sceneId) {
|
|
35
|
+
const stack = findInnermostContainingStack(graph, sceneId);
|
|
36
|
+
if (!stack || !Array.isArray(stack.children) || stack.children.length <= 1) {
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
const childIndex = stack.children.findIndex(child => containsScene(child, sceneId));
|
|
40
|
+
return childIndex >= 0 && childIndex < stack.children.length - 1;
|
|
41
|
+
}
|
|
42
|
+
export default class PresentationCoordinator {
|
|
43
|
+
exitHandlers = new Map();
|
|
44
|
+
closingScenes = new Map();
|
|
45
|
+
presentingOwnerByChild = new Map();
|
|
46
|
+
uncoveredWaiters = new Map();
|
|
47
|
+
registerExitHandler(sceneId, handler) {
|
|
48
|
+
this.exitHandlers.set(sceneId, handler);
|
|
49
|
+
return () => {
|
|
50
|
+
if (this.exitHandlers.get(sceneId) === handler) {
|
|
51
|
+
this.exitHandlers.delete(sceneId);
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
hasExitHandler(sceneId) {
|
|
56
|
+
return this.exitHandlers.has(sceneId);
|
|
57
|
+
}
|
|
58
|
+
async isSceneCovered(sceneId, includeStackCover = false) {
|
|
59
|
+
try {
|
|
60
|
+
const layers = await NativeNavigation.routeGraph();
|
|
61
|
+
if (!Array.isArray(layers) || layers.length === 0) {
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
const index = layers.findIndex(layer => containsScene(layer, sceneId));
|
|
65
|
+
if (index < 0) {
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
if (index < layers.length - 1) {
|
|
69
|
+
return true;
|
|
70
|
+
}
|
|
71
|
+
return includeStackCover ? isSceneCoveredInStack(layers[index], sceneId) : false;
|
|
72
|
+
}
|
|
73
|
+
catch {
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
async findTopmostDirectPresentedExitSceneId(sceneId) {
|
|
78
|
+
try {
|
|
79
|
+
const layers = await NativeNavigation.routeGraph();
|
|
80
|
+
if (!Array.isArray(layers) || layers.length <= 1) {
|
|
81
|
+
return null;
|
|
82
|
+
}
|
|
83
|
+
const index = layers.findIndex(layer => containsScene(layer, sceneId));
|
|
84
|
+
if (index < 0 || index + 1 !== layers.length - 1) {
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
87
|
+
const presentedLayer = layers[index + 1];
|
|
88
|
+
if (presentedLayer.layout === 'screen') {
|
|
89
|
+
return this.exitHandlers.has(presentedLayer.sceneId) ? presentedLayer.sceneId : null;
|
|
90
|
+
}
|
|
91
|
+
const match = findSceneInGraph(presentedLayer, node => this.exitHandlers.has(node.sceneId));
|
|
92
|
+
return match?.sceneId ?? null;
|
|
93
|
+
}
|
|
94
|
+
catch {
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
waitUntilUncovered(sceneId, includeStackCover) {
|
|
99
|
+
if (lifecycleHandler.getSceneState(sceneId) === 'destroyed') {
|
|
100
|
+
return Promise.resolve({ uncovered: false, waited: false });
|
|
101
|
+
}
|
|
102
|
+
return new Promise(resolve => {
|
|
103
|
+
let settled = false;
|
|
104
|
+
let waited = false;
|
|
105
|
+
let checkVersion = 0;
|
|
106
|
+
let checking = false;
|
|
107
|
+
let recheckQueued = false;
|
|
108
|
+
const finish = (result) => {
|
|
109
|
+
if (settled) {
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
settled = true;
|
|
113
|
+
lifecycleHandler.removeGlobalLifecycleListener(onGlobalLifecycle);
|
|
114
|
+
const waiters = this.uncoveredWaiters.get(sceneId);
|
|
115
|
+
if (waiters) {
|
|
116
|
+
waiters.delete(finish);
|
|
117
|
+
if (waiters.size === 0) {
|
|
118
|
+
this.uncoveredWaiters.delete(sceneId);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
resolve(result);
|
|
122
|
+
};
|
|
123
|
+
const runCheck = () => {
|
|
124
|
+
if (settled) {
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
if (lifecycleHandler.getSceneState(sceneId) === 'destroyed') {
|
|
128
|
+
finish({ uncovered: false, waited });
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
if (checking) {
|
|
132
|
+
recheckQueued = true;
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
checking = true;
|
|
136
|
+
const version = ++checkVersion;
|
|
137
|
+
this.isSceneCovered(sceneId, includeStackCover).then(covered => {
|
|
138
|
+
checking = false;
|
|
139
|
+
if (settled || version !== checkVersion) {
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
if (lifecycleHandler.getSceneState(sceneId) === 'destroyed') {
|
|
143
|
+
finish({ uncovered: false, waited });
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
if (!covered) {
|
|
147
|
+
finish({ uncovered: true, waited });
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
waited = true;
|
|
151
|
+
if (recheckQueued) {
|
|
152
|
+
recheckQueued = false;
|
|
153
|
+
runCheck();
|
|
154
|
+
}
|
|
155
|
+
}, () => {
|
|
156
|
+
checking = false;
|
|
157
|
+
if (settled) {
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
waited = true;
|
|
161
|
+
if (recheckQueued) {
|
|
162
|
+
recheckQueued = false;
|
|
163
|
+
runCheck();
|
|
164
|
+
}
|
|
165
|
+
});
|
|
166
|
+
};
|
|
167
|
+
const onGlobalLifecycle = (changedSceneId, nextState) => {
|
|
168
|
+
if (settled) {
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
if (changedSceneId === sceneId) {
|
|
172
|
+
if (nextState === 'destroyed') {
|
|
173
|
+
finish({ uncovered: false, waited });
|
|
174
|
+
}
|
|
175
|
+
else if (nextState === 'visible') {
|
|
176
|
+
waited = true;
|
|
177
|
+
runCheck();
|
|
178
|
+
}
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
// iOS OverFullScreen modal dismissal emits invisible/destroyed on the upper scene
|
|
182
|
+
// without re-emitting visible on the underlying scene.
|
|
183
|
+
if (nextState === 'invisible' || nextState === 'destroyed') {
|
|
184
|
+
waited = true;
|
|
185
|
+
runCheck();
|
|
186
|
+
}
|
|
187
|
+
};
|
|
188
|
+
let waiters = this.uncoveredWaiters.get(sceneId);
|
|
189
|
+
if (!waiters) {
|
|
190
|
+
waiters = new Set();
|
|
191
|
+
this.uncoveredWaiters.set(sceneId, waiters);
|
|
192
|
+
}
|
|
193
|
+
waiters.add(finish);
|
|
194
|
+
lifecycleHandler.addGlobalLifecycleListener(onGlobalLifecycle);
|
|
195
|
+
// Re-check right after subscribing so an uncover during the prior async check is never missed.
|
|
196
|
+
runCheck();
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
async awaitPendingClosesForOwner(ownerSceneId) {
|
|
200
|
+
if (this.closingScenes.size === 0) {
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
const pending = [];
|
|
204
|
+
for (const [closingSceneId, entry] of this.closingScenes) {
|
|
205
|
+
if (entry.deferred || closingSceneId === ownerSceneId) {
|
|
206
|
+
continue;
|
|
207
|
+
}
|
|
208
|
+
const presentingOwner = lifecycleHandler.getSceneInfo(closingSceneId)?.presentingId ??
|
|
209
|
+
this.presentingOwnerByChild.get(closingSceneId);
|
|
210
|
+
if (!presentingOwner || presentingOwner !== ownerSceneId) {
|
|
211
|
+
continue;
|
|
212
|
+
}
|
|
213
|
+
pending.push(entry.promise.catch(() => undefined));
|
|
214
|
+
}
|
|
215
|
+
if (pending.length > 0) {
|
|
216
|
+
await Promise.all(pending);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
runExitWithTimeout(handler) {
|
|
220
|
+
const timeoutMs = handler.exitTimeoutMs ?? DEFAULT_EXIT_TIMEOUT_MS;
|
|
221
|
+
let result;
|
|
222
|
+
try {
|
|
223
|
+
result = handler.onExit();
|
|
224
|
+
}
|
|
225
|
+
catch (error) {
|
|
226
|
+
return Promise.reject(error);
|
|
227
|
+
}
|
|
228
|
+
if (!result || typeof result.then !== 'function') {
|
|
229
|
+
return Promise.resolve();
|
|
230
|
+
}
|
|
231
|
+
if (timeoutMs <= 0) {
|
|
232
|
+
return result;
|
|
233
|
+
}
|
|
234
|
+
return new Promise((resolve, reject) => {
|
|
235
|
+
let settled = false;
|
|
236
|
+
const timer = setTimeout(() => {
|
|
237
|
+
if (settled) {
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
240
|
+
settled = true;
|
|
241
|
+
reject(new Error(`Scene exit handler timed out after ${timeoutMs}ms.`));
|
|
242
|
+
}, timeoutMs);
|
|
243
|
+
timer.unref?.();
|
|
244
|
+
Promise.resolve(result).then(() => {
|
|
245
|
+
if (settled) {
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
248
|
+
settled = true;
|
|
249
|
+
clearTimeout(timer);
|
|
250
|
+
resolve();
|
|
251
|
+
}, error => {
|
|
252
|
+
if (settled) {
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
255
|
+
settled = true;
|
|
256
|
+
clearTimeout(timer);
|
|
257
|
+
reject(error);
|
|
258
|
+
});
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
middleware = async (sceneId, action, params, next) => {
|
|
262
|
+
if (PRESENT_ACTIONS.has(action)) {
|
|
263
|
+
return this.handleOpen(sceneId, action, params, next);
|
|
264
|
+
}
|
|
265
|
+
if (CLOSE_ACTIONS.has(action)) {
|
|
266
|
+
return this.handleClose(sceneId, action, params, next);
|
|
267
|
+
}
|
|
268
|
+
return next(sceneId, action, params);
|
|
269
|
+
};
|
|
270
|
+
async handleOpen(ownerSceneId, action, params, next) {
|
|
271
|
+
await this.awaitPendingClosesForOwner(ownerSceneId);
|
|
272
|
+
const result = await next(ownerSceneId, action, params);
|
|
273
|
+
if (result.ok && result.sceneId) {
|
|
274
|
+
this.presentingOwnerByChild.set(result.sceneId, ownerSceneId);
|
|
275
|
+
}
|
|
276
|
+
return result;
|
|
277
|
+
}
|
|
278
|
+
async handleClose(sceneId, action, params, next) {
|
|
279
|
+
const explicitTarget = params.target ?? (action === 'close' ? 'self' : undefined);
|
|
280
|
+
const onCovered = params.onCovered ?? 'fail';
|
|
281
|
+
if (explicitTarget === 'presented') {
|
|
282
|
+
const childSceneId = await this.findTopmostDirectPresentedExitSceneId(sceneId);
|
|
283
|
+
if (childSceneId) {
|
|
284
|
+
const childMode = lifecycleHandler.getSceneInfo(childSceneId)?.mode;
|
|
285
|
+
const childAction = childMode === 'present' ? 'dismiss' : 'hideModal';
|
|
286
|
+
return this.runDeduplicatedSelfClose(childSceneId, childAction, { ...params, target: 'self' }, onCovered, next);
|
|
287
|
+
}
|
|
288
|
+
return next(sceneId, action, params);
|
|
289
|
+
}
|
|
290
|
+
if (explicitTarget === 'self') {
|
|
291
|
+
return this.runDeduplicatedSelfClose(sceneId, action, params, onCovered, next);
|
|
292
|
+
}
|
|
293
|
+
// Legacy hideModal() / dismiss() without explicit target:
|
|
294
|
+
// If covered by a presented child, keep v5.0 native semantics (dismiss child).
|
|
295
|
+
// Only coordinate self exit when not covered and an exit handler is registered.
|
|
296
|
+
if (this.exitHandlers.has(sceneId) && !(await this.isSceneCovered(sceneId, false))) {
|
|
297
|
+
return this.runDeduplicatedSelfClose(sceneId, action, params, onCovered, next);
|
|
298
|
+
}
|
|
299
|
+
return next(sceneId, action, params);
|
|
300
|
+
}
|
|
301
|
+
async runDeduplicatedSelfClose(sceneId, action, params, onCovered, next) {
|
|
302
|
+
const existing = this.closingScenes.get(sceneId);
|
|
303
|
+
if (existing) {
|
|
304
|
+
if (existing.deferred) {
|
|
305
|
+
if (onCovered === 'fail') {
|
|
306
|
+
return {
|
|
307
|
+
ok: false,
|
|
308
|
+
code: 'covered',
|
|
309
|
+
message: 'Target scene is covered by another scene.',
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
if (params.result !== undefined) {
|
|
313
|
+
existing.nativeParams = {
|
|
314
|
+
...existing.nativeParams,
|
|
315
|
+
result: params.result,
|
|
316
|
+
};
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
return existing.promise;
|
|
320
|
+
}
|
|
321
|
+
const nativeParams = { ...params, target: 'self' };
|
|
322
|
+
delete nativeParams.onCovered;
|
|
323
|
+
const entry = {
|
|
324
|
+
promise: Promise.resolve({ ok: false, code: 'invalid_action' }),
|
|
325
|
+
deferred: false,
|
|
326
|
+
nativeParams,
|
|
327
|
+
};
|
|
328
|
+
const pending = this.executeSelfClose(sceneId, action, onCovered, entry, next);
|
|
329
|
+
entry.promise = pending;
|
|
330
|
+
this.closingScenes.set(sceneId, entry);
|
|
331
|
+
try {
|
|
332
|
+
return await pending;
|
|
333
|
+
}
|
|
334
|
+
finally {
|
|
335
|
+
if (this.closingScenes.get(sceneId) === entry) {
|
|
336
|
+
this.closingScenes.delete(sceneId);
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
async waitForDeferredUncover(sceneId, entry, includeStackCover) {
|
|
341
|
+
entry.deferred = true;
|
|
342
|
+
try {
|
|
343
|
+
return await this.waitUntilUncovered(sceneId, includeStackCover);
|
|
344
|
+
}
|
|
345
|
+
finally {
|
|
346
|
+
entry.deferred = false;
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
async executeSelfClose(sceneId, action, onCovered, entry, next) {
|
|
350
|
+
const includeStackCover = action === 'close';
|
|
351
|
+
let immediateCoveredRetries = 0;
|
|
352
|
+
while (true) {
|
|
353
|
+
if (lifecycleHandler.getSceneState(sceneId) === 'destroyed') {
|
|
354
|
+
return { ok: false, code: 'scene_not_found', message: 'Target scene is already destroyed.' };
|
|
355
|
+
}
|
|
356
|
+
if (await this.isSceneCovered(sceneId, includeStackCover)) {
|
|
357
|
+
if (onCovered === 'defer') {
|
|
358
|
+
const waitResult = await this.waitForDeferredUncover(sceneId, entry, includeStackCover);
|
|
359
|
+
if (!waitResult.uncovered) {
|
|
360
|
+
return { ok: false, code: 'scene_not_found' };
|
|
361
|
+
}
|
|
362
|
+
immediateCoveredRetries = 0;
|
|
363
|
+
continue;
|
|
364
|
+
}
|
|
365
|
+
return {
|
|
366
|
+
ok: false,
|
|
367
|
+
code: 'covered',
|
|
368
|
+
message: 'Target scene is covered by another scene.',
|
|
369
|
+
};
|
|
370
|
+
}
|
|
371
|
+
const registration = this.exitHandlers.get(sceneId);
|
|
372
|
+
if (registration) {
|
|
373
|
+
try {
|
|
374
|
+
await this.runExitWithTimeout(registration);
|
|
375
|
+
}
|
|
376
|
+
catch (error) {
|
|
377
|
+
registration.onRestore?.('exit_error');
|
|
378
|
+
return {
|
|
379
|
+
ok: false,
|
|
380
|
+
code: 'invalid_action',
|
|
381
|
+
message: error instanceof Error ? error.message : String(error),
|
|
382
|
+
};
|
|
383
|
+
}
|
|
384
|
+
if (await this.isSceneCovered(sceneId, includeStackCover)) {
|
|
385
|
+
registration.onRestore?.('covered');
|
|
386
|
+
if (onCovered === 'defer') {
|
|
387
|
+
const waitResult = await this.waitForDeferredUncover(sceneId, entry, includeStackCover);
|
|
388
|
+
if (!waitResult.uncovered) {
|
|
389
|
+
return { ok: false, code: 'scene_not_found' };
|
|
390
|
+
}
|
|
391
|
+
immediateCoveredRetries = 0;
|
|
392
|
+
continue;
|
|
393
|
+
}
|
|
394
|
+
return {
|
|
395
|
+
ok: false,
|
|
396
|
+
code: 'covered',
|
|
397
|
+
message: 'Target scene became covered during exit.',
|
|
398
|
+
};
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
const result = await next(sceneId, action, entry.nativeParams);
|
|
402
|
+
if (result.ok) {
|
|
403
|
+
this.presentingOwnerByChild.delete(sceneId);
|
|
404
|
+
return result;
|
|
405
|
+
}
|
|
406
|
+
registration?.onRestore?.(result.code);
|
|
407
|
+
if (result.code === 'covered' && onCovered === 'defer') {
|
|
408
|
+
const waitResult = await this.waitForDeferredUncover(sceneId, entry, includeStackCover);
|
|
409
|
+
if (!waitResult.uncovered) {
|
|
410
|
+
return { ok: false, code: 'scene_not_found' };
|
|
411
|
+
}
|
|
412
|
+
if (waitResult.waited) {
|
|
413
|
+
immediateCoveredRetries = 0;
|
|
414
|
+
}
|
|
415
|
+
else {
|
|
416
|
+
immediateCoveredRetries += 1;
|
|
417
|
+
if (immediateCoveredRetries > MAX_IMMEDIATE_COVERED_RETRIES) {
|
|
418
|
+
return result;
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
continue;
|
|
422
|
+
}
|
|
423
|
+
return result;
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
onSceneDestroyed(sceneId) {
|
|
427
|
+
this.exitHandlers.delete(sceneId);
|
|
428
|
+
const waiters = this.uncoveredWaiters.get(sceneId);
|
|
429
|
+
if (waiters) {
|
|
430
|
+
this.uncoveredWaiters.delete(sceneId);
|
|
431
|
+
waiters.forEach(resolve => resolve({ uncovered: false, waited: false }));
|
|
432
|
+
}
|
|
433
|
+
this.presentingOwnerByChild.delete(sceneId);
|
|
434
|
+
for (const [childId, ownerId] of this.presentingOwnerByChild) {
|
|
435
|
+
if (ownerId === sceneId) {
|
|
436
|
+
this.presentingOwnerByChild.delete(childId);
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
onRootReset() {
|
|
441
|
+
for (const waiters of this.uncoveredWaiters.values()) {
|
|
442
|
+
waiters.forEach(resolve => resolve({ uncovered: false, waited: false }));
|
|
443
|
+
}
|
|
444
|
+
this.uncoveredWaiters.clear();
|
|
445
|
+
this.exitHandlers.clear();
|
|
446
|
+
this.presentingOwnerByChild.clear();
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
export const presentationCoordinator = new PresentationCoordinator();
|
|
@@ -1,15 +1,27 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
export type
|
|
5
|
-
export type
|
|
1
|
+
import type { CancelReason, NavigationResult, ResultMap } from '../Route';
|
|
2
|
+
export declare const RESULT_CANCEL: number, RESULT_OK: number, RESULT_BLOCK: number;
|
|
3
|
+
export type ResultType = unknown;
|
|
4
|
+
export type ResultEventListener<T = unknown> = (result: NavigationResult<T>) => void;
|
|
5
|
+
export type PreparedResult<T = unknown> = {
|
|
6
|
+
promise: Promise<NavigationResult<T>>;
|
|
7
|
+
cancel: () => void;
|
|
8
|
+
};
|
|
6
9
|
export default class ResultEventHandler {
|
|
7
10
|
private listenerRecords;
|
|
8
|
-
constructor();
|
|
9
11
|
handleComponentResult(): void;
|
|
10
12
|
private addResultEventListener;
|
|
11
13
|
private removeResultEventListener;
|
|
12
|
-
invalidateResultEventListener(sceneId: string): void;
|
|
13
|
-
|
|
14
|
+
invalidateResultEventListener(sceneId: string, reason?: CancelReason): void;
|
|
15
|
+
invalidateAll(reason?: CancelReason): void;
|
|
16
|
+
prepareResult<T = unknown>(sceneId: string, requestCode: number): PreparedResult<T>;
|
|
17
|
+
normalizePendingResult<T extends ResultMap = ResultMap>(pending: {
|
|
18
|
+
code: number;
|
|
19
|
+
data?: T | null;
|
|
20
|
+
}): {
|
|
21
|
+
code: number;
|
|
22
|
+
data: T | null;
|
|
23
|
+
};
|
|
24
|
+
setResult<T extends ResultMap = ResultMap>(sceneId: string, resultCode: number, data?: T | null): void;
|
|
25
|
+
waitResult<T = unknown>(sceneId: string, requestCode: number): Promise<NavigationResult<T>>;
|
|
14
26
|
}
|
|
15
|
-
export
|
|
27
|
+
export declare const resultHandler: ResultEventHandler;
|