hybrid-navigation 4.1.0 → 5.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (148) hide show
  1. package/HybridNavigation.podspec +9 -1
  2. package/README.md +12 -4
  3. package/android/build.gradle +4 -4
  4. package/android/proguard-rules.pro +0 -14
  5. package/android/src/main/java/com/navigation/androidx/AwesomeActivity.java +114 -23
  6. package/android/src/main/java/com/navigation/androidx/AwesomeFragment.java +115 -12
  7. package/android/src/main/java/com/navigation/androidx/DrawableUtils.java +83 -18
  8. package/android/src/main/java/com/navigation/androidx/DrawerFragment.java +185 -25
  9. package/android/src/main/java/com/navigation/androidx/FragmentHelper.java +11 -0
  10. package/android/src/main/java/com/navigation/androidx/HybridDrawerLayout.java +563 -143
  11. package/android/src/main/java/com/navigation/androidx/LifecycleDelegate.java +39 -12
  12. package/android/src/main/java/com/navigation/androidx/PresentableActivity.java +3 -0
  13. package/android/src/main/java/com/navigation/androidx/PresentationDelegate.java +8 -3
  14. package/android/src/main/java/com/navigation/androidx/StackDelegate.java +29 -9
  15. package/android/src/main/java/com/navigation/androidx/StackFragment.java +28 -3
  16. package/android/src/main/java/com/navigation/androidx/SystemUI.java +59 -90
  17. package/android/src/main/java/com/navigation/androidx/TabBar.java +21 -2
  18. package/android/src/main/java/com/navigation/androidx/TabBarFragment.java +24 -7
  19. package/android/src/main/java/com/navigation/androidx/TabView.java +11 -2
  20. package/android/src/main/java/com/reactnative/hybridnavigation/DispatchResult.java +59 -0
  21. package/android/src/main/java/com/reactnative/hybridnavigation/Garden.java +18 -10
  22. package/android/src/main/java/com/reactnative/hybridnavigation/GlobalStyle.java +15 -2
  23. package/android/src/main/java/com/reactnative/hybridnavigation/HBDRootView.java +3 -8
  24. package/android/src/main/java/com/reactnative/hybridnavigation/HybridFragment.java +113 -6
  25. package/android/src/main/java/com/reactnative/hybridnavigation/HybridNavigationPackage.java +13 -24
  26. package/android/src/main/java/com/reactnative/hybridnavigation/NativeEvent.java +5 -1
  27. package/android/src/main/java/com/reactnative/hybridnavigation/NativeNavigation.java +196 -194
  28. package/android/src/main/java/com/reactnative/hybridnavigation/NavigationCommandQueue.java +499 -0
  29. package/android/src/main/java/com/reactnative/hybridnavigation/Navigator.java +4 -1
  30. package/android/src/main/java/com/reactnative/hybridnavigation/OrientationCoordinator.java +162 -0
  31. package/android/src/main/java/com/reactnative/hybridnavigation/ReactAppCompatActivity.java +103 -66
  32. package/android/src/main/java/com/reactnative/hybridnavigation/ReactFragment.java +50 -89
  33. package/android/src/main/java/com/reactnative/hybridnavigation/ReactManager.java +156 -77
  34. package/android/src/main/java/com/reactnative/hybridnavigation/ReactStackFragment.java +6 -6
  35. package/android/src/main/java/com/reactnative/hybridnavigation/ReactTabBarProvider.java +8 -1
  36. package/android/src/main/java/com/reactnative/hybridnavigation/SceneLifecycleState.java +21 -0
  37. package/android/src/main/java/com/reactnative/hybridnavigation/SceneLifecycleStateMachine.java +116 -0
  38. package/android/src/main/java/com/reactnative/hybridnavigation/ScreenOrientation.java +42 -0
  39. package/android/src/main/java/com/reactnative/hybridnavigation/Utils.java +17 -0
  40. package/android/src/main/java/com/reactnative/hybridnavigation/navigator/DrawerNavigator.java +14 -6
  41. package/android/src/main/java/com/reactnative/hybridnavigation/navigator/ScreenNavigator.java +62 -22
  42. package/android/src/main/java/com/reactnative/hybridnavigation/navigator/StackNavigator.java +35 -16
  43. package/android/src/main/java/com/reactnative/hybridnavigation/navigator/TabNavigator.java +16 -13
  44. package/dist/DeepLink.d.ts +3 -2
  45. package/dist/DeepLink.js +37 -14
  46. package/dist/NativeEvent.d.ts +4 -12
  47. package/dist/NativeNavigation.d.ts +17 -19
  48. package/dist/Navigation.d.ts +21 -19
  49. package/dist/Navigation.js +83 -89
  50. package/dist/NavigationComponentWrap.d.ts +4 -3
  51. package/dist/NavigationComponentWrap.js +3 -6
  52. package/dist/Navigator.d.ts +31 -42
  53. package/dist/Navigator.js +97 -90
  54. package/dist/Options.d.ts +3 -1
  55. package/dist/Route.d.ts +85 -18
  56. package/dist/handler/DispatchCommandHandler.d.ts +10 -6
  57. package/dist/handler/DispatchCommandHandler.js +41 -17
  58. package/dist/handler/LayoutCommandHandler.d.ts +7 -7
  59. package/dist/handler/LayoutCommandHandler.js +21 -15
  60. package/dist/handler/LifecycleEventHandler.d.ts +26 -0
  61. package/dist/handler/LifecycleEventHandler.js +136 -0
  62. package/dist/handler/ResultEventHandler.d.ts +14 -9
  63. package/dist/handler/ResultEventHandler.js +88 -13
  64. package/dist/hooks.d.ts +5 -2
  65. package/dist/hooks.js +26 -4
  66. package/dist/index.d.ts +13 -0
  67. package/dist/index.js +0 -2
  68. package/dist/routeConfigRegistry.d.ts +6 -0
  69. package/dist/routeConfigRegistry.js +19 -0
  70. package/dist/router/drawer.d.ts +2 -2
  71. package/dist/router/drawer.js +6 -3
  72. package/dist/router/index.d.ts +8 -5
  73. package/dist/router/index.js +127 -43
  74. package/dist/router/screen.d.ts +2 -2
  75. package/dist/router/screen.js +1 -1
  76. package/dist/router/stack.d.ts +2 -2
  77. package/dist/router/stack.js +59 -30
  78. package/dist/router/tabs.d.ts +2 -2
  79. package/dist/router/tabs.js +4 -1
  80. package/dist/store.d.ts +2 -2
  81. package/ios/HBDGlobalStyle.h +26 -0
  82. package/ios/{GlobalStyle.mm → HBDGlobalStyle.mm} +30 -29
  83. package/ios/HybridNavigation.h +4 -1
  84. package/ios/Navigator/HBDDrawerNavigator.mm +24 -8
  85. package/ios/Navigator/HBDNavigatorRegistry.mm +1 -0
  86. package/ios/Navigator/HBDScreenNavigator.mm +106 -52
  87. package/ios/Navigator/HBDStackNavigator.mm +109 -61
  88. package/ios/Navigator/HBDTabNavigator.mm +32 -14
  89. package/ios/React/HBDNativeNavigation.mm +109 -131
  90. package/ios/React/HBDNavigator.h +29 -5
  91. package/ios/React/HBDOrientationCoordinator.h +27 -0
  92. package/ios/React/HBDOrientationCoordinator.mm +282 -0
  93. package/ios/React/HBDReactBridgeManager.h +36 -20
  94. package/ios/React/HBDReactBridgeManager.mm +450 -108
  95. package/ios/React/HBDSceneLifecycleStateMachine.h +38 -0
  96. package/ios/React/HBDSceneLifecycleStateMachine.mm +180 -0
  97. package/ios/View/HBDRootView.h +2 -0
  98. package/ios/View/HBDRootView.mm +148 -0
  99. package/ios/View/UIImage+WithBadge.h +3 -3
  100. package/ios/View/UIImage+WithBadge.mm +10 -15
  101. package/ios/ViewController/HBDDrawerController.mm +126 -5
  102. package/ios/ViewController/HBDNavigationController.mm +17 -4
  103. package/ios/ViewController/HBDReactViewController.mm +59 -121
  104. package/ios/ViewController/HBDTabBarController.h +4 -0
  105. package/ios/ViewController/HBDTabBarController.mm +59 -44
  106. package/ios/ViewController/HBDViewController.h +10 -3
  107. package/ios/ViewController/HBDViewController.mm +161 -159
  108. package/ios/ViewController/UINavigationController+HBD.h +2 -2
  109. package/ios/ViewController/UINavigationController+HBD.mm +3 -3
  110. package/ios/ViewController/UIViewController+HBD.h +17 -10
  111. package/ios/ViewController/UIViewController+HBD.mm +85 -69
  112. package/package.json +1 -1
  113. package/src/DeepLink.ts +41 -14
  114. package/src/NativeEvent.ts +4 -14
  115. package/src/NativeNavigation.ts +22 -33
  116. package/src/Navigation.ts +162 -121
  117. package/src/NavigationComponentWrap.tsx +9 -9
  118. package/src/Navigator.ts +191 -153
  119. package/src/Options.ts +3 -1
  120. package/src/Route.ts +134 -19
  121. package/src/handler/DispatchCommandHandler.ts +55 -21
  122. package/src/handler/LayoutCommandHandler.ts +36 -17
  123. package/src/handler/LifecycleEventHandler.ts +172 -0
  124. package/src/handler/ResultEventHandler.ts +112 -19
  125. package/src/hooks.ts +39 -7
  126. package/src/index.ts +26 -2
  127. package/src/routeConfigRegistry.ts +26 -0
  128. package/src/router/drawer.ts +8 -5
  129. package/src/router/index.ts +161 -50
  130. package/src/router/screen.ts +3 -3
  131. package/src/router/stack.ts +80 -29
  132. package/src/router/tabs.ts +15 -7
  133. package/src/store.ts +1 -1
  134. package/android/src/main/java/androidx/fragment/app/InternalFragment.java +0 -22
  135. package/android/src/main/java/com/navigation/androidx/SystemUI30.java +0 -87
  136. package/android/src/main/java/com/reactnative/hybridnavigation/NativeGarden.java +0 -176
  137. package/android/src/main/java/com/reactnative/hybridnavigation/ReactTabBar.java +0 -130
  138. package/android/src/main/res/layout/nav_tab_bar_react.xml +0 -32
  139. package/android/src/main/res/layout/nav_tab_bar_react_indeterminate.xml +0 -32
  140. package/dist/NativeGarden.d.ts +0 -13
  141. package/dist/NativeGarden.js +0 -2
  142. package/dist/handler/VisibilityEventHandler.d.ts +0 -13
  143. package/dist/handler/VisibilityEventHandler.js +0 -39
  144. package/ios/GlobalStyle.h +0 -15
  145. package/ios/React/HBDNativeGarden.h +0 -10
  146. package/ios/React/HBDNativeGarden.mm +0 -111
  147. package/src/NativeGarden.ts +0 -16
  148. package/src/handler/VisibilityEventHandler.ts +0 -52
@@ -1,9 +1,37 @@
1
- import NativeNavigation from '../NativeNavigation';
1
+ import NativeNavigation, { DispatchResultWire } from '../NativeNavigation';
2
2
  import { NavigationItem } from '../Options';
3
- import { BuildInLayout, Layout } from '../Route';
3
+ import type { BuildInLayout, DispatchResult, DispatchResultCode, Layout } from '../Route';
4
+
5
+ const VALID_DISPATCH_CODES = new Set<DispatchResultCode>([
6
+ 'ok',
7
+ 'intercepted',
8
+ 'scene_not_found',
9
+ 'already_presenting',
10
+ 'not_ready',
11
+ 'superseded',
12
+ 'invalid_action',
13
+ 'host_unavailable',
14
+ ]);
15
+
16
+ export function normalizeDispatchResult(
17
+ wire: DispatchResultWire | null | undefined,
18
+ ): DispatchResult {
19
+ if (!wire) {
20
+ return { ok: false, code: 'invalid_action' };
21
+ }
22
+ const code: DispatchResultCode = VALID_DISPATCH_CODES.has(wire.code as DispatchResultCode)
23
+ ? (wire.code as DispatchResultCode)
24
+ : wire.ok
25
+ ? 'ok'
26
+ : 'invalid_action';
27
+ return wire.message
28
+ ? { ok: Boolean(wire.ok), code, message: wire.message }
29
+ : { ok: Boolean(wire.ok), code };
30
+ }
4
31
 
5
32
  export interface DispatchParams {
6
33
  animated?: boolean;
34
+ enabled?: boolean;
7
35
  moduleName?: string;
8
36
  from?: string | number;
9
37
  to?: string | number;
@@ -15,7 +43,7 @@ export interface DispatchParams {
15
43
  [index: string]: any;
16
44
  }
17
45
 
18
- interface InterceptParams {
46
+ export interface InterceptParams {
19
47
  sceneId: string;
20
48
  from?: number | string;
21
49
  to?: number | string;
@@ -28,9 +56,13 @@ export type NavigationInterceptor = (
28
56
  ) => boolean | Promise<boolean>;
29
57
 
30
58
  export default class DispatchCommandHandler {
31
- private interceptor: NavigationInterceptor = () => false;
59
+ private interceptors: NavigationInterceptor[] = [];
32
60
 
33
- async dispatch(sceneId: string, action: string, params: DispatchParams): Promise<boolean> {
61
+ async dispatch(
62
+ sceneId: string,
63
+ action: string,
64
+ params: DispatchParams,
65
+ ): Promise<DispatchResult> {
34
66
  const { from, to, props } = params;
35
67
  const intercepted = await this.intercept(action, {
36
68
  sceneId,
@@ -40,28 +72,30 @@ export default class DispatchCommandHandler {
40
72
  });
41
73
 
42
74
  if (intercepted) {
43
- return false;
75
+ return { ok: false, code: 'intercepted' };
44
76
  }
45
- return new Promise<boolean>(resolve => {
46
- NativeNavigation.dispatch(sceneId, action, params, (_, result) => {
47
- resolve(result);
48
- });
49
- });
77
+ const wire = await NativeNavigation.dispatch(sceneId, action, params);
78
+ return normalizeDispatchResult(wire);
50
79
  }
51
80
 
52
- private intercept(action: string, params: InterceptParams) {
53
- if (!this.interceptor) {
54
- return Promise.resolve(false);
81
+ private async intercept(action: string, params: InterceptParams): Promise<boolean> {
82
+ for (const interceptor of this.interceptors.slice()) {
83
+ const result = interceptor(action, params);
84
+ const intercepted = result instanceof Promise ? await result : result;
85
+ if (intercepted) {
86
+ return true;
87
+ }
55
88
  }
89
+ return false;
90
+ }
56
91
 
57
- const result = this.interceptor(action, params);
58
- if (result instanceof Promise) {
59
- return result;
60
- }
61
- return Promise.resolve(result);
92
+ addInterceptor(interceptor: NavigationInterceptor) {
93
+ this.interceptors.push(interceptor);
62
94
  }
63
95
 
64
- setInterceptor(interceptor: NavigationInterceptor) {
65
- this.interceptor = interceptor;
96
+ removeInterceptor(interceptor: NavigationInterceptor) {
97
+ this.interceptors = this.interceptors.filter(item => item !== interceptor);
66
98
  }
67
99
  }
100
+
101
+ export const dispatchHandler = new DispatchCommandHandler();
@@ -1,32 +1,51 @@
1
- import { BuildInLayout, Layout } from '../Route';
1
+ import type { BuildInLayout, DispatchResult, Layout, SetRootOptions } from '../Route';
2
2
  import NativeEvent from '../NativeEvent';
3
3
  import NativeNavigation from '../NativeNavigation';
4
+ import { normalizeDispatchResult } from './DispatchCommandHandler';
4
5
 
5
- export default class LayoutCommandHandler {
6
- private willSetRoot = () => {};
7
- private didSetRoot = () => {};
6
+ export type RootLayoutListener = () => void;
7
+
8
+ type RootLayoutSubscriptionRecord = {
9
+ willSetRoot?: RootLayoutListener;
10
+ didSetRoot?: RootLayoutListener;
11
+ };
8
12
 
9
- constructor() {}
13
+ export default class LayoutCommandHandler {
14
+ private listeners: RootLayoutSubscriptionRecord[] = [];
10
15
 
11
- handleRootLayoutChange() {
16
+ handleRootLayoutChange(onWillSetRoot?: () => void) {
12
17
  NativeEvent.willSetRoot(() => {
13
- this.willSetRoot();
18
+ onWillSetRoot?.();
19
+ for (const listener of this.listeners.slice()) {
20
+ listener.willSetRoot?.();
21
+ }
14
22
  });
15
23
  NativeEvent.didSetRoot(() => {
16
- this.didSetRoot();
24
+ for (const listener of this.listeners.slice()) {
25
+ listener.didSetRoot?.();
26
+ }
17
27
  });
18
28
  }
19
29
 
20
- setRoot(layout: BuildInLayout | Layout, sticky = false) {
21
- return new Promise<boolean>(resolve => {
22
- NativeNavigation.setRoot(layout, sticky, (_, success) => {
23
- resolve(success);
24
- });
25
- });
30
+ async setRoot(
31
+ layout: BuildInLayout | Layout,
32
+ options?: SetRootOptions,
33
+ ): Promise<DispatchResult> {
34
+ const sticky = Boolean(options?.stickyAndroid);
35
+ const wire = await NativeNavigation.setRoot(layout, sticky);
36
+ return normalizeDispatchResult(wire);
26
37
  }
27
38
 
28
- setRootLayoutUpdateListener(willSetRoot = () => {}, didSetRoot = () => {}) {
29
- this.willSetRoot = willSetRoot;
30
- this.didSetRoot = didSetRoot;
39
+ addRootLayoutUpdateListener(
40
+ willSetRoot?: RootLayoutListener,
41
+ didSetRoot?: RootLayoutListener,
42
+ ): () => void {
43
+ const record: RootLayoutSubscriptionRecord = { willSetRoot, didSetRoot };
44
+ this.listeners.push(record);
45
+ return () => {
46
+ this.listeners = this.listeners.filter(item => item !== record);
47
+ };
31
48
  }
32
49
  }
50
+
51
+ export const layoutHandler = new LayoutCommandHandler();
@@ -0,0 +1,172 @@
1
+ import type { SceneLifecycleState, Visibility } from '../Route';
2
+ import NativeEvent from '../NativeEvent';
3
+
4
+ export type SceneLifecycleListener = (
5
+ state: SceneLifecycleState,
6
+ prevState: SceneLifecycleState,
7
+ ) => void;
8
+ export type GlobalSceneLifecycleListener = (
9
+ sceneId: string,
10
+ state: SceneLifecycleState,
11
+ prevState: SceneLifecycleState,
12
+ ) => void;
13
+ export type VisibilityEventListener = (visibility: Visibility) => void;
14
+ export type GlobalVisibilityEventListener = (sceneId: string, visibility: Visibility) => void;
15
+
16
+ const VALID_STATES = new Set<SceneLifecycleState>([
17
+ 'created',
18
+ 'visible',
19
+ 'invisible',
20
+ 'destroyed',
21
+ ]);
22
+
23
+ function normalizeLifecycleState(raw: string): SceneLifecycleState {
24
+ if (VALID_STATES.has(raw as SceneLifecycleState)) {
25
+ return raw as SceneLifecycleState;
26
+ }
27
+ return 'created';
28
+ }
29
+
30
+ const MAX_DESTROYED_SCENES = 2000;
31
+
32
+ export default class LifecycleEventHandler {
33
+ private sceneStates = new Map<string, SceneLifecycleState>();
34
+ private destroyedScenes = new Set<string>();
35
+ private lifecycleListeners: Record<string, SceneLifecycleListener[]> = {};
36
+ private globalLifecycleListeners: GlobalSceneLifecycleListener[] = [];
37
+ private visibilityListeners: Record<string, VisibilityEventListener[]> = {};
38
+ private globalVisibilityListeners: GlobalVisibilityEventListener[] = [];
39
+
40
+ handleSceneLifecycle(onSceneDestroyed?: (sceneId: string) => void) {
41
+ NativeEvent.onSceneStateChange(({ sceneId, state, prevState }) => {
42
+ const next = normalizeLifecycleState(state);
43
+ const prev = normalizeLifecycleState(prevState);
44
+
45
+ if (next !== 'destroyed') {
46
+ this.destroyedScenes.delete(sceneId);
47
+ }
48
+ this.sceneStates.set(sceneId, next);
49
+
50
+ const visibilityChange: Visibility | null =
51
+ next === 'visible'
52
+ ? 'visible'
53
+ : prev === 'visible' && (next === 'invisible' || next === 'destroyed')
54
+ ? 'invisible'
55
+ : null;
56
+
57
+ if (visibilityChange) {
58
+ this.globalVisibilityListeners
59
+ .slice()
60
+ .forEach(listener => listener(sceneId, visibilityChange));
61
+ }
62
+
63
+ this.globalLifecycleListeners
64
+ .slice()
65
+ .forEach(listener => listener(sceneId, next, prev));
66
+
67
+ const sceneListeners = this.lifecycleListeners[sceneId];
68
+ if (sceneListeners) {
69
+ sceneListeners.slice().forEach(listener => listener(next, prev));
70
+ }
71
+
72
+ if (visibilityChange) {
73
+ const visListeners = this.visibilityListeners[sceneId];
74
+ if (visListeners) {
75
+ visListeners.slice().forEach(listener => listener(visibilityChange));
76
+ }
77
+ }
78
+
79
+ if (next === 'destroyed') {
80
+ this.markSceneDestroyed(sceneId);
81
+ onSceneDestroyed?.(sceneId);
82
+ this.cleanupScene(sceneId);
83
+ }
84
+ });
85
+ }
86
+
87
+ private markSceneDestroyed(sceneId: string) {
88
+ this.destroyedScenes.delete(sceneId);
89
+ this.destroyedScenes.add(sceneId);
90
+ if (this.destroyedScenes.size > MAX_DESTROYED_SCENES) {
91
+ const oldest = this.destroyedScenes.values().next().value;
92
+ if (oldest !== undefined) {
93
+ this.destroyedScenes.delete(oldest);
94
+ }
95
+ }
96
+ }
97
+
98
+ getSceneState(sceneId: string): SceneLifecycleState {
99
+ const activeState = this.sceneStates.get(sceneId);
100
+ if (activeState !== undefined) {
101
+ return activeState;
102
+ }
103
+ if (this.destroyedScenes.has(sceneId)) {
104
+ return 'destroyed';
105
+ }
106
+ return 'created';
107
+ }
108
+
109
+ cleanupScene(sceneId: string) {
110
+ this.sceneStates.delete(sceneId);
111
+ delete this.lifecycleListeners[sceneId];
112
+ delete this.visibilityListeners[sceneId];
113
+ }
114
+
115
+ addLifecycleListener(sceneId: string, listener: SceneLifecycleListener) {
116
+ if (!this.lifecycleListeners[sceneId]) {
117
+ this.lifecycleListeners[sceneId] = [];
118
+ }
119
+ this.lifecycleListeners[sceneId].push(listener);
120
+ }
121
+
122
+ removeLifecycleListener(sceneId: string, listener: SceneLifecycleListener) {
123
+ const list = this.lifecycleListeners[sceneId];
124
+ if (!list) {
125
+ return;
126
+ }
127
+ const next = list.filter(l => l !== listener);
128
+ if (next.length > 0) {
129
+ this.lifecycleListeners[sceneId] = next;
130
+ } else {
131
+ delete this.lifecycleListeners[sceneId];
132
+ }
133
+ }
134
+
135
+ addGlobalLifecycleListener(listener: GlobalSceneLifecycleListener) {
136
+ this.globalLifecycleListeners.push(listener);
137
+ }
138
+
139
+ removeGlobalLifecycleListener(listener: GlobalSceneLifecycleListener) {
140
+ this.globalLifecycleListeners = this.globalLifecycleListeners.filter(l => l !== listener);
141
+ }
142
+
143
+ addGlobalVisibilityEventListener(listener: GlobalVisibilityEventListener) {
144
+ this.globalVisibilityListeners.push(listener);
145
+ }
146
+
147
+ removeGlobalVisibilityEventListener(listener: GlobalVisibilityEventListener) {
148
+ this.globalVisibilityListeners = this.globalVisibilityListeners.filter(l => l !== listener);
149
+ }
150
+
151
+ addVisibilityEventListener(sceneId: string, listener: VisibilityEventListener) {
152
+ if (!this.visibilityListeners[sceneId]) {
153
+ this.visibilityListeners[sceneId] = [];
154
+ }
155
+ this.visibilityListeners[sceneId].push(listener);
156
+ }
157
+
158
+ removeVisibilityEventListener(sceneId: string, listener: VisibilityEventListener) {
159
+ const list = this.visibilityListeners[sceneId];
160
+ if (!list) {
161
+ return;
162
+ }
163
+ const next = list.filter(l => l !== listener);
164
+ if (next.length > 0) {
165
+ this.visibilityListeners[sceneId] = next;
166
+ } else {
167
+ delete this.visibilityListeners[sceneId];
168
+ }
169
+ }
170
+ }
171
+
172
+ export const lifecycleHandler = new LifecycleEventHandler();
@@ -1,23 +1,32 @@
1
+ import type { CancelReason, NavigationResult, ResultMap } from '../Route';
1
2
  import NativeEvent from '../NativeEvent';
3
+ import NativeNavigation from '../NativeNavigation';
2
4
 
3
- interface IndexType {
4
- [index: string]: any;
5
- }
5
+ export const { RESULT_CANCEL, RESULT_OK, RESULT_BLOCK } = NativeNavigation.getConstants();
6
6
 
7
- export type ResultType = IndexType | null;
8
- export type ResultEventListener<T extends ResultType> = (resultCode: number, data: T) => void;
7
+ export type ResultType = unknown;
8
+ export type ResultEventListener<T = unknown> = (result: NavigationResult<T>) => void;
9
+ export type PreparedResult<T = unknown> = {
10
+ promise: Promise<NavigationResult<T>>;
11
+ cancel: () => void;
12
+ };
9
13
 
10
14
  export default class ResultEventHandler {
11
15
  private listenerRecords: Record<string, Record<number, Array<ResultEventListener<any>>>> = {};
12
16
 
13
- constructor() {}
14
-
15
17
  handleComponentResult() {
16
18
  NativeEvent.onResult(({ sceneId, requestCode, resultCode, resultData }) => {
17
19
  const listeners = this.listenerRecords[sceneId]?.[requestCode];
18
20
  if (listeners) {
19
21
  delete this.listenerRecords[sceneId][requestCode];
20
- listeners.forEach(l => l(resultCode, resultData));
22
+ if (Object.keys(this.listenerRecords[sceneId]).length === 0) {
23
+ delete this.listenerRecords[sceneId];
24
+ }
25
+ const result: NavigationResult<any> =
26
+ resultCode === RESULT_CANCEL
27
+ ? { status: 'cancel', code: RESULT_CANCEL, reason: 'dismissed' }
28
+ : { status: 'ok', code: resultCode, data: resultData };
29
+ listeners.forEach(l => l(result));
21
30
  }
22
31
  });
23
32
  }
@@ -38,25 +47,109 @@ export default class ResultEventHandler {
38
47
  this.listenerRecords[sceneId][requestCode].push(listener);
39
48
  }
40
49
 
41
- private removeResultEventListener(sceneId: string, requestCode: number) {
42
- if (this.listenerRecords[sceneId]) {
43
- delete this.listenerRecords[sceneId][requestCode];
50
+ private removeResultEventListener(
51
+ sceneId: string,
52
+ requestCode: number,
53
+ listener: ResultEventListener<any>,
54
+ ) {
55
+ const sceneRecords = this.listenerRecords[sceneId];
56
+ const listeners = sceneRecords?.[requestCode];
57
+ if (!listeners) {
58
+ return;
59
+ }
60
+
61
+ const nextListeners = listeners.filter(l => l !== listener);
62
+ if (nextListeners.length > 0) {
63
+ sceneRecords[requestCode] = nextListeners;
64
+ } else {
65
+ delete sceneRecords[requestCode];
66
+ if (Object.keys(sceneRecords).length === 0) {
67
+ delete this.listenerRecords[sceneId];
68
+ }
69
+ }
70
+ }
71
+
72
+ invalidateResultEventListener(sceneId: string, reason: CancelReason = 'unmounted') {
73
+ const sceneRecords = this.listenerRecords[sceneId];
74
+ if (!sceneRecords) {
75
+ return;
76
+ }
77
+ delete this.listenerRecords[sceneId];
78
+ const cancelled: NavigationResult<never> = {
79
+ status: 'cancel',
80
+ code: RESULT_CANCEL,
81
+ reason,
82
+ };
83
+ for (const listeners of Object.values(sceneRecords)) {
84
+ listeners.forEach(l => l(cancelled));
44
85
  }
45
86
  }
46
87
 
47
- invalidateResultEventListener(sceneId: string) {
48
- if (this.listenerRecords[sceneId]) {
49
- delete this.listenerRecords[sceneId];
88
+ invalidateAll(reason: CancelReason = 'root_reset') {
89
+ const allRecords = this.listenerRecords;
90
+ this.listenerRecords = {};
91
+ const cancelled: NavigationResult<never> = {
92
+ status: 'cancel',
93
+ code: RESULT_CANCEL,
94
+ reason,
95
+ };
96
+ for (const sceneRecords of Object.values(allRecords)) {
97
+ for (const listeners of Object.values(sceneRecords)) {
98
+ listeners.forEach(l => l(cancelled));
99
+ }
50
100
  }
51
101
  }
52
102
 
53
- waitResult<T extends ResultType>(sceneId: string, requestCode: number): Promise<[number, T]> {
54
- return new Promise<[number, T]>(resolve => {
55
- const listener = (resultCode: number, data: T) => {
56
- this.removeResultEventListener(sceneId, requestCode);
57
- resolve([resultCode, data]);
103
+ prepareResult<T = unknown>(sceneId: string, requestCode: number): PreparedResult<T> {
104
+ let settled = false;
105
+ let listenerRef: ResultEventListener<T> | undefined;
106
+ const promise = new Promise<NavigationResult<T>>(resolve => {
107
+ const listener: ResultEventListener<T> = result => {
108
+ if (settled) {
109
+ return;
110
+ }
111
+ settled = true;
112
+ this.removeResultEventListener(sceneId, requestCode, listener);
113
+ resolve(result);
58
114
  };
115
+ listenerRef = listener;
59
116
  this.addResultEventListener(sceneId, requestCode, listener);
60
117
  });
118
+
119
+ const cancel = () => {
120
+ if (settled) {
121
+ return;
122
+ }
123
+ settled = true;
124
+ if (listenerRef) {
125
+ this.removeResultEventListener(sceneId, requestCode, listenerRef);
126
+ }
127
+ };
128
+
129
+ return { promise, cancel };
130
+ }
131
+
132
+ setResult<T extends ResultMap = ResultMap>(
133
+ sceneId: string,
134
+ resultCode: number,
135
+ data: T | null = null,
136
+ ): void {
137
+ if (resultCode < RESULT_OK) {
138
+ console.warn(
139
+ '`resultCode` can only be `RESULT_OK`, `RESULT_CANCEL` or an integer greater than 0.',
140
+ );
141
+ }
142
+ if (data !== null && (typeof data !== 'object' || Array.isArray(data))) {
143
+ console.warn('`setResult` data must be a plain object or null.');
144
+ NativeNavigation.setResult(sceneId, resultCode, null);
145
+ return;
146
+ }
147
+ NativeNavigation.setResult(sceneId, resultCode, data);
148
+ }
149
+
150
+ waitResult<T = unknown>(sceneId: string, requestCode: number): Promise<NavigationResult<T>> {
151
+ return this.prepareResult<T>(sceneId, requestCode).promise;
61
152
  }
62
153
  }
154
+
155
+ export const resultHandler = new ResultEventHandler();
package/src/hooks.ts CHANGED
@@ -1,17 +1,45 @@
1
- import { useContext, useEffect, useEffectEvent } from 'react';
1
+ import { useContext, useEffect, useEffectEvent, useState } from 'react';
2
2
  import Navigation from './Navigation';
3
3
  import { Navigator, NavigationContext } from './Navigator';
4
+ import type { RouteName, SceneLifecycleState } from './Route';
4
5
 
5
- export function useNavigator(): Navigator {
6
- return useContext<Navigator>(NavigationContext);
6
+ export type VisibleEffectCallback = () => void | (() => void);
7
+
8
+ export function useNavigator<Name extends RouteName = RouteName>(): Navigator<Name> {
9
+ const navigator = useContext<Navigator<Name> | null>(NavigationContext);
10
+ if (!navigator) {
11
+ throw new Error(
12
+ '[hybrid-navigation] useNavigator() must be used inside a screen registered with Navigation.registerComponent().',
13
+ );
14
+ }
15
+ return navigator;
16
+ }
17
+
18
+ export function useSceneState(): SceneLifecycleState {
19
+ const navigator = useNavigator();
20
+ const [state, setState] = useState<SceneLifecycleState>(() =>
21
+ Navigation.getSceneState(navigator.sceneId),
22
+ );
23
+
24
+ useEffect(() => {
25
+ setState(Navigation.getSceneState(navigator.sceneId));
26
+ const subscription = Navigation.addLifecycleListener(navigator.sceneId, nextState => {
27
+ setState(nextState);
28
+ });
29
+ return () => {
30
+ subscription.remove();
31
+ };
32
+ }, [navigator.sceneId]);
33
+
34
+ return state;
7
35
  }
8
36
 
9
- export function useVisibleEffect<T extends Function>(callback: T) {
37
+ export function useVisibleEffect(callback: VisibleEffectCallback) {
10
38
  const navigator = useNavigator();
11
39
  const event = useEffectEvent(callback);
12
40
 
13
41
  useEffect(() => {
14
- let destructor: (() => void) | undefined;
42
+ let destructor: (() => void) | void;
15
43
  if (navigator.visibility === 'visible') {
16
44
  destructor = event();
17
45
  }
@@ -19,13 +47,17 @@ export function useVisibleEffect<T extends Function>(callback: T) {
19
47
  if (v === 'visible') {
20
48
  destructor = event();
21
49
  } else {
22
- destructor && destructor();
50
+ if (typeof destructor === 'function') {
51
+ destructor();
52
+ }
23
53
  destructor = undefined;
24
54
  }
25
55
  });
26
56
  return () => {
27
57
  subscription.remove();
28
- destructor && destructor();
58
+ if (typeof destructor === 'function') {
59
+ destructor();
60
+ }
29
61
  };
30
62
  }, [navigator]);
31
63
  }
package/src/index.ts CHANGED
@@ -1,11 +1,35 @@
1
- import { withNavigation } from './NavigationComponentWrap';
2
1
  import navigation from './Navigation';
3
2
 
4
- navigation.setNavigationComponentWrap(withNavigation);
3
+ /**
4
+ * Augment this interface via `declare module 'hybrid-navigation'` to enable
5
+ * strongly typed route names, props, and navigation results.
6
+ */
7
+ export interface NavigationParamList {}
8
+
9
+ /**
10
+ * Augment this interface via `declare module 'hybrid-navigation'` to register
11
+ * strongly typed custom container layouts alongside built-in layouts.
12
+ */
13
+ export interface CustomLayoutMap {}
14
+
5
15
  const Navigation = navigation;
6
16
  export default Navigation;
7
17
 
8
18
  export { RESULT_OK, RESULT_CANCEL, RESULT_BLOCK } from './Navigation';
19
+ export type {
20
+ DispatchParams,
21
+ GlobalSceneLifecycleListener,
22
+ GlobalVisibilityEventListener,
23
+ HOC,
24
+ InterceptParams,
25
+ Navigation,
26
+ NavigationInterceptor,
27
+ NavigationSubscription,
28
+ ResultType,
29
+ RootLayoutListener,
30
+ SceneLifecycleListener,
31
+ VisibilityEventListener,
32
+ } from './Navigation';
9
33
  export { withNavigationItem } from './NavigationComponentWrap';
10
34
  export type { NavigationProps } from './NavigationComponentWrap';
11
35
 
@@ -0,0 +1,26 @@
1
+ import type { RouteConfig } from './Route';
2
+
3
+ const routeConfigs = new Map<string, RouteConfig>();
4
+ let routeConfigVersion = 0;
5
+
6
+ export function clearRouteConfigs() {
7
+ routeConfigs.clear();
8
+ routeConfigVersion++;
9
+ }
10
+
11
+ export function setRouteConfig(moduleName: string, config: RouteConfig) {
12
+ routeConfigs.set(moduleName, config);
13
+ routeConfigVersion++;
14
+ }
15
+
16
+ export function bumpRouteConfigVersion() {
17
+ routeConfigVersion++;
18
+ }
19
+
20
+ export function getRouteConfigs(): ReadonlyMap<string, RouteConfig> {
21
+ return routeConfigs;
22
+ }
23
+
24
+ export function getRouteConfigVersion(): number {
25
+ return routeConfigVersion;
26
+ }
@@ -1,5 +1,5 @@
1
1
  import { ScreenGraph, isTargetLocateIn } from './screen';
2
- import { LayoutMode, RouteGraph, RouteHandler, RouteInfo } from '../Route';
2
+ import { LayoutMode, RouteGraph, RouteHandler, RouteHandleResult, RouteInfo } from '../Route';
3
3
  import Navigation from '../Navigation';
4
4
 
5
5
  export interface DrawerGraph extends RouteGraph {
@@ -14,7 +14,7 @@ export function isDrawerGraph(graph: RouteGraph): graph is DrawerGraph {
14
14
  }
15
15
 
16
16
  export class DrawerRouteHandler implements RouteHandler {
17
- async process(graph: RouteGraph, target: RouteInfo): Promise<[boolean, RouteGraph | null]> {
17
+ async process(graph: RouteGraph, target: RouteInfo): Promise<RouteHandleResult> {
18
18
  if (!isDrawerGraph(graph)) {
19
19
  throw new Error(`${graph} is Not a DrawerGraph`);
20
20
  }
@@ -23,12 +23,15 @@ export class DrawerRouteHandler implements RouteHandler {
23
23
 
24
24
  const { moduleName, sceneId } = children[1];
25
25
  if (moduleName === target.moduleName) {
26
- await Navigation.dispatch(sceneId, 'openMenu');
27
- return [true, null];
26
+ const res = await Navigation.dispatch(sceneId, 'openMenu');
27
+ return [true, null, res];
28
28
  }
29
29
 
30
30
  if (isTargetLocateIn(children[0], target)) {
31
- Navigation.dispatch(sceneId, 'closeMenu', { from: moduleName });
31
+ const result = await Navigation.dispatch(sceneId, 'closeMenu', { from: moduleName });
32
+ if (result.code === 'intercepted') {
33
+ return [true, null, result];
34
+ }
32
35
  return [true, children[0]];
33
36
  }
34
37