hybrid-navigation 5.2.0 → 5.3.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/README.md +9 -20
- package/android/src/main/java/com/navigation/androidx/AwesomeFragment.java +5 -13
- package/android/src/main/java/com/navigation/androidx/DefaultTabBarProvider.java +2 -2
- package/android/src/main/java/com/navigation/androidx/TabBarFragment.java +0 -50
- package/android/src/main/java/com/navigation/androidx/TabBarItem.java +0 -4
- package/android/src/main/java/com/reactnative/hybridnavigation/GlobalStyle.java +0 -7
- package/android/src/main/java/com/reactnative/hybridnavigation/HybridFragment.java +7 -43
- package/android/src/main/java/com/reactnative/hybridnavigation/NativeNavigation.java +5 -39
- package/android/src/main/java/com/reactnative/hybridnavigation/ReactFragment.java +62 -135
- package/android/src/main/java/com/reactnative/hybridnavigation/ReactManager.java +0 -20
- package/android/src/main/java/com/reactnative/hybridnavigation/ReactTabBarFragment.java +10 -21
- package/android/src/main/java/com/reactnative/hybridnavigation/navigator/ScreenNavigator.java +39 -87
- package/android/src/main/java/com/reactnative/hybridnavigation/navigator/TabNavigator.java +2 -16
- package/dist/NativeEvent.d.ts +1 -11
- package/dist/NativeNavigation.d.ts +7 -6
- package/dist/Navigation.d.ts +8 -22
- package/dist/Navigation.js +33 -175
- package/dist/NavigationComponentWrap.d.ts +3 -3
- package/dist/Navigator.d.ts +4 -5
- package/dist/Navigator.js +13 -37
- package/dist/Options.d.ts +2 -2
- package/dist/Route.d.ts +10 -35
- package/dist/handler/DispatchCommandHandler.d.ts +3 -18
- package/dist/handler/DispatchCommandHandler.js +17 -64
- package/dist/handler/LayoutCommandHandler.d.ts +2 -3
- package/dist/handler/LayoutCommandHandler.js +0 -3
- package/dist/handler/LeaveGuardHandler.d.ts +3 -3
- package/dist/handler/LeaveGuardHandler.js +57 -32
- package/dist/handler/LifecycleEventHandler.d.ts +2 -14
- package/dist/handler/LifecycleEventHandler.js +2 -79
- package/dist/handler/ResultEventHandler.d.ts +0 -1
- package/dist/handler/ResultEventHandler.js +0 -3
- package/dist/handler/SceneExitHandler.d.ts +3 -0
- package/dist/handler/SceneExitHandler.js +76 -0
- package/dist/hooks.d.ts +0 -3
- package/dist/hooks.js +21 -46
- package/dist/index.d.ts +1 -12
- package/dist/index.js +0 -3
- package/dist/router/index.d.ts +0 -1
- package/dist/router/index.js +43 -11
- package/dist/router/stack.d.ts +1 -1
- package/dist/sceneCoverage.d.ts +2 -0
- package/dist/sceneCoverage.js +25 -0
- package/dist/store.d.ts +0 -2
- package/dist/store.js +0 -4
- package/ios/HBDGlobalStyle.h +0 -1
- package/ios/HBDGlobalStyle.mm +0 -7
- package/ios/HybridNavigation.h +0 -1
- package/ios/Navigator/HBDScreenNavigator.mm +89 -237
- package/ios/Navigator/HBDStackNavigator.mm +1 -1
- package/ios/Navigator/HBDTabNavigator.mm +15 -30
- package/ios/React/HBDNativeNavigation.mm +4 -31
- package/ios/React/HBDReactBridgeManager.h +0 -5
- package/ios/React/HBDReactBridgeManager.mm +0 -17
- package/ios/ViewController/HBDReactViewController.h +1 -1
- package/ios/ViewController/HBDReactViewController.mm +34 -9
- package/ios/ViewController/HBDTabBarController.h +2 -11
- package/ios/ViewController/HBDTabBarController.mm +14 -170
- package/ios/ViewController/HBDViewController.h +0 -1
- package/ios/ViewController/HBDViewController.mm +9 -57
- package/ios/ViewController/UIViewController+HBD.h +2 -0
- package/ios/ViewController/UIViewController+HBD.mm +5 -0
- package/package.json +1 -3
- package/src/NativeEvent.ts +1 -12
- package/src/NativeNavigation.ts +8 -7
- package/src/Navigation.ts +50 -281
- package/src/NavigationComponentWrap.tsx +3 -11
- package/src/Navigator.ts +15 -46
- package/src/Options.ts +2 -3
- package/src/Route.ts +13 -56
- package/src/handler/DispatchCommandHandler.ts +29 -111
- package/src/handler/LayoutCommandHandler.ts +2 -6
- package/src/handler/LeaveGuardHandler.ts +65 -32
- package/src/handler/LifecycleEventHandler.ts +22 -122
- package/src/handler/ResultEventHandler.ts +0 -6
- package/src/handler/SceneExitHandler.ts +86 -0
- package/src/hooks.ts +18 -53
- package/src/index.ts +0 -14
- package/src/router/index.ts +45 -12
- package/src/router/stack.ts +1 -1
- package/src/sceneCoverage.ts +30 -0
- package/src/store.ts +0 -5
- package/dist/defineRoutes.d.ts +0 -19
- package/dist/defineRoutes.js +0 -6
- package/dist/handler/PresentationCoordinator.d.ts +0 -39
- package/dist/handler/PresentationCoordinator.js +0 -673
- package/dist/handler/TabEventHandler.d.ts +0 -8
- package/dist/handler/TabEventHandler.js +0 -25
- package/dist/reveal.d.ts +0 -46
- package/dist/reveal.js +0 -431
- package/dist/routeGraph.d.ts +0 -12
- package/dist/routeGraph.js +0 -68
- package/dist/testing.d.ts +0 -82
- package/dist/testing.js +0 -253
- package/ios/ViewController/HBDTabBarRenderer.h +0 -28
- package/src/defineRoutes.ts +0 -46
- package/src/handler/PresentationCoordinator.ts +0 -806
- package/src/handler/TabEventHandler.ts +0 -31
- package/src/reveal.ts +0 -565
- package/src/routeGraph.ts +0 -87
- package/src/testing.ts +0 -402
- package/testing.d.ts +0 -1
- package/testing.js +0 -3
|
@@ -1,806 +0,0 @@
|
|
|
1
|
-
import NativeNavigation from '../NativeNavigation';
|
|
2
|
-
import type {
|
|
3
|
-
CloseTarget,
|
|
4
|
-
CoveredPolicy,
|
|
5
|
-
DispatchResult,
|
|
6
|
-
RouteGraph,
|
|
7
|
-
SceneCoverageListener,
|
|
8
|
-
SceneExitHandler,
|
|
9
|
-
SceneLifecycleState,
|
|
10
|
-
} from '../Route';
|
|
11
|
-
import { containsScene, findSceneInGraph } from '../routeGraph';
|
|
12
|
-
import type { DispatchParams } from './DispatchCommandHandler';
|
|
13
|
-
import { lifecycleHandler } from './LifecycleEventHandler';
|
|
14
|
-
|
|
15
|
-
const PRESENT_ACTIONS = new Set([
|
|
16
|
-
'showModal',
|
|
17
|
-
'showModalLayout',
|
|
18
|
-
'present',
|
|
19
|
-
'presentLayout',
|
|
20
|
-
]);
|
|
21
|
-
|
|
22
|
-
const CLOSE_ACTIONS = new Set(['hideModal', 'dismiss', 'close']);
|
|
23
|
-
|
|
24
|
-
const DEFAULT_EXIT_TIMEOUT_MS = 1200;
|
|
25
|
-
const MAX_IMMEDIATE_COVERED_RETRIES = 1;
|
|
26
|
-
|
|
27
|
-
function findInnermostContainingStack(graph: RouteGraph, sceneId: string): RouteGraph | null {
|
|
28
|
-
if (graph.layout === 'stack' && Array.isArray(graph.children)) {
|
|
29
|
-
for (const child of graph.children) {
|
|
30
|
-
if (!containsScene(child, sceneId)) {
|
|
31
|
-
continue;
|
|
32
|
-
}
|
|
33
|
-
const nested = findInnermostContainingStack(child, sceneId);
|
|
34
|
-
return nested ?? graph;
|
|
35
|
-
}
|
|
36
|
-
return null;
|
|
37
|
-
}
|
|
38
|
-
if (!Array.isArray(graph.children)) {
|
|
39
|
-
return null;
|
|
40
|
-
}
|
|
41
|
-
for (const child of graph.children) {
|
|
42
|
-
if (containsScene(child, sceneId)) {
|
|
43
|
-
return findInnermostContainingStack(child, sceneId);
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
return null;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* Matches native closeSelf/closeScene coverage semantics:
|
|
51
|
-
* covered only by an upper presented layer or a newer screen in the innermost containing stack
|
|
52
|
-
* (does not treat unselected tabs or outer ancestor stacks as blocking close).
|
|
53
|
-
*/
|
|
54
|
-
function isSceneCoveredInStack(graph: RouteGraph, sceneId: string): boolean {
|
|
55
|
-
const stack = findInnermostContainingStack(graph, sceneId);
|
|
56
|
-
if (!stack || !Array.isArray(stack.children) || stack.children.length <= 1) {
|
|
57
|
-
return false;
|
|
58
|
-
}
|
|
59
|
-
const childIndex = stack.children.findIndex(child => containsScene(child, sceneId));
|
|
60
|
-
return childIndex >= 0 && childIndex < stack.children.length - 1;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
function isSceneCoveredForClose(
|
|
64
|
-
layers: RouteGraph[],
|
|
65
|
-
sceneId: string,
|
|
66
|
-
includeStackCover: boolean,
|
|
67
|
-
): boolean {
|
|
68
|
-
if (!Array.isArray(layers) || layers.length === 0) {
|
|
69
|
-
return false;
|
|
70
|
-
}
|
|
71
|
-
const index = layers.findIndex(layer => containsScene(layer, sceneId));
|
|
72
|
-
if (index < 0) {
|
|
73
|
-
return false;
|
|
74
|
-
}
|
|
75
|
-
if (index < layers.length - 1) {
|
|
76
|
-
return true;
|
|
77
|
-
}
|
|
78
|
-
return includeStackCover ? isSceneCoveredInStack(layers[index], sceneId) : false;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* Full visual coverage check for observation APIs (useSceneCovered / useIsTopmost / addSceneCoverageListener):
|
|
83
|
-
* covered by an upper presented layer, any non-top ancestor stack, or an unselected tab.
|
|
84
|
-
*/
|
|
85
|
-
function isSceneVisuallyCoveredInLayer(graph: RouteGraph, sceneId: string): boolean {
|
|
86
|
-
if (!Array.isArray(graph.children) || graph.children.length === 0) {
|
|
87
|
-
return false;
|
|
88
|
-
}
|
|
89
|
-
const childIndex = graph.children.findIndex(child => containsScene(child, sceneId));
|
|
90
|
-
if (childIndex < 0) {
|
|
91
|
-
return false;
|
|
92
|
-
}
|
|
93
|
-
if (graph.layout === 'stack' && childIndex < graph.children.length - 1) {
|
|
94
|
-
return true;
|
|
95
|
-
}
|
|
96
|
-
if (graph.layout === 'tabs') {
|
|
97
|
-
const selectedIndex = (graph as { selectedIndex?: unknown }).selectedIndex;
|
|
98
|
-
if (typeof selectedIndex === 'number' && childIndex !== selectedIndex) {
|
|
99
|
-
return true;
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
return isSceneVisuallyCoveredInLayer(graph.children[childIndex], sceneId);
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
function isSceneVisuallyCoveredInLayers(layers: RouteGraph[], sceneId: string): boolean {
|
|
106
|
-
if (!Array.isArray(layers) || layers.length === 0) {
|
|
107
|
-
return false;
|
|
108
|
-
}
|
|
109
|
-
const index = layers.findIndex(layer => containsScene(layer, sceneId));
|
|
110
|
-
if (index < 0) {
|
|
111
|
-
return false;
|
|
112
|
-
}
|
|
113
|
-
if (index < layers.length - 1) {
|
|
114
|
-
return true;
|
|
115
|
-
}
|
|
116
|
-
return isSceneVisuallyCoveredInLayer(layers[index], sceneId);
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
interface CoverageWatcherEntry {
|
|
120
|
-
listener: SceneCoverageListener;
|
|
121
|
-
lastNotified?: boolean;
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
interface PendingCloseEntry {
|
|
125
|
-
promise: Promise<DispatchResult>;
|
|
126
|
-
deferred: boolean;
|
|
127
|
-
nativeParams: DispatchParams;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
interface DeferredUncoverResult {
|
|
131
|
-
uncovered: boolean;
|
|
132
|
-
waited: boolean;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
export default class PresentationCoordinator {
|
|
136
|
-
private exitHandlers = new Map<string, SceneExitHandler[]>();
|
|
137
|
-
private closingScenes = new Map<string, PendingCloseEntry>();
|
|
138
|
-
private presentingOwnerByChild = new Map<string, string>();
|
|
139
|
-
private uncoveredWaiters = new Map<string, Set<(result: DeferredUncoverResult) => void>>();
|
|
140
|
-
private coverageWatchers = new Map<string, Set<CoverageWatcherEntry>>();
|
|
141
|
-
private coverageLifecycleAttached = false;
|
|
142
|
-
private coverageRefreshScheduled = false;
|
|
143
|
-
private coverageChecking = false;
|
|
144
|
-
private coverageRecheckQueued = false;
|
|
145
|
-
private coverageCheckVersion = 0;
|
|
146
|
-
private lastKnownLayers: RouteGraph[] | null = null;
|
|
147
|
-
|
|
148
|
-
private onCoverageLifecycle = () => {
|
|
149
|
-
this.scheduleCoverageRefresh();
|
|
150
|
-
};
|
|
151
|
-
|
|
152
|
-
addCoverageListener(sceneId: string, listener: SceneCoverageListener): () => void {
|
|
153
|
-
let set = this.coverageWatchers.get(sceneId);
|
|
154
|
-
if (!set) {
|
|
155
|
-
set = new Set();
|
|
156
|
-
this.coverageWatchers.set(sceneId, set);
|
|
157
|
-
}
|
|
158
|
-
const entry: CoverageWatcherEntry = { listener };
|
|
159
|
-
set.add(entry);
|
|
160
|
-
if (!this.coverageLifecycleAttached) {
|
|
161
|
-
this.coverageLifecycleAttached = true;
|
|
162
|
-
lifecycleHandler.addGlobalLifecycleListener(this.onCoverageLifecycle);
|
|
163
|
-
}
|
|
164
|
-
this.scheduleCoverageRefresh();
|
|
165
|
-
|
|
166
|
-
return () => {
|
|
167
|
-
const current = this.coverageWatchers.get(sceneId);
|
|
168
|
-
if (!current) {
|
|
169
|
-
return;
|
|
170
|
-
}
|
|
171
|
-
current.delete(entry);
|
|
172
|
-
if (current.size === 0) {
|
|
173
|
-
this.coverageWatchers.delete(sceneId);
|
|
174
|
-
}
|
|
175
|
-
if (this.coverageWatchers.size === 0 && this.coverageLifecycleAttached) {
|
|
176
|
-
this.coverageLifecycleAttached = false;
|
|
177
|
-
lifecycleHandler.removeGlobalLifecycleListener(this.onCoverageLifecycle);
|
|
178
|
-
}
|
|
179
|
-
};
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
notifyRouteGraphChanged(layers?: RouteGraph[]) {
|
|
183
|
-
if (Array.isArray(layers)) {
|
|
184
|
-
this.lastKnownLayers = layers;
|
|
185
|
-
}
|
|
186
|
-
this.scheduleCoverageRefresh();
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
private scheduleCoverageRefresh() {
|
|
190
|
-
if (this.coverageWatchers.size === 0) {
|
|
191
|
-
return;
|
|
192
|
-
}
|
|
193
|
-
if (this.coverageChecking) {
|
|
194
|
-
// Invalidate any in-flight routeGraph() snapshot so stale coverage is never broadcast.
|
|
195
|
-
this.coverageCheckVersion += 1;
|
|
196
|
-
this.coverageRecheckQueued = true;
|
|
197
|
-
return;
|
|
198
|
-
}
|
|
199
|
-
if (this.coverageRefreshScheduled) {
|
|
200
|
-
return;
|
|
201
|
-
}
|
|
202
|
-
this.coverageRefreshScheduled = true;
|
|
203
|
-
Promise.resolve().then(() => {
|
|
204
|
-
this.flushCoverageRefresh();
|
|
205
|
-
});
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
private flushCoverageRefresh() {
|
|
209
|
-
this.coverageRefreshScheduled = false;
|
|
210
|
-
if (this.coverageWatchers.size === 0) {
|
|
211
|
-
return;
|
|
212
|
-
}
|
|
213
|
-
if (this.coverageChecking) {
|
|
214
|
-
this.coverageCheckVersion += 1;
|
|
215
|
-
this.coverageRecheckQueued = true;
|
|
216
|
-
return;
|
|
217
|
-
}
|
|
218
|
-
this.coverageChecking = true;
|
|
219
|
-
const version = ++this.coverageCheckVersion;
|
|
220
|
-
NativeNavigation.routeGraph().then(
|
|
221
|
-
layers => {
|
|
222
|
-
this.coverageChecking = false;
|
|
223
|
-
if (version === this.coverageCheckVersion && Array.isArray(layers)) {
|
|
224
|
-
this.lastKnownLayers = layers;
|
|
225
|
-
for (const [sceneId, entries] of Array.from(this.coverageWatchers.entries())) {
|
|
226
|
-
const covered = isSceneVisuallyCoveredInLayers(layers, sceneId);
|
|
227
|
-
for (const entry of Array.from(entries)) {
|
|
228
|
-
if (entry.lastNotified !== covered) {
|
|
229
|
-
entry.lastNotified = covered;
|
|
230
|
-
try {
|
|
231
|
-
entry.listener(covered);
|
|
232
|
-
} catch (error) {
|
|
233
|
-
console.error(
|
|
234
|
-
'[hybrid-navigation] scene coverage listener error:',
|
|
235
|
-
error,
|
|
236
|
-
);
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
if (this.coverageRecheckQueued) {
|
|
243
|
-
this.coverageRecheckQueued = false;
|
|
244
|
-
this.scheduleCoverageRefresh();
|
|
245
|
-
}
|
|
246
|
-
},
|
|
247
|
-
() => {
|
|
248
|
-
this.coverageChecking = false;
|
|
249
|
-
if (this.coverageRecheckQueued) {
|
|
250
|
-
this.coverageRecheckQueued = false;
|
|
251
|
-
this.scheduleCoverageRefresh();
|
|
252
|
-
}
|
|
253
|
-
},
|
|
254
|
-
);
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
registerExitHandler(sceneId: string, handler: SceneExitHandler): () => void {
|
|
258
|
-
let stack = this.exitHandlers.get(sceneId);
|
|
259
|
-
if (!stack) {
|
|
260
|
-
stack = [];
|
|
261
|
-
this.exitHandlers.set(sceneId, stack);
|
|
262
|
-
}
|
|
263
|
-
stack.push(handler);
|
|
264
|
-
return () => {
|
|
265
|
-
const current = this.exitHandlers.get(sceneId);
|
|
266
|
-
if (!current) {
|
|
267
|
-
return;
|
|
268
|
-
}
|
|
269
|
-
const idx = current.lastIndexOf(handler);
|
|
270
|
-
if (idx >= 0) {
|
|
271
|
-
current.splice(idx, 1);
|
|
272
|
-
}
|
|
273
|
-
if (current.length === 0) {
|
|
274
|
-
this.exitHandlers.delete(sceneId);
|
|
275
|
-
}
|
|
276
|
-
};
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
private getTopExitHandler(sceneId: string): SceneExitHandler | undefined {
|
|
280
|
-
const stack = this.exitHandlers.get(sceneId);
|
|
281
|
-
return stack && stack.length > 0 ? stack[stack.length - 1] : undefined;
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
hasExitHandler(sceneId: string): boolean {
|
|
285
|
-
const stack = this.exitHandlers.get(sceneId);
|
|
286
|
-
return Boolean(stack && stack.length > 0);
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
isSceneVisuallyCoveredSync(sceneId: string): boolean {
|
|
290
|
-
if (!this.lastKnownLayers) {
|
|
291
|
-
return false;
|
|
292
|
-
}
|
|
293
|
-
return isSceneVisuallyCoveredInLayers(this.lastKnownLayers, sceneId);
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
async isSceneVisuallyCovered(sceneId: string): Promise<boolean> {
|
|
297
|
-
try {
|
|
298
|
-
const layers: RouteGraph[] = await NativeNavigation.routeGraph();
|
|
299
|
-
if (Array.isArray(layers)) {
|
|
300
|
-
this.lastKnownLayers = layers;
|
|
301
|
-
}
|
|
302
|
-
return isSceneVisuallyCoveredInLayers(layers, sceneId);
|
|
303
|
-
} catch {
|
|
304
|
-
return false;
|
|
305
|
-
}
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
async isSceneCovered(sceneId: string, includeStackCover: boolean = false): Promise<boolean> {
|
|
309
|
-
try {
|
|
310
|
-
const layers: RouteGraph[] = await NativeNavigation.routeGraph();
|
|
311
|
-
if (Array.isArray(layers)) {
|
|
312
|
-
this.lastKnownLayers = layers;
|
|
313
|
-
}
|
|
314
|
-
return isSceneCoveredForClose(layers, sceneId, includeStackCover);
|
|
315
|
-
} catch {
|
|
316
|
-
return false;
|
|
317
|
-
}
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
private async findTopmostDirectPresentedExitSceneId(sceneId: string): Promise<string | null> {
|
|
321
|
-
try {
|
|
322
|
-
const layers: RouteGraph[] = await NativeNavigation.routeGraph();
|
|
323
|
-
if (!Array.isArray(layers) || layers.length <= 1) {
|
|
324
|
-
return null;
|
|
325
|
-
}
|
|
326
|
-
const index = layers.findIndex(layer => containsScene(layer, sceneId));
|
|
327
|
-
if (index < 0 || index + 1 !== layers.length - 1) {
|
|
328
|
-
return null;
|
|
329
|
-
}
|
|
330
|
-
const presentedLayer = layers[index + 1];
|
|
331
|
-
if (presentedLayer.layout === 'screen') {
|
|
332
|
-
return this.hasExitHandler(presentedLayer.sceneId) ? presentedLayer.sceneId : null;
|
|
333
|
-
}
|
|
334
|
-
const match = findSceneInGraph(presentedLayer, node => this.hasExitHandler(node.sceneId));
|
|
335
|
-
return match?.sceneId ?? null;
|
|
336
|
-
} catch {
|
|
337
|
-
return null;
|
|
338
|
-
}
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
private waitUntilUncovered(
|
|
342
|
-
sceneId: string,
|
|
343
|
-
includeStackCover: boolean,
|
|
344
|
-
): Promise<DeferredUncoverResult> {
|
|
345
|
-
if (lifecycleHandler.getSceneState(sceneId) === 'destroyed') {
|
|
346
|
-
return Promise.resolve({ uncovered: false, waited: false });
|
|
347
|
-
}
|
|
348
|
-
return new Promise<DeferredUncoverResult>(resolve => {
|
|
349
|
-
let settled = false;
|
|
350
|
-
let waited = false;
|
|
351
|
-
let checkVersion = 0;
|
|
352
|
-
let checking = false;
|
|
353
|
-
let recheckQueued = false;
|
|
354
|
-
|
|
355
|
-
const finish = (result: DeferredUncoverResult) => {
|
|
356
|
-
if (settled) {
|
|
357
|
-
return;
|
|
358
|
-
}
|
|
359
|
-
settled = true;
|
|
360
|
-
lifecycleHandler.removeGlobalLifecycleListener(onGlobalLifecycle);
|
|
361
|
-
const waiters = this.uncoveredWaiters.get(sceneId);
|
|
362
|
-
if (waiters) {
|
|
363
|
-
waiters.delete(finish);
|
|
364
|
-
if (waiters.size === 0) {
|
|
365
|
-
this.uncoveredWaiters.delete(sceneId);
|
|
366
|
-
}
|
|
367
|
-
}
|
|
368
|
-
resolve(result);
|
|
369
|
-
};
|
|
370
|
-
|
|
371
|
-
const runCheck = () => {
|
|
372
|
-
if (settled) {
|
|
373
|
-
return;
|
|
374
|
-
}
|
|
375
|
-
if (lifecycleHandler.getSceneState(sceneId) === 'destroyed') {
|
|
376
|
-
finish({ uncovered: false, waited });
|
|
377
|
-
return;
|
|
378
|
-
}
|
|
379
|
-
if (checking) {
|
|
380
|
-
recheckQueued = true;
|
|
381
|
-
return;
|
|
382
|
-
}
|
|
383
|
-
checking = true;
|
|
384
|
-
const version = ++checkVersion;
|
|
385
|
-
this.isSceneCovered(sceneId, includeStackCover).then(
|
|
386
|
-
covered => {
|
|
387
|
-
checking = false;
|
|
388
|
-
if (settled || version !== checkVersion) {
|
|
389
|
-
return;
|
|
390
|
-
}
|
|
391
|
-
if (lifecycleHandler.getSceneState(sceneId) === 'destroyed') {
|
|
392
|
-
finish({ uncovered: false, waited });
|
|
393
|
-
return;
|
|
394
|
-
}
|
|
395
|
-
if (!covered) {
|
|
396
|
-
finish({ uncovered: true, waited });
|
|
397
|
-
return;
|
|
398
|
-
}
|
|
399
|
-
waited = true;
|
|
400
|
-
if (recheckQueued) {
|
|
401
|
-
recheckQueued = false;
|
|
402
|
-
runCheck();
|
|
403
|
-
}
|
|
404
|
-
},
|
|
405
|
-
() => {
|
|
406
|
-
checking = false;
|
|
407
|
-
if (settled) {
|
|
408
|
-
return;
|
|
409
|
-
}
|
|
410
|
-
waited = true;
|
|
411
|
-
if (recheckQueued) {
|
|
412
|
-
recheckQueued = false;
|
|
413
|
-
runCheck();
|
|
414
|
-
}
|
|
415
|
-
},
|
|
416
|
-
);
|
|
417
|
-
};
|
|
418
|
-
|
|
419
|
-
const onGlobalLifecycle = (changedSceneId: string, nextState: SceneLifecycleState) => {
|
|
420
|
-
if (settled) {
|
|
421
|
-
return;
|
|
422
|
-
}
|
|
423
|
-
if (changedSceneId === sceneId) {
|
|
424
|
-
if (nextState === 'destroyed') {
|
|
425
|
-
finish({ uncovered: false, waited });
|
|
426
|
-
} else if (nextState === 'visible') {
|
|
427
|
-
waited = true;
|
|
428
|
-
runCheck();
|
|
429
|
-
}
|
|
430
|
-
return;
|
|
431
|
-
}
|
|
432
|
-
// iOS OverFullScreen modal dismissal emits invisible/destroyed on the upper scene
|
|
433
|
-
// without re-emitting visible on the underlying scene.
|
|
434
|
-
if (nextState === 'invisible' || nextState === 'destroyed') {
|
|
435
|
-
waited = true;
|
|
436
|
-
runCheck();
|
|
437
|
-
}
|
|
438
|
-
};
|
|
439
|
-
|
|
440
|
-
let waiters = this.uncoveredWaiters.get(sceneId);
|
|
441
|
-
if (!waiters) {
|
|
442
|
-
waiters = new Set();
|
|
443
|
-
this.uncoveredWaiters.set(sceneId, waiters);
|
|
444
|
-
}
|
|
445
|
-
waiters.add(finish);
|
|
446
|
-
lifecycleHandler.addGlobalLifecycleListener(onGlobalLifecycle);
|
|
447
|
-
// Re-check right after subscribing so an uncover during the prior async check is never missed.
|
|
448
|
-
runCheck();
|
|
449
|
-
});
|
|
450
|
-
}
|
|
451
|
-
|
|
452
|
-
private async awaitPendingClosesForOwner(ownerSceneId: string): Promise<void> {
|
|
453
|
-
if (this.closingScenes.size === 0) {
|
|
454
|
-
return;
|
|
455
|
-
}
|
|
456
|
-
const pending: Promise<unknown>[] = [];
|
|
457
|
-
for (const [closingSceneId, entry] of this.closingScenes) {
|
|
458
|
-
if (entry.deferred || closingSceneId === ownerSceneId) {
|
|
459
|
-
continue;
|
|
460
|
-
}
|
|
461
|
-
const presentingOwner =
|
|
462
|
-
lifecycleHandler.getSceneInfo(closingSceneId)?.presentingId ??
|
|
463
|
-
this.presentingOwnerByChild.get(closingSceneId);
|
|
464
|
-
if (!presentingOwner || presentingOwner !== ownerSceneId) {
|
|
465
|
-
continue;
|
|
466
|
-
}
|
|
467
|
-
pending.push(entry.promise.catch(() => undefined));
|
|
468
|
-
}
|
|
469
|
-
if (pending.length > 0) {
|
|
470
|
-
await Promise.all(pending);
|
|
471
|
-
}
|
|
472
|
-
}
|
|
473
|
-
|
|
474
|
-
private runExitWithTimeout(handler: SceneExitHandler): Promise<void> {
|
|
475
|
-
const timeoutMs = handler.exitTimeoutMs ?? DEFAULT_EXIT_TIMEOUT_MS;
|
|
476
|
-
let result: void | Promise<void>;
|
|
477
|
-
try {
|
|
478
|
-
result = handler.onExit();
|
|
479
|
-
} catch (error) {
|
|
480
|
-
return Promise.reject(error);
|
|
481
|
-
}
|
|
482
|
-
if (!result || typeof (result as Promise<void>).then !== 'function') {
|
|
483
|
-
return Promise.resolve();
|
|
484
|
-
}
|
|
485
|
-
if (timeoutMs <= 0) {
|
|
486
|
-
return result;
|
|
487
|
-
}
|
|
488
|
-
return new Promise<void>((resolve, reject) => {
|
|
489
|
-
let settled = false;
|
|
490
|
-
const timer = setTimeout(() => {
|
|
491
|
-
if (settled) {
|
|
492
|
-
return;
|
|
493
|
-
}
|
|
494
|
-
settled = true;
|
|
495
|
-
reject(new Error(`Scene exit handler timed out after ${timeoutMs}ms.`));
|
|
496
|
-
}, timeoutMs);
|
|
497
|
-
(timer as { unref?: () => void }).unref?.();
|
|
498
|
-
Promise.resolve(result).then(
|
|
499
|
-
() => {
|
|
500
|
-
if (settled) {
|
|
501
|
-
return;
|
|
502
|
-
}
|
|
503
|
-
settled = true;
|
|
504
|
-
clearTimeout(timer);
|
|
505
|
-
resolve();
|
|
506
|
-
},
|
|
507
|
-
error => {
|
|
508
|
-
if (settled) {
|
|
509
|
-
return;
|
|
510
|
-
}
|
|
511
|
-
settled = true;
|
|
512
|
-
clearTimeout(timer);
|
|
513
|
-
reject(error);
|
|
514
|
-
},
|
|
515
|
-
);
|
|
516
|
-
});
|
|
517
|
-
}
|
|
518
|
-
|
|
519
|
-
middleware = async (
|
|
520
|
-
sceneId: string,
|
|
521
|
-
action: string,
|
|
522
|
-
params: DispatchParams,
|
|
523
|
-
next: (sceneId: string, action: string, params: DispatchParams) => Promise<DispatchResult>,
|
|
524
|
-
): Promise<DispatchResult> => {
|
|
525
|
-
let result: DispatchResult;
|
|
526
|
-
if (PRESENT_ACTIONS.has(action)) {
|
|
527
|
-
result = await this.handleOpen(sceneId, action, params, next);
|
|
528
|
-
} else if (CLOSE_ACTIONS.has(action)) {
|
|
529
|
-
result = await this.handleClose(sceneId, action, params, next);
|
|
530
|
-
} else {
|
|
531
|
-
result = await next(sceneId, action, params);
|
|
532
|
-
}
|
|
533
|
-
if (result.ok) {
|
|
534
|
-
if (this.coverageWatchers.size > 0) {
|
|
535
|
-
this.scheduleCoverageRefresh();
|
|
536
|
-
} else {
|
|
537
|
-
this.lastKnownLayers = null;
|
|
538
|
-
}
|
|
539
|
-
}
|
|
540
|
-
return result;
|
|
541
|
-
};
|
|
542
|
-
|
|
543
|
-
private async handleOpen(
|
|
544
|
-
ownerSceneId: string,
|
|
545
|
-
action: string,
|
|
546
|
-
params: DispatchParams,
|
|
547
|
-
next: (sceneId: string, action: string, params: DispatchParams) => Promise<DispatchResult>,
|
|
548
|
-
): Promise<DispatchResult> {
|
|
549
|
-
await this.awaitPendingClosesForOwner(ownerSceneId);
|
|
550
|
-
const result = await next(ownerSceneId, action, params);
|
|
551
|
-
if (result.ok && result.sceneId) {
|
|
552
|
-
this.presentingOwnerByChild.set(result.sceneId, ownerSceneId);
|
|
553
|
-
}
|
|
554
|
-
return result;
|
|
555
|
-
}
|
|
556
|
-
|
|
557
|
-
private async handleClose(
|
|
558
|
-
sceneId: string,
|
|
559
|
-
action: string,
|
|
560
|
-
params: DispatchParams,
|
|
561
|
-
next: (sceneId: string, action: string, params: DispatchParams) => Promise<DispatchResult>,
|
|
562
|
-
): Promise<DispatchResult> {
|
|
563
|
-
const explicitTarget: CloseTarget | undefined =
|
|
564
|
-
params.target ?? (action === 'close' ? 'self' : undefined);
|
|
565
|
-
const onCovered: CoveredPolicy = params.onCovered ?? 'fail';
|
|
566
|
-
|
|
567
|
-
if (explicitTarget === 'presented') {
|
|
568
|
-
const childSceneId = await this.findTopmostDirectPresentedExitSceneId(sceneId);
|
|
569
|
-
if (childSceneId) {
|
|
570
|
-
const childMode = lifecycleHandler.getSceneInfo(childSceneId)?.mode;
|
|
571
|
-
const childAction = childMode === 'present' ? 'dismiss' : 'hideModal';
|
|
572
|
-
return this.runDeduplicatedSelfClose(
|
|
573
|
-
childSceneId,
|
|
574
|
-
childAction,
|
|
575
|
-
{ ...params, target: 'self' },
|
|
576
|
-
onCovered,
|
|
577
|
-
next,
|
|
578
|
-
);
|
|
579
|
-
}
|
|
580
|
-
return next(sceneId, action, params);
|
|
581
|
-
}
|
|
582
|
-
|
|
583
|
-
if (explicitTarget === 'self') {
|
|
584
|
-
return this.runDeduplicatedSelfClose(sceneId, action, params, onCovered, next);
|
|
585
|
-
}
|
|
586
|
-
|
|
587
|
-
// Legacy hideModal() / dismiss() without explicit target:
|
|
588
|
-
// If covered by a presented child, keep v5.0 native semantics (dismiss child).
|
|
589
|
-
// Only coordinate self exit when not covered and an exit handler is registered.
|
|
590
|
-
if (this.hasExitHandler(sceneId) && !(await this.isSceneCovered(sceneId, false))) {
|
|
591
|
-
return this.runDeduplicatedSelfClose(sceneId, action, params, onCovered, next);
|
|
592
|
-
}
|
|
593
|
-
|
|
594
|
-
return next(sceneId, action, params);
|
|
595
|
-
}
|
|
596
|
-
|
|
597
|
-
private async runDeduplicatedSelfClose(
|
|
598
|
-
sceneId: string,
|
|
599
|
-
action: string,
|
|
600
|
-
params: DispatchParams,
|
|
601
|
-
onCovered: CoveredPolicy,
|
|
602
|
-
next: (sceneId: string, action: string, params: DispatchParams) => Promise<DispatchResult>,
|
|
603
|
-
): Promise<DispatchResult> {
|
|
604
|
-
const existing = this.closingScenes.get(sceneId);
|
|
605
|
-
if (existing) {
|
|
606
|
-
if (existing.deferred) {
|
|
607
|
-
if (onCovered === 'fail') {
|
|
608
|
-
return {
|
|
609
|
-
ok: false,
|
|
610
|
-
code: 'covered',
|
|
611
|
-
message: 'Target scene is covered by another scene.',
|
|
612
|
-
};
|
|
613
|
-
}
|
|
614
|
-
if (params.result !== undefined) {
|
|
615
|
-
existing.nativeParams = {
|
|
616
|
-
...existing.nativeParams,
|
|
617
|
-
result: params.result,
|
|
618
|
-
};
|
|
619
|
-
}
|
|
620
|
-
}
|
|
621
|
-
return existing.promise;
|
|
622
|
-
}
|
|
623
|
-
|
|
624
|
-
const nativeParams: DispatchParams = { ...params, target: 'self' };
|
|
625
|
-
delete nativeParams.onCovered;
|
|
626
|
-
|
|
627
|
-
const entry: PendingCloseEntry = {
|
|
628
|
-
promise: Promise.resolve({ ok: false, code: 'invalid_action' }),
|
|
629
|
-
deferred: false,
|
|
630
|
-
nativeParams,
|
|
631
|
-
};
|
|
632
|
-
const pending = this.executeSelfClose(sceneId, action, onCovered, entry, next);
|
|
633
|
-
entry.promise = pending;
|
|
634
|
-
this.closingScenes.set(sceneId, entry);
|
|
635
|
-
try {
|
|
636
|
-
return await pending;
|
|
637
|
-
} finally {
|
|
638
|
-
if (this.closingScenes.get(sceneId) === entry) {
|
|
639
|
-
this.closingScenes.delete(sceneId);
|
|
640
|
-
}
|
|
641
|
-
}
|
|
642
|
-
}
|
|
643
|
-
|
|
644
|
-
private async waitForDeferredUncover(
|
|
645
|
-
sceneId: string,
|
|
646
|
-
entry: PendingCloseEntry,
|
|
647
|
-
includeStackCover: boolean,
|
|
648
|
-
): Promise<DeferredUncoverResult> {
|
|
649
|
-
entry.deferred = true;
|
|
650
|
-
try {
|
|
651
|
-
return await this.waitUntilUncovered(sceneId, includeStackCover);
|
|
652
|
-
} finally {
|
|
653
|
-
entry.deferred = false;
|
|
654
|
-
}
|
|
655
|
-
}
|
|
656
|
-
|
|
657
|
-
private async executeSelfClose(
|
|
658
|
-
sceneId: string,
|
|
659
|
-
action: string,
|
|
660
|
-
onCovered: CoveredPolicy,
|
|
661
|
-
entry: PendingCloseEntry,
|
|
662
|
-
next: (sceneId: string, action: string, params: DispatchParams) => Promise<DispatchResult>,
|
|
663
|
-
): Promise<DispatchResult> {
|
|
664
|
-
const includeStackCover = action === 'close';
|
|
665
|
-
let immediateCoveredRetries = 0;
|
|
666
|
-
|
|
667
|
-
while (true) {
|
|
668
|
-
if (lifecycleHandler.getSceneState(sceneId) === 'destroyed') {
|
|
669
|
-
return { ok: false, code: 'scene_not_found', message: 'Target scene is already destroyed.' };
|
|
670
|
-
}
|
|
671
|
-
|
|
672
|
-
if (await this.isSceneCovered(sceneId, includeStackCover)) {
|
|
673
|
-
if (onCovered === 'defer') {
|
|
674
|
-
const waitResult = await this.waitForDeferredUncover(
|
|
675
|
-
sceneId,
|
|
676
|
-
entry,
|
|
677
|
-
includeStackCover,
|
|
678
|
-
);
|
|
679
|
-
if (!waitResult.uncovered) {
|
|
680
|
-
return { ok: false, code: 'scene_not_found' };
|
|
681
|
-
}
|
|
682
|
-
immediateCoveredRetries = 0;
|
|
683
|
-
continue;
|
|
684
|
-
}
|
|
685
|
-
return {
|
|
686
|
-
ok: false,
|
|
687
|
-
code: 'covered',
|
|
688
|
-
message: 'Target scene is covered by another scene.',
|
|
689
|
-
};
|
|
690
|
-
}
|
|
691
|
-
|
|
692
|
-
const registration = this.getTopExitHandler(sceneId);
|
|
693
|
-
if (registration) {
|
|
694
|
-
try {
|
|
695
|
-
await this.runExitWithTimeout(registration);
|
|
696
|
-
} catch (error) {
|
|
697
|
-
registration.onRestore?.('exit_error');
|
|
698
|
-
return {
|
|
699
|
-
ok: false,
|
|
700
|
-
code: 'invalid_action',
|
|
701
|
-
message: error instanceof Error ? error.message : String(error),
|
|
702
|
-
};
|
|
703
|
-
}
|
|
704
|
-
|
|
705
|
-
if (await this.isSceneCovered(sceneId, includeStackCover)) {
|
|
706
|
-
registration.onRestore?.('covered');
|
|
707
|
-
if (onCovered === 'defer') {
|
|
708
|
-
const waitResult = await this.waitForDeferredUncover(
|
|
709
|
-
sceneId,
|
|
710
|
-
entry,
|
|
711
|
-
includeStackCover,
|
|
712
|
-
);
|
|
713
|
-
if (!waitResult.uncovered) {
|
|
714
|
-
return { ok: false, code: 'scene_not_found' };
|
|
715
|
-
}
|
|
716
|
-
immediateCoveredRetries = 0;
|
|
717
|
-
continue;
|
|
718
|
-
}
|
|
719
|
-
return {
|
|
720
|
-
ok: false,
|
|
721
|
-
code: 'covered',
|
|
722
|
-
message: 'Target scene became covered during exit.',
|
|
723
|
-
};
|
|
724
|
-
}
|
|
725
|
-
}
|
|
726
|
-
|
|
727
|
-
let result: DispatchResult;
|
|
728
|
-
try {
|
|
729
|
-
result = await next(sceneId, action, entry.nativeParams);
|
|
730
|
-
} catch (error) {
|
|
731
|
-
registration?.onRestore?.('invalid_action');
|
|
732
|
-
throw error;
|
|
733
|
-
}
|
|
734
|
-
if (result.ok) {
|
|
735
|
-
this.presentingOwnerByChild.delete(sceneId);
|
|
736
|
-
return result;
|
|
737
|
-
}
|
|
738
|
-
|
|
739
|
-
registration?.onRestore?.(result.code);
|
|
740
|
-
if (result.code === 'covered' && onCovered === 'defer') {
|
|
741
|
-
const waitResult = await this.waitForDeferredUncover(
|
|
742
|
-
sceneId,
|
|
743
|
-
entry,
|
|
744
|
-
includeStackCover,
|
|
745
|
-
);
|
|
746
|
-
if (!waitResult.uncovered) {
|
|
747
|
-
return { ok: false, code: 'scene_not_found' };
|
|
748
|
-
}
|
|
749
|
-
if (waitResult.waited) {
|
|
750
|
-
immediateCoveredRetries = 0;
|
|
751
|
-
} else {
|
|
752
|
-
immediateCoveredRetries += 1;
|
|
753
|
-
if (immediateCoveredRetries > MAX_IMMEDIATE_COVERED_RETRIES) {
|
|
754
|
-
return result;
|
|
755
|
-
}
|
|
756
|
-
}
|
|
757
|
-
continue;
|
|
758
|
-
}
|
|
759
|
-
return result;
|
|
760
|
-
}
|
|
761
|
-
}
|
|
762
|
-
|
|
763
|
-
onSceneDestroyed(sceneId: string) {
|
|
764
|
-
this.exitHandlers.delete(sceneId);
|
|
765
|
-
this.coverageWatchers.delete(sceneId);
|
|
766
|
-
if (this.coverageWatchers.size === 0) {
|
|
767
|
-
this.lastKnownLayers = null;
|
|
768
|
-
if (this.coverageLifecycleAttached) {
|
|
769
|
-
this.coverageLifecycleAttached = false;
|
|
770
|
-
lifecycleHandler.removeGlobalLifecycleListener(this.onCoverageLifecycle);
|
|
771
|
-
}
|
|
772
|
-
}
|
|
773
|
-
const waiters = this.uncoveredWaiters.get(sceneId);
|
|
774
|
-
if (waiters) {
|
|
775
|
-
this.uncoveredWaiters.delete(sceneId);
|
|
776
|
-
waiters.forEach(resolve => resolve({ uncovered: false, waited: false }));
|
|
777
|
-
}
|
|
778
|
-
|
|
779
|
-
this.presentingOwnerByChild.delete(sceneId);
|
|
780
|
-
for (const [childId, ownerId] of this.presentingOwnerByChild) {
|
|
781
|
-
if (ownerId === sceneId) {
|
|
782
|
-
this.presentingOwnerByChild.delete(childId);
|
|
783
|
-
}
|
|
784
|
-
}
|
|
785
|
-
}
|
|
786
|
-
|
|
787
|
-
onRootReset() {
|
|
788
|
-
for (const waiters of this.uncoveredWaiters.values()) {
|
|
789
|
-
waiters.forEach(resolve => resolve({ uncovered: false, waited: false }));
|
|
790
|
-
}
|
|
791
|
-
this.uncoveredWaiters.clear();
|
|
792
|
-
this.exitHandlers.clear();
|
|
793
|
-
this.presentingOwnerByChild.clear();
|
|
794
|
-
this.coverageCheckVersion += 1;
|
|
795
|
-
this.lastKnownLayers = null;
|
|
796
|
-
this.coverageWatchers.clear();
|
|
797
|
-
this.coverageRefreshScheduled = false;
|
|
798
|
-
this.coverageRecheckQueued = false;
|
|
799
|
-
if (this.coverageLifecycleAttached) {
|
|
800
|
-
this.coverageLifecycleAttached = false;
|
|
801
|
-
lifecycleHandler.removeGlobalLifecycleListener(this.onCoverageLifecycle);
|
|
802
|
-
}
|
|
803
|
-
}
|
|
804
|
-
}
|
|
805
|
-
|
|
806
|
-
export const presentationCoordinator = new PresentationCoordinator();
|