hybrid-navigation 4.0.8 → 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 (149) hide show
  1. package/HybridNavigation.podspec +9 -1
  2. package/README.md +12 -4
  3. package/android/build.gradle +5 -4
  4. package/android/proguard-rules.pro +3 -0
  5. package/android/src/main/java/com/navigation/androidx/AwesomeActivity.java +114 -23
  6. package/android/src/main/java/com/navigation/androidx/AwesomeFragment.java +116 -13
  7. package/android/src/main/java/com/navigation/androidx/DrawableUtils.java +83 -18
  8. package/android/src/main/java/com/navigation/androidx/DrawerFragment.java +192 -28
  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 +47 -15
  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 -87
  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/ReactSurfaceCleanup.java +3 -1
  36. package/android/src/main/java/com/reactnative/hybridnavigation/ReactTabBarProvider.java +8 -1
  37. package/android/src/main/java/com/reactnative/hybridnavigation/SceneLifecycleState.java +21 -0
  38. package/android/src/main/java/com/reactnative/hybridnavigation/SceneLifecycleStateMachine.java +116 -0
  39. package/android/src/main/java/com/reactnative/hybridnavigation/ScreenOrientation.java +42 -0
  40. package/android/src/main/java/com/reactnative/hybridnavigation/Utils.java +17 -0
  41. package/android/src/main/java/com/reactnative/hybridnavigation/navigator/DrawerNavigator.java +14 -6
  42. package/android/src/main/java/com/reactnative/hybridnavigation/navigator/ScreenNavigator.java +62 -22
  43. package/android/src/main/java/com/reactnative/hybridnavigation/navigator/StackNavigator.java +35 -16
  44. package/android/src/main/java/com/reactnative/hybridnavigation/navigator/TabNavigator.java +16 -13
  45. package/dist/DeepLink.d.ts +3 -2
  46. package/dist/DeepLink.js +37 -14
  47. package/dist/NativeEvent.d.ts +4 -12
  48. package/dist/NativeNavigation.d.ts +17 -19
  49. package/dist/Navigation.d.ts +21 -19
  50. package/dist/Navigation.js +83 -89
  51. package/dist/NavigationComponentWrap.d.ts +4 -3
  52. package/dist/NavigationComponentWrap.js +3 -6
  53. package/dist/Navigator.d.ts +31 -42
  54. package/dist/Navigator.js +97 -90
  55. package/dist/Options.d.ts +3 -1
  56. package/dist/Route.d.ts +85 -18
  57. package/dist/handler/DispatchCommandHandler.d.ts +10 -6
  58. package/dist/handler/DispatchCommandHandler.js +41 -17
  59. package/dist/handler/LayoutCommandHandler.d.ts +7 -7
  60. package/dist/handler/LayoutCommandHandler.js +21 -15
  61. package/dist/handler/LifecycleEventHandler.d.ts +26 -0
  62. package/dist/handler/LifecycleEventHandler.js +136 -0
  63. package/dist/handler/ResultEventHandler.d.ts +14 -9
  64. package/dist/handler/ResultEventHandler.js +88 -13
  65. package/dist/hooks.d.ts +5 -2
  66. package/dist/hooks.js +26 -4
  67. package/dist/index.d.ts +13 -0
  68. package/dist/index.js +0 -2
  69. package/dist/routeConfigRegistry.d.ts +6 -0
  70. package/dist/routeConfigRegistry.js +19 -0
  71. package/dist/router/drawer.d.ts +2 -2
  72. package/dist/router/drawer.js +6 -3
  73. package/dist/router/index.d.ts +8 -5
  74. package/dist/router/index.js +127 -43
  75. package/dist/router/screen.d.ts +2 -2
  76. package/dist/router/screen.js +1 -1
  77. package/dist/router/stack.d.ts +2 -2
  78. package/dist/router/stack.js +59 -30
  79. package/dist/router/tabs.d.ts +2 -2
  80. package/dist/router/tabs.js +4 -1
  81. package/dist/store.d.ts +2 -2
  82. package/ios/HBDGlobalStyle.h +26 -0
  83. package/ios/{GlobalStyle.mm → HBDGlobalStyle.mm} +30 -27
  84. package/ios/HybridNavigation.h +4 -1
  85. package/ios/Navigator/HBDDrawerNavigator.mm +24 -8
  86. package/ios/Navigator/HBDNavigatorRegistry.mm +1 -0
  87. package/ios/Navigator/HBDScreenNavigator.mm +106 -52
  88. package/ios/Navigator/HBDStackNavigator.mm +109 -61
  89. package/ios/Navigator/HBDTabNavigator.mm +32 -14
  90. package/ios/React/HBDNativeNavigation.mm +109 -131
  91. package/ios/React/HBDNavigator.h +29 -5
  92. package/ios/React/HBDOrientationCoordinator.h +27 -0
  93. package/ios/React/HBDOrientationCoordinator.mm +282 -0
  94. package/ios/React/HBDReactBridgeManager.h +37 -19
  95. package/ios/React/HBDReactBridgeManager.mm +490 -96
  96. package/ios/React/HBDSceneLifecycleStateMachine.h +38 -0
  97. package/ios/React/HBDSceneLifecycleStateMachine.mm +180 -0
  98. package/ios/View/HBDRootView.h +2 -0
  99. package/ios/View/HBDRootView.mm +148 -0
  100. package/ios/View/UIImage+WithBadge.h +3 -3
  101. package/ios/View/UIImage+WithBadge.mm +10 -15
  102. package/ios/ViewController/HBDDrawerController.mm +127 -6
  103. package/ios/ViewController/HBDNavigationController.mm +17 -4
  104. package/ios/ViewController/HBDReactViewController.mm +59 -121
  105. package/ios/ViewController/HBDTabBarController.h +4 -0
  106. package/ios/ViewController/HBDTabBarController.mm +59 -44
  107. package/ios/ViewController/HBDViewController.h +10 -3
  108. package/ios/ViewController/HBDViewController.mm +161 -147
  109. package/ios/ViewController/UINavigationController+HBD.h +2 -2
  110. package/ios/ViewController/UINavigationController+HBD.mm +3 -3
  111. package/ios/ViewController/UIViewController+HBD.h +17 -10
  112. package/ios/ViewController/UIViewController+HBD.mm +85 -69
  113. package/package.json +1 -1
  114. package/src/DeepLink.ts +41 -14
  115. package/src/NativeEvent.ts +4 -14
  116. package/src/NativeNavigation.ts +22 -33
  117. package/src/Navigation.ts +162 -121
  118. package/src/NavigationComponentWrap.tsx +9 -9
  119. package/src/Navigator.ts +191 -153
  120. package/src/Options.ts +3 -1
  121. package/src/Route.ts +134 -19
  122. package/src/handler/DispatchCommandHandler.ts +55 -21
  123. package/src/handler/LayoutCommandHandler.ts +36 -17
  124. package/src/handler/LifecycleEventHandler.ts +172 -0
  125. package/src/handler/ResultEventHandler.ts +112 -19
  126. package/src/hooks.ts +39 -7
  127. package/src/index.ts +26 -2
  128. package/src/routeConfigRegistry.ts +26 -0
  129. package/src/router/drawer.ts +8 -5
  130. package/src/router/index.ts +161 -50
  131. package/src/router/screen.ts +3 -3
  132. package/src/router/stack.ts +80 -29
  133. package/src/router/tabs.ts +15 -7
  134. package/src/store.ts +1 -1
  135. package/android/src/main/java/androidx/fragment/app/InternalFragment.java +0 -22
  136. package/android/src/main/java/com/navigation/androidx/SystemUI30.java +0 -87
  137. package/android/src/main/java/com/reactnative/hybridnavigation/NativeGarden.java +0 -176
  138. package/android/src/main/java/com/reactnative/hybridnavigation/ReactTabBar.java +0 -130
  139. package/android/src/main/res/layout/nav_tab_bar_react.xml +0 -32
  140. package/android/src/main/res/layout/nav_tab_bar_react_indeterminate.xml +0 -32
  141. package/dist/NativeGarden.d.ts +0 -13
  142. package/dist/NativeGarden.js +0 -2
  143. package/dist/handler/VisibilityEventHandler.d.ts +0 -13
  144. package/dist/handler/VisibilityEventHandler.js +0 -39
  145. package/ios/GlobalStyle.h +0 -15
  146. package/ios/React/HBDNativeGarden.h +0 -10
  147. package/ios/React/HBDNativeGarden.mm +0 -111
  148. package/src/NativeGarden.ts +0 -16
  149. package/src/handler/VisibilityEventHandler.ts +0 -52
package/dist/Route.d.ts CHANGED
@@ -1,28 +1,88 @@
1
+ import type { NavigationItem } from './Options';
2
+ import type { CustomLayoutMap, NavigationParamList } from './index';
1
3
  export type Visibility = 'visible' | 'invisible' | 'pending';
2
- export interface Screen extends Layout {
4
+ export type SceneLifecycleState = 'created' | 'visible' | 'invisible' | 'destroyed';
5
+ export type DispatchResultCode = 'ok' | 'intercepted' | 'scene_not_found' | 'already_presenting' | 'not_ready' | 'superseded' | 'invalid_action' | 'host_unavailable';
6
+ export interface DispatchResult {
7
+ ok: boolean;
8
+ code: DispatchResultCode;
9
+ message?: string;
10
+ }
11
+ export interface SetRootOptions {
12
+ stickyAndroid?: boolean;
13
+ }
14
+ export type CancelReason = 'dismissed' | 'unmounted' | 'root_reset';
15
+ export type NavigationResult<T = unknown> = {
16
+ status: 'ok';
17
+ code: number;
18
+ data: T;
19
+ } | {
20
+ status: 'cancel';
21
+ code: number;
22
+ reason: CancelReason;
23
+ } | {
24
+ status: 'blocked';
25
+ code: number;
26
+ reason: DispatchResultCode;
27
+ };
28
+ export type ScreenRoute<Props extends Record<string, any> = Record<never, never>, Result = unknown> = {
29
+ props: Props;
30
+ result: Result;
31
+ };
32
+ export type HasTypedRoutes = keyof NavigationParamList extends never ? false : true;
33
+ export type RouteName = HasTypedRoutes extends true ? Extract<keyof NavigationParamList, string> : string;
34
+ type RawRouteEntry<Name extends string> = Name extends keyof NavigationParamList ? NavigationParamList[Name] : ScreenRoute<Record<string, any>, unknown>;
35
+ export type RoutePropsOf<Name extends string> = RawRouteEntry<Name> extends {
36
+ props: infer P;
37
+ } ? P : RawRouteEntry<Name> extends {
38
+ props?: infer P;
39
+ } ? [Exclude<P, undefined>] extends [never] ? Record<never, never> : Exclude<P, undefined> : Record<string, any>;
40
+ export type RouteResultOf<Name extends string> = RawRouteEntry<Name> extends {
41
+ result: infer R;
42
+ } ? R : RawRouteEntry<Name> extends {
43
+ result?: infer R;
44
+ } ? unknown extends R ? unknown : [Exclude<R, undefined>] extends [never] ? unknown : Exclude<R, undefined> : unknown;
45
+ export type ResultMap = Record<string, any> & {
46
+ [Symbol.iterator]?: never;
47
+ };
48
+ type IsAny<T> = 0 extends 1 & T ? true : false;
49
+ export type SetResultData<Name extends string> = IsAny<Name> extends true ? any : unknown extends RouteResultOf<Name> ? ResultMap : Extract<RouteResultOf<Name>, ResultMap>;
50
+ type RequiredKeysOf<T> = {
51
+ [K in keyof T]-?: {} extends Pick<T, K> ? never : K;
52
+ }[keyof T];
53
+ export type IsPropsRequired<P> = [RequiredKeysOf<P>] extends [never] ? false : true;
54
+ export type ScreenPropsArgs<Name extends string> = HasTypedRoutes extends true ? IsPropsRequired<RoutePropsOf<Name>> extends true ? [props: RoutePropsOf<Name>, options?: NavigationItem] : [props?: RoutePropsOf<Name>, options?: NavigationItem] : [props?: Record<string, any>, options?: NavigationItem];
55
+ export type Screen<Name extends RouteName = RouteName> = HasTypedRoutes extends true ? Name extends RouteName ? {
3
56
  screen: {
4
- moduleName: string;
5
- props?: object;
6
- options?: {};
57
+ moduleName: Name;
58
+ props?: RoutePropsOf<Name>;
59
+ options?: NavigationItem;
7
60
  };
8
- }
9
- export interface Stack extends Layout {
61
+ } : never : {
62
+ screen: {
63
+ moduleName: Name;
64
+ props?: Record<string, any>;
65
+ options?: NavigationItem;
66
+ };
67
+ };
68
+ export type TypedScreen = Screen<RouteName>;
69
+ export interface Stack {
10
70
  stack: {
11
- children: Array<BuildInLayout | Layout>;
71
+ children: Array<Layout>;
12
72
  options?: {};
13
73
  };
14
74
  }
15
- export interface Tabs extends Layout {
75
+ export interface Tabs {
16
76
  tabs: {
17
- children: Array<BuildInLayout | Layout>;
77
+ children: Array<Layout>;
18
78
  options?: {
19
79
  selectedIndex?: number;
20
80
  };
21
81
  };
22
82
  }
23
- export interface Drawer extends Layout {
83
+ export interface Drawer {
24
84
  drawer: {
25
- children: [BuildInLayout | Layout, BuildInLayout | Layout];
85
+ children: [Layout, Layout];
26
86
  options?: {
27
87
  maxDrawerWidth?: number;
28
88
  minDrawerMargin?: number;
@@ -30,10 +90,13 @@ export interface Drawer extends Layout {
30
90
  };
31
91
  };
32
92
  }
33
- export type BuildInLayout = Screen | Stack | Tabs | Drawer;
34
- export interface Layout {
35
- [index: string]: {};
36
- }
93
+ export type HasCustomLayouts = keyof CustomLayoutMap extends never ? false : true;
94
+ export type CustomLayout = HasCustomLayouts extends true ? {
95
+ [K in Extract<keyof CustomLayoutMap, string>]: Record<K, CustomLayoutMap[K]>;
96
+ }[Extract<keyof CustomLayoutMap, string>] : never;
97
+ export type BuiltInLayout = TypedScreen | Stack | Tabs | Drawer;
98
+ export type BuildInLayout = BuiltInLayout;
99
+ export type Layout = BuiltInLayout | CustomLayout;
37
100
  export type RouteMode = 'modal' | 'present' | 'push';
38
101
  export type LayoutMode = 'modal' | 'present' | 'normal';
39
102
  export interface Route {
@@ -51,8 +114,10 @@ export interface RouteGraph {
51
114
  }
52
115
  export interface RouteConfig {
53
116
  path?: string;
54
- dependency?: string;
117
+ dependency?: RouteName;
55
118
  mode?: RouteMode;
119
+ options?: NavigationItem;
120
+ lazy?: boolean;
56
121
  }
57
122
  export interface RouteData {
58
123
  moduleName: string;
@@ -68,12 +133,14 @@ export interface RouteInfo {
68
133
  props: object;
69
134
  options: object;
70
135
  }
136
+ export type RouteHandleResult = [boolean, RouteGraph | null, DispatchResult?];
71
137
  export interface RouteHandler {
72
138
  /**
73
139
  * @param graph - current RouteGraph to process
74
140
  * @param target - route may add to the graph
75
- * @returns [boolean, RouteGraph] - if the route is handled, return true and childGraph for next Handler to process, [false, graph] otherwise
141
+ * @returns [boolean, RouteGraph | null, DispatchResult?] - if the route is handled, return true and childGraph for next Handler to process, [false, graph] otherwise
76
142
  */
77
- process: (graph: RouteGraph, target: RouteInfo) => Promise<[boolean, RouteGraph | null]>;
143
+ process: (graph: RouteGraph, target: RouteInfo) => Promise<RouteHandleResult>;
78
144
  }
79
145
  export type RouteInterceptor = (path: string) => boolean | Promise<boolean>;
146
+ export {};
@@ -1,7 +1,10 @@
1
+ import { DispatchResultWire } from '../NativeNavigation';
1
2
  import { NavigationItem } from '../Options';
2
- import { BuildInLayout, Layout } from '../Route';
3
+ import type { BuildInLayout, DispatchResult, Layout } from '../Route';
4
+ export declare function normalizeDispatchResult(wire: DispatchResultWire | null | undefined): DispatchResult;
3
5
  export interface DispatchParams {
4
6
  animated?: boolean;
7
+ enabled?: boolean;
5
8
  moduleName?: string;
6
9
  from?: string | number;
7
10
  to?: string | number;
@@ -12,7 +15,7 @@ export interface DispatchParams {
12
15
  options?: NavigationItem;
13
16
  [index: string]: any;
14
17
  }
15
- interface InterceptParams {
18
+ export interface InterceptParams {
16
19
  sceneId: string;
17
20
  from?: number | string;
18
21
  to?: number | string;
@@ -20,9 +23,10 @@ interface InterceptParams {
20
23
  }
21
24
  export type NavigationInterceptor = (action: string, params: InterceptParams) => boolean | Promise<boolean>;
22
25
  export default class DispatchCommandHandler {
23
- private interceptor;
24
- dispatch(sceneId: string, action: string, params: DispatchParams): Promise<boolean>;
26
+ private interceptors;
27
+ dispatch(sceneId: string, action: string, params: DispatchParams): Promise<DispatchResult>;
25
28
  private intercept;
26
- setInterceptor(interceptor: NavigationInterceptor): void;
29
+ addInterceptor(interceptor: NavigationInterceptor): void;
30
+ removeInterceptor(interceptor: NavigationInterceptor): void;
27
31
  }
28
- export {};
32
+ export declare const dispatchHandler: DispatchCommandHandler;
@@ -1,6 +1,29 @@
1
1
  import NativeNavigation from '../NativeNavigation';
2
+ const VALID_DISPATCH_CODES = new Set([
3
+ 'ok',
4
+ 'intercepted',
5
+ 'scene_not_found',
6
+ 'already_presenting',
7
+ 'not_ready',
8
+ 'superseded',
9
+ 'invalid_action',
10
+ 'host_unavailable',
11
+ ]);
12
+ export function normalizeDispatchResult(wire) {
13
+ if (!wire) {
14
+ return { ok: false, code: 'invalid_action' };
15
+ }
16
+ const code = VALID_DISPATCH_CODES.has(wire.code)
17
+ ? wire.code
18
+ : wire.ok
19
+ ? 'ok'
20
+ : 'invalid_action';
21
+ return wire.message
22
+ ? { ok: Boolean(wire.ok), code, message: wire.message }
23
+ : { ok: Boolean(wire.ok), code };
24
+ }
2
25
  export default class DispatchCommandHandler {
3
- interceptor = () => false;
26
+ interceptors = [];
4
27
  async dispatch(sceneId, action, params) {
5
28
  const { from, to, props } = params;
6
29
  const intercepted = await this.intercept(action, {
@@ -10,25 +33,26 @@ export default class DispatchCommandHandler {
10
33
  props,
11
34
  });
12
35
  if (intercepted) {
13
- return false;
36
+ return { ok: false, code: 'intercepted' };
14
37
  }
15
- return new Promise(resolve => {
16
- NativeNavigation.dispatch(sceneId, action, params, (_, result) => {
17
- resolve(result);
18
- });
19
- });
38
+ const wire = await NativeNavigation.dispatch(sceneId, action, params);
39
+ return normalizeDispatchResult(wire);
20
40
  }
21
- intercept(action, params) {
22
- if (!this.interceptor) {
23
- return Promise.resolve(false);
41
+ async intercept(action, params) {
42
+ for (const interceptor of this.interceptors.slice()) {
43
+ const result = interceptor(action, params);
44
+ const intercepted = result instanceof Promise ? await result : result;
45
+ if (intercepted) {
46
+ return true;
47
+ }
24
48
  }
25
- const result = this.interceptor(action, params);
26
- if (result instanceof Promise) {
27
- return result;
28
- }
29
- return Promise.resolve(result);
49
+ return false;
50
+ }
51
+ addInterceptor(interceptor) {
52
+ this.interceptors.push(interceptor);
30
53
  }
31
- setInterceptor(interceptor) {
32
- this.interceptor = interceptor;
54
+ removeInterceptor(interceptor) {
55
+ this.interceptors = this.interceptors.filter(item => item !== interceptor);
33
56
  }
34
57
  }
58
+ export const dispatchHandler = new DispatchCommandHandler();
@@ -1,9 +1,9 @@
1
- import { BuildInLayout, Layout } from '../Route';
1
+ import type { BuildInLayout, DispatchResult, Layout, SetRootOptions } from '../Route';
2
+ export type RootLayoutListener = () => void;
2
3
  export default class LayoutCommandHandler {
3
- private willSetRoot;
4
- private didSetRoot;
5
- constructor();
6
- handleRootLayoutChange(): void;
7
- setRoot(layout: BuildInLayout | Layout, sticky?: boolean): Promise<boolean>;
8
- setRootLayoutUpdateListener(willSetRoot?: () => void, didSetRoot?: () => void): void;
4
+ private listeners;
5
+ handleRootLayoutChange(onWillSetRoot?: () => void): void;
6
+ setRoot(layout: BuildInLayout | Layout, options?: SetRootOptions): Promise<DispatchResult>;
7
+ addRootLayoutUpdateListener(willSetRoot?: RootLayoutListener, didSetRoot?: RootLayoutListener): () => void;
9
8
  }
9
+ export declare const layoutHandler: LayoutCommandHandler;
@@ -1,26 +1,32 @@
1
1
  import NativeEvent from '../NativeEvent';
2
2
  import NativeNavigation from '../NativeNavigation';
3
+ import { normalizeDispatchResult } from './DispatchCommandHandler';
3
4
  export default class LayoutCommandHandler {
4
- willSetRoot = () => { };
5
- didSetRoot = () => { };
6
- constructor() { }
7
- handleRootLayoutChange() {
5
+ listeners = [];
6
+ handleRootLayoutChange(onWillSetRoot) {
8
7
  NativeEvent.willSetRoot(() => {
9
- this.willSetRoot();
8
+ onWillSetRoot?.();
9
+ for (const listener of this.listeners.slice()) {
10
+ listener.willSetRoot?.();
11
+ }
10
12
  });
11
13
  NativeEvent.didSetRoot(() => {
12
- this.didSetRoot();
14
+ for (const listener of this.listeners.slice()) {
15
+ listener.didSetRoot?.();
16
+ }
13
17
  });
14
18
  }
15
- setRoot(layout, sticky = false) {
16
- return new Promise(resolve => {
17
- NativeNavigation.setRoot(layout, sticky, (_, success) => {
18
- resolve(success);
19
- });
20
- });
19
+ async setRoot(layout, options) {
20
+ const sticky = Boolean(options?.stickyAndroid);
21
+ const wire = await NativeNavigation.setRoot(layout, sticky);
22
+ return normalizeDispatchResult(wire);
21
23
  }
22
- setRootLayoutUpdateListener(willSetRoot = () => { }, didSetRoot = () => { }) {
23
- this.willSetRoot = willSetRoot;
24
- this.didSetRoot = didSetRoot;
24
+ addRootLayoutUpdateListener(willSetRoot, didSetRoot) {
25
+ const record = { willSetRoot, didSetRoot };
26
+ this.listeners.push(record);
27
+ return () => {
28
+ this.listeners = this.listeners.filter(item => item !== record);
29
+ };
25
30
  }
26
31
  }
32
+ export const layoutHandler = new LayoutCommandHandler();
@@ -0,0 +1,26 @@
1
+ import type { SceneLifecycleState, Visibility } from '../Route';
2
+ export type SceneLifecycleListener = (state: SceneLifecycleState, prevState: SceneLifecycleState) => void;
3
+ export type GlobalSceneLifecycleListener = (sceneId: string, state: SceneLifecycleState, prevState: SceneLifecycleState) => void;
4
+ export type VisibilityEventListener = (visibility: Visibility) => void;
5
+ export type GlobalVisibilityEventListener = (sceneId: string, visibility: Visibility) => void;
6
+ export default class LifecycleEventHandler {
7
+ private sceneStates;
8
+ private destroyedScenes;
9
+ private lifecycleListeners;
10
+ private globalLifecycleListeners;
11
+ private visibilityListeners;
12
+ private globalVisibilityListeners;
13
+ handleSceneLifecycle(onSceneDestroyed?: (sceneId: string) => void): void;
14
+ private markSceneDestroyed;
15
+ getSceneState(sceneId: string): SceneLifecycleState;
16
+ cleanupScene(sceneId: string): void;
17
+ addLifecycleListener(sceneId: string, listener: SceneLifecycleListener): void;
18
+ removeLifecycleListener(sceneId: string, listener: SceneLifecycleListener): void;
19
+ addGlobalLifecycleListener(listener: GlobalSceneLifecycleListener): void;
20
+ removeGlobalLifecycleListener(listener: GlobalSceneLifecycleListener): void;
21
+ addGlobalVisibilityEventListener(listener: GlobalVisibilityEventListener): void;
22
+ removeGlobalVisibilityEventListener(listener: GlobalVisibilityEventListener): void;
23
+ addVisibilityEventListener(sceneId: string, listener: VisibilityEventListener): void;
24
+ removeVisibilityEventListener(sceneId: string, listener: VisibilityEventListener): void;
25
+ }
26
+ export declare const lifecycleHandler: LifecycleEventHandler;
@@ -0,0 +1,136 @@
1
+ import NativeEvent from '../NativeEvent';
2
+ const VALID_STATES = new Set([
3
+ 'created',
4
+ 'visible',
5
+ 'invisible',
6
+ 'destroyed',
7
+ ]);
8
+ function normalizeLifecycleState(raw) {
9
+ if (VALID_STATES.has(raw)) {
10
+ return raw;
11
+ }
12
+ return 'created';
13
+ }
14
+ const MAX_DESTROYED_SCENES = 2000;
15
+ export default class LifecycleEventHandler {
16
+ sceneStates = new Map();
17
+ destroyedScenes = new Set();
18
+ lifecycleListeners = {};
19
+ globalLifecycleListeners = [];
20
+ visibilityListeners = {};
21
+ globalVisibilityListeners = [];
22
+ handleSceneLifecycle(onSceneDestroyed) {
23
+ NativeEvent.onSceneStateChange(({ sceneId, state, prevState }) => {
24
+ const next = normalizeLifecycleState(state);
25
+ const prev = normalizeLifecycleState(prevState);
26
+ if (next !== 'destroyed') {
27
+ this.destroyedScenes.delete(sceneId);
28
+ }
29
+ this.sceneStates.set(sceneId, next);
30
+ const visibilityChange = next === 'visible'
31
+ ? 'visible'
32
+ : prev === 'visible' && (next === 'invisible' || next === 'destroyed')
33
+ ? 'invisible'
34
+ : null;
35
+ if (visibilityChange) {
36
+ this.globalVisibilityListeners
37
+ .slice()
38
+ .forEach(listener => listener(sceneId, visibilityChange));
39
+ }
40
+ this.globalLifecycleListeners
41
+ .slice()
42
+ .forEach(listener => listener(sceneId, next, prev));
43
+ const sceneListeners = this.lifecycleListeners[sceneId];
44
+ if (sceneListeners) {
45
+ sceneListeners.slice().forEach(listener => listener(next, prev));
46
+ }
47
+ if (visibilityChange) {
48
+ const visListeners = this.visibilityListeners[sceneId];
49
+ if (visListeners) {
50
+ visListeners.slice().forEach(listener => listener(visibilityChange));
51
+ }
52
+ }
53
+ if (next === 'destroyed') {
54
+ this.markSceneDestroyed(sceneId);
55
+ onSceneDestroyed?.(sceneId);
56
+ this.cleanupScene(sceneId);
57
+ }
58
+ });
59
+ }
60
+ markSceneDestroyed(sceneId) {
61
+ this.destroyedScenes.delete(sceneId);
62
+ this.destroyedScenes.add(sceneId);
63
+ if (this.destroyedScenes.size > MAX_DESTROYED_SCENES) {
64
+ const oldest = this.destroyedScenes.values().next().value;
65
+ if (oldest !== undefined) {
66
+ this.destroyedScenes.delete(oldest);
67
+ }
68
+ }
69
+ }
70
+ getSceneState(sceneId) {
71
+ const activeState = this.sceneStates.get(sceneId);
72
+ if (activeState !== undefined) {
73
+ return activeState;
74
+ }
75
+ if (this.destroyedScenes.has(sceneId)) {
76
+ return 'destroyed';
77
+ }
78
+ return 'created';
79
+ }
80
+ cleanupScene(sceneId) {
81
+ this.sceneStates.delete(sceneId);
82
+ delete this.lifecycleListeners[sceneId];
83
+ delete this.visibilityListeners[sceneId];
84
+ }
85
+ addLifecycleListener(sceneId, listener) {
86
+ if (!this.lifecycleListeners[sceneId]) {
87
+ this.lifecycleListeners[sceneId] = [];
88
+ }
89
+ this.lifecycleListeners[sceneId].push(listener);
90
+ }
91
+ removeLifecycleListener(sceneId, listener) {
92
+ const list = this.lifecycleListeners[sceneId];
93
+ if (!list) {
94
+ return;
95
+ }
96
+ const next = list.filter(l => l !== listener);
97
+ if (next.length > 0) {
98
+ this.lifecycleListeners[sceneId] = next;
99
+ }
100
+ else {
101
+ delete this.lifecycleListeners[sceneId];
102
+ }
103
+ }
104
+ addGlobalLifecycleListener(listener) {
105
+ this.globalLifecycleListeners.push(listener);
106
+ }
107
+ removeGlobalLifecycleListener(listener) {
108
+ this.globalLifecycleListeners = this.globalLifecycleListeners.filter(l => l !== listener);
109
+ }
110
+ addGlobalVisibilityEventListener(listener) {
111
+ this.globalVisibilityListeners.push(listener);
112
+ }
113
+ removeGlobalVisibilityEventListener(listener) {
114
+ this.globalVisibilityListeners = this.globalVisibilityListeners.filter(l => l !== listener);
115
+ }
116
+ addVisibilityEventListener(sceneId, listener) {
117
+ if (!this.visibilityListeners[sceneId]) {
118
+ this.visibilityListeners[sceneId] = [];
119
+ }
120
+ this.visibilityListeners[sceneId].push(listener);
121
+ }
122
+ removeVisibilityEventListener(sceneId, listener) {
123
+ const list = this.visibilityListeners[sceneId];
124
+ if (!list) {
125
+ return;
126
+ }
127
+ const next = list.filter(l => l !== listener);
128
+ if (next.length > 0) {
129
+ this.visibilityListeners[sceneId] = next;
130
+ }
131
+ else {
132
+ delete this.visibilityListeners[sceneId];
133
+ }
134
+ }
135
+ }
136
+ export const lifecycleHandler = new LifecycleEventHandler();
@@ -1,15 +1,20 @@
1
- interface IndexType {
2
- [index: string]: any;
3
- }
4
- export type ResultType = IndexType | null;
5
- export type ResultEventListener<T extends ResultType> = (resultCode: number, data: T) => void;
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
- waitResult<T extends ResultType>(sceneId: string, requestCode: number): Promise<[number, T]>;
14
+ invalidateResultEventListener(sceneId: string, reason?: CancelReason): void;
15
+ invalidateAll(reason?: CancelReason): void;
16
+ prepareResult<T = unknown>(sceneId: string, requestCode: number): PreparedResult<T>;
17
+ setResult<T extends ResultMap = ResultMap>(sceneId: string, resultCode: number, data?: T | null): void;
18
+ waitResult<T = unknown>(sceneId: string, requestCode: number): Promise<NavigationResult<T>>;
14
19
  }
15
- export {};
20
+ export declare const resultHandler: ResultEventHandler;
@@ -1,13 +1,20 @@
1
1
  import NativeEvent from '../NativeEvent';
2
+ import NativeNavigation from '../NativeNavigation';
3
+ export const { RESULT_CANCEL, RESULT_OK, RESULT_BLOCK } = NativeNavigation.getConstants();
2
4
  export default class ResultEventHandler {
3
5
  listenerRecords = {};
4
- constructor() { }
5
6
  handleComponentResult() {
6
7
  NativeEvent.onResult(({ sceneId, requestCode, resultCode, resultData }) => {
7
8
  const listeners = this.listenerRecords[sceneId]?.[requestCode];
8
9
  if (listeners) {
9
10
  delete this.listenerRecords[sceneId][requestCode];
10
- listeners.forEach(l => l(resultCode, resultData));
11
+ if (Object.keys(this.listenerRecords[sceneId]).length === 0) {
12
+ delete this.listenerRecords[sceneId];
13
+ }
14
+ const result = resultCode === RESULT_CANCEL
15
+ ? { status: 'cancel', code: RESULT_CANCEL, reason: 'dismissed' }
16
+ : { status: 'ok', code: resultCode, data: resultData };
17
+ listeners.forEach(l => l(result));
11
18
  }
12
19
  });
13
20
  }
@@ -20,23 +27,91 @@ export default class ResultEventHandler {
20
27
  }
21
28
  this.listenerRecords[sceneId][requestCode].push(listener);
22
29
  }
23
- removeResultEventListener(sceneId, requestCode) {
24
- if (this.listenerRecords[sceneId]) {
25
- delete this.listenerRecords[sceneId][requestCode];
30
+ removeResultEventListener(sceneId, requestCode, listener) {
31
+ const sceneRecords = this.listenerRecords[sceneId];
32
+ const listeners = sceneRecords?.[requestCode];
33
+ if (!listeners) {
34
+ return;
35
+ }
36
+ const nextListeners = listeners.filter(l => l !== listener);
37
+ if (nextListeners.length > 0) {
38
+ sceneRecords[requestCode] = nextListeners;
39
+ }
40
+ else {
41
+ delete sceneRecords[requestCode];
42
+ if (Object.keys(sceneRecords).length === 0) {
43
+ delete this.listenerRecords[sceneId];
44
+ }
45
+ }
46
+ }
47
+ invalidateResultEventListener(sceneId, reason = 'unmounted') {
48
+ const sceneRecords = this.listenerRecords[sceneId];
49
+ if (!sceneRecords) {
50
+ return;
51
+ }
52
+ delete this.listenerRecords[sceneId];
53
+ const cancelled = {
54
+ status: 'cancel',
55
+ code: RESULT_CANCEL,
56
+ reason,
57
+ };
58
+ for (const listeners of Object.values(sceneRecords)) {
59
+ listeners.forEach(l => l(cancelled));
26
60
  }
27
61
  }
28
- invalidateResultEventListener(sceneId) {
29
- if (this.listenerRecords[sceneId]) {
30
- delete this.listenerRecords[sceneId];
62
+ invalidateAll(reason = 'root_reset') {
63
+ const allRecords = this.listenerRecords;
64
+ this.listenerRecords = {};
65
+ const cancelled = {
66
+ status: 'cancel',
67
+ code: RESULT_CANCEL,
68
+ reason,
69
+ };
70
+ for (const sceneRecords of Object.values(allRecords)) {
71
+ for (const listeners of Object.values(sceneRecords)) {
72
+ listeners.forEach(l => l(cancelled));
73
+ }
31
74
  }
32
75
  }
33
- waitResult(sceneId, requestCode) {
34
- return new Promise(resolve => {
35
- const listener = (resultCode, data) => {
36
- this.removeResultEventListener(sceneId, requestCode);
37
- resolve([resultCode, data]);
76
+ prepareResult(sceneId, requestCode) {
77
+ let settled = false;
78
+ let listenerRef;
79
+ const promise = new Promise(resolve => {
80
+ const listener = result => {
81
+ if (settled) {
82
+ return;
83
+ }
84
+ settled = true;
85
+ this.removeResultEventListener(sceneId, requestCode, listener);
86
+ resolve(result);
38
87
  };
88
+ listenerRef = listener;
39
89
  this.addResultEventListener(sceneId, requestCode, listener);
40
90
  });
91
+ const cancel = () => {
92
+ if (settled) {
93
+ return;
94
+ }
95
+ settled = true;
96
+ if (listenerRef) {
97
+ this.removeResultEventListener(sceneId, requestCode, listenerRef);
98
+ }
99
+ };
100
+ return { promise, cancel };
101
+ }
102
+ setResult(sceneId, resultCode, data = null) {
103
+ if (resultCode < RESULT_OK) {
104
+ console.warn('`resultCode` can only be `RESULT_OK`, `RESULT_CANCEL` or an integer greater than 0.');
105
+ }
106
+ if (data !== null && (typeof data !== 'object' || Array.isArray(data))) {
107
+ console.warn('`setResult` data must be a plain object or null.');
108
+ NativeNavigation.setResult(sceneId, resultCode, null);
109
+ return;
110
+ }
111
+ NativeNavigation.setResult(sceneId, resultCode, data);
112
+ }
113
+ waitResult(sceneId, requestCode) {
114
+ return this.prepareResult(sceneId, requestCode).promise;
41
115
  }
42
116
  }
117
+ export const resultHandler = new ResultEventHandler();
package/dist/hooks.d.ts CHANGED
@@ -1,3 +1,6 @@
1
1
  import { Navigator } from './Navigator';
2
- export declare function useNavigator(): Navigator;
3
- export declare function useVisibleEffect<T extends Function>(callback: T): void;
2
+ import type { RouteName, SceneLifecycleState } from './Route';
3
+ export type VisibleEffectCallback = () => void | (() => void);
4
+ export declare function useNavigator<Name extends RouteName = RouteName>(): Navigator<Name>;
5
+ export declare function useSceneState(): SceneLifecycleState;
6
+ export declare function useVisibleEffect(callback: VisibleEffectCallback): void;