hybrid-navigation 2.13.0-beta.2 → 2.13.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 (80) hide show
  1. package/android/src/main/java/com/reactnative/hybridnavigation/ReactFragment.java +3 -20
  2. package/ios/HybridNavigation/HBDUtils.h +3 -6
  3. package/ios/HybridNavigation/HBDUtils.m +10 -0
  4. package/ios/HybridNavigation/Navigator/HBDStackNavigator.m +7 -2
  5. package/ios/HybridNavigation/ReactBridge/HBDNavigationModule.m +1 -1
  6. package/ios/HybridNavigation/View/HBDRootView.m +9 -3
  7. package/ios/HybridNavigation/ViewController/HBDNavigationController.m +0 -30
  8. package/ios/HybridNavigation/ViewController/HBDViewController.m +7 -3
  9. package/ios/HybridNavigation/ViewController/UINavigationController+HBD.m +0 -3
  10. package/lib/Event.d.ts +18 -0
  11. package/lib/Event.js +71 -0
  12. package/lib/Garden.d.ts +46 -68
  13. package/lib/Garden.js +53 -70
  14. package/lib/GardenModule.d.ts +20 -0
  15. package/lib/GardenModule.js +3 -0
  16. package/lib/Navigation.d.ts +74 -0
  17. package/lib/Navigation.js +182 -0
  18. package/lib/NavigationComponentWrap.d.ts +19 -0
  19. package/lib/NavigationComponentWrap.js +46 -0
  20. package/lib/NavigationModule.d.ts +33 -22
  21. package/lib/NavigationModule.js +79 -22
  22. package/lib/Navigator.d.ts +55 -28
  23. package/lib/Navigator.js +122 -263
  24. package/lib/{typing.d.ts → Options.d.ts} +46 -50
  25. package/lib/{typing.js → Options.js} +0 -0
  26. package/lib/ReactRegistry.d.ts +16 -14
  27. package/lib/ReactRegistry.js +18 -93
  28. package/lib/Route.d.ts +74 -0
  29. package/lib/{router/typing.js → Route.js} +0 -0
  30. package/lib/handler/BarButtonEventHandler.d.ts +10 -0
  31. package/lib/handler/BarButtonEventHandler.js +38 -0
  32. package/lib/handler/DispatchCommandHandler.d.ts +24 -0
  33. package/lib/handler/DispatchCommandHandler.js +28 -0
  34. package/lib/handler/LayoutCommandHandler.d.ts +11 -0
  35. package/lib/handler/LayoutCommandHandler.js +39 -0
  36. package/lib/handler/ResultEventHandler.d.ts +15 -0
  37. package/lib/handler/ResultEventHandler.js +47 -0
  38. package/lib/handler/VisibilityEventHandler.d.ts +13 -0
  39. package/lib/handler/VisibilityEventHandler.js +32 -0
  40. package/lib/hooks.d.ts +0 -1
  41. package/lib/hooks.js +14 -29
  42. package/lib/index.d.ts +10 -2
  43. package/lib/index.js +17 -2
  44. package/lib/router/drawer.d.ts +1 -1
  45. package/lib/router/index.d.ts +4 -7
  46. package/lib/router/index.js +13 -6
  47. package/lib/router/screen.d.ts +1 -1
  48. package/lib/router/stack.d.ts +1 -1
  49. package/lib/router/tabs.d.ts +1 -1
  50. package/lib/store.d.ts +0 -9
  51. package/lib/store.js +0 -22
  52. package/package.json +1 -1
  53. package/src/Event.ts +123 -0
  54. package/src/Garden.ts +63 -149
  55. package/src/GardenModule.ts +24 -0
  56. package/src/Navigation.ts +278 -0
  57. package/src/NavigationComponentWrap.tsx +73 -0
  58. package/src/NavigationModule.ts +118 -29
  59. package/src/Navigator.ts +147 -319
  60. package/src/{typing.ts → Options.ts} +51 -57
  61. package/src/ReactRegistry.tsx +25 -127
  62. package/src/Route.ts +88 -0
  63. package/src/handler/BarButtonEventHandler.ts +50 -0
  64. package/src/handler/DispatchCommandHandler.ts +56 -0
  65. package/src/handler/LayoutCommandHandler.ts +49 -0
  66. package/src/handler/ResultEventHandler.ts +67 -0
  67. package/src/handler/VisibilityEventHandler.ts +44 -0
  68. package/src/hooks.ts +13 -38
  69. package/src/index.ts +38 -3
  70. package/src/router/drawer.ts +1 -1
  71. package/src/router/index.ts +36 -12
  72. package/src/router/screen.ts +1 -1
  73. package/src/router/stack.ts +1 -1
  74. package/src/router/tabs.ts +1 -1
  75. package/src/store.ts +0 -28
  76. package/lib/router/typing.d.ts +0 -37
  77. package/lib/utils.d.ts +0 -9
  78. package/lib/utils.js +0 -50
  79. package/src/router/typing.ts +0 -45
  80. package/src/utils.ts +0 -83
@@ -1,25 +1,5 @@
1
- import { Insets } from 'react-native'
2
- import { Navigator } from './Navigator'
3
-
4
- export interface IndexType {
5
- [index: string]: any
6
- }
7
-
8
- export interface PropsType {
9
- [index: string]: any
10
- }
11
-
12
- export type ResultType = IndexType | null
13
-
14
- interface Extras {
15
- sceneId: string
16
- from?: number | string
17
- to?: number | string
18
- }
19
-
20
- export interface NavigationInterceptor {
21
- (action: string, extras: Extras): boolean | Promise<boolean>
22
- }
1
+ import type { Insets } from 'react-native'
2
+ import type { Navigator } from './Navigator'
23
3
 
24
4
  export type Color = string
25
5
  export type ImageSource = { uri: string; scale?: number; height?: number; width?: number }
@@ -104,45 +84,59 @@ export interface TabItem {
104
84
  hideTabBarWhenPush?: boolean
105
85
  }
106
86
 
107
- export interface Screen extends Layout {
108
- screen: {
109
- moduleName: string
110
- props?: IndexType
111
- options?: NavigationItem
112
- }
113
- }
114
-
115
- export interface Stack extends Layout {
116
- stack: {
117
- children: Array<BuildInLayout | Layout>
118
- options?: {}
119
- }
87
+ export interface ShadowImage {
88
+ image?: ImageSource
89
+ color?: Color
120
90
  }
121
91
 
122
- export interface Tabs extends Layout {
123
- tabs: {
124
- children: Array<BuildInLayout | Layout>
125
- options?: {
126
- selectedIndex?: number
127
- tabBarModuleName?: string
128
- sizeIndeterminate?: boolean
129
- }
130
- }
131
- }
92
+ export interface DefaultOptions {
93
+ screenBackgroundColor?: Color // 页面背景,默认是白色
94
+ topBarStyle?: BarStyle // TopBar 样式,决定了状态栏的颜色,可选项有 `BarStyleLightContent` 和 `BarStyleDarkContent`
95
+ topBarColor?: Color // TopBar 背景颜色,默认根据 topBarStyle 来计算
96
+ topBarColorDarkContent?: Color // TopBar 背景颜色,当 topBarStyle 的值为 BarStyleDarkContent 时生效,覆盖 topBarColor 的值
97
+ topBarColorLightContent?: Color // TopBar 背景颜色,当 topBarStyle 的值为 BarStyleLightContent 时生效,覆盖 topBarColor 的值
98
+ statusBarColorAndroid?: Color // 状态栏背景颜色,默认取 topBarColor 的值
99
+ navigationBarColorAndroid?: Color // 底部虚拟键背景颜色,仅对 Android 8.0 以上版本生效
100
+ hideBackTitleIOS?: boolean // 是否隐藏返回按钮旁边的文字,默认是 false, 仅对 iOS 生效
101
+ elevationAndroid?: number // TopBar 阴影高度,默认值为 4 dp
102
+ shadowImage?: ShadowImage // TopBar 阴影图片,仅对 iOS 生效
103
+ backIcon?: ImageSource // 返回按钮图片
104
+ topBarTintColor?: Color // TopBar 按钮的颜色。默认根据 topBarStyle 来计算
105
+ topBarTintColorDarkContent?: Color // TopBar 按钮颜色,当 topBarStyle 的值为 BarStyleDarkContent 时生效,覆盖 topBarTintColor 的值
106
+ topBarTintColorLightContent?: Color // TopBar 按钮颜色,当 topBarStyle 的值为 BarStyleLightContent 时生效,覆盖 topBarTintColor 的值
107
+ titleTextColor?: Color // TopBar 标题颜色,默认根据 topBarStyle 来计算
108
+ titleTextColorDarkContent?: Color // TopBar 标题颜色,当 topBarStyle 的值为 BarStyleDarkContent 时生效,覆盖 titleTextColor 的值
109
+ titleTextColorLightContent?: Color // TopBar 标题颜色,当 topBarStyle 的值为 BarStyleLightContent 时生效,覆盖 titleTextColor 的值
110
+ titleTextSize?: number // TopBar 标题字体大小,默认是 17 dp(pt)
111
+ titleAlignmentAndroid?: TitleAlignment // TopBar 标题的位置,可选项有 `TitleAlignmentLeft` 和 `TitleAlignmentCenter` ,仅对 Android 生效
112
+ barButtonItemTextSize?: number // TopBar 按钮字体大小,默认是 15 dp(pt)
113
+ swipeBackEnabledAndroid?: boolean // Android 是否开启右滑返回,默认是 false
114
+ splitTopBarTransitionIOS?: boolean // iOS 侧滑返回时,是否总是割裂导航栏背景
115
+ scrimAlphaAndroid?: number // Android 侧滑返回遮罩效果 [0 - 255]
116
+ displayCutoutWhenLandscapeAndroid?: boolean // 横屏时,是否将界面延伸至刘海区域,默认 true
132
117
 
133
- export interface Drawer extends Layout {
134
- drawer: {
135
- children: [BuildInLayout | Layout, BuildInLayout | Layout]
136
- options?: {
137
- maxDrawerWidth?: number
138
- minDrawerMargin?: number
139
- menuInteractive?: boolean
140
- }
141
- }
118
+ tabBarColor?: Color // 底部 TabBar 背景颜色,请勿使用带透明度的颜色。
119
+ tabBarShadowImage?: ShadowImage // 底部 TabBar 阴影图片。对于 iOS, 只有同时设置了 tabBarColor 才会生效
120
+ tabBarItemColor?: Color // 底部 TabBarItem icon 选中颜色
121
+ tabBarUnselectedItemColor?: Color // 底部 TabBarItem icon 未选中颜色,默认为 #BDBDBD
122
+ tabBarBadgeColor?: Color // Tab badge 颜色
142
123
  }
143
124
 
144
- export type BuildInLayout = Screen | Stack | Tabs | Drawer
125
+ export type TabBarStyle = Pick<
126
+ DefaultOptions,
127
+ 'tabBarColor' | 'tabBarShadowImage' | 'tabBarItemColor' | 'tabBarUnselectedItemColor'
128
+ >
145
129
 
146
- export interface Layout {
147
- [index: string]: {}
130
+ export interface TabItemInfo {
131
+ index: number
132
+ title?: string
133
+ badge?: {
134
+ text?: string
135
+ hidden: boolean
136
+ dot?: boolean
137
+ }
138
+ icon?: {
139
+ selected: ImageSource
140
+ unselected?: ImageSource
141
+ }
148
142
  }
@@ -1,138 +1,36 @@
1
- import { AppRegistry, ComponentProvider } from 'react-native'
2
- import React, { useEffect, createContext } from 'react'
3
- import { Navigator } from './Navigator'
4
- import {
5
- NavigationModule,
6
- EventEmitter,
7
- EVENT_NAVIGATION,
8
- KEY_SCENE_ID,
9
- KEY_ON,
10
- ON_COMPONENT_APPEAR,
11
- ON_COMPONENT_DISAPPEAR,
12
- } from './NavigationModule'
13
- import { Garden } from './Garden'
14
- import { RouteConfig, router } from './router'
15
- import store from './store'
16
- import { bindBarButtonItemClickEvent, removeBarButtonItemClickEvent } from './utils'
17
- import { NavigationItem } from './typing'
1
+ import { ComponentProvider } from 'react-native'
2
+ import type { RouteConfig } from './Route'
18
3
 
19
- export interface InjectedProps {
20
- navigator: Navigator
21
- garden: Garden
22
- sceneId: string
23
- }
24
-
25
- interface Props {
26
- sceneId: string
27
- }
28
-
29
- function getDisplayName(WrappedComponent: React.ComponentType<any>) {
30
- return WrappedComponent.displayName || WrappedComponent.name || 'Component'
31
- }
32
-
33
- export const NavigationContext = createContext<any>(null)
34
-
35
- function withNavigator(moduleName: string) {
36
- return function (WrappedComponent: React.ComponentType<any>) {
37
- const FC = React.forwardRef((props: Props, ref: React.Ref<React.ComponentType<any>>) => {
38
- const { sceneId } = props
39
-
40
- const navigator = Navigator.of(sceneId)
41
- if (navigator.moduleName === undefined) {
42
- navigator.moduleName = moduleName
43
- }
44
-
45
- const garden = navigator.garden
46
-
47
- useEffect(() => {
48
- navigator.signalFirstRenderComplete()
49
- return () => {
50
- removeBarButtonItemClickEvent(navigator.sceneId)
51
- store.removeNavigator(navigator.sceneId)
52
- navigator.unmount()
53
- }
54
- }, [navigator])
55
-
56
- useEffect(() => {
57
- const subscription = EventEmitter.addListener(EVENT_NAVIGATION, data => {
58
- if (navigator.sceneId === data[KEY_SCENE_ID]) {
59
- if (data[KEY_ON] === ON_COMPONENT_APPEAR) {
60
- navigator.visibility = 'visible'
61
- } else if (data[KEY_ON] === ON_COMPONENT_DISAPPEAR) {
62
- navigator.visibility = 'invisible'
63
- }
64
- }
65
- })
66
- return () => {
67
- subscription.remove()
68
- }
69
- }, [navigator])
70
-
71
- const injected = {
72
- garden,
73
- navigator,
74
- }
75
-
76
- return (
77
- <NavigationContext.Provider value={navigator}>
78
- <WrappedComponent ref={ref} {...props} {...injected} />
79
- </NavigationContext.Provider>
80
- )
81
- })
82
-
83
- FC.displayName = `withNavigator(${getDisplayName(WrappedComponent)})`
84
- return FC
85
- }
86
- }
87
-
88
- export type HOC = (WrappedComponent: React.ComponentType<any>) => React.ComponentType<any>
89
- let wrap: HOC | undefined
4
+ import Navigation, { HOC } from './Navigation'
90
5
 
91
6
  export class ReactRegistry {
92
- static registerEnded: boolean
7
+ /**
8
+ * @deprecated Use Navigation.startRegisterComponent() instead.
9
+ * @param hoc
10
+ */
93
11
  static startRegisterComponent(hoc?: HOC) {
94
- store.clear()
95
- wrap = hoc
96
- ReactRegistry.registerEnded = false
97
- NavigationModule.startRegisterReactComponent()
12
+ Navigation.startRegisterComponent(hoc)
98
13
  }
99
14
 
15
+ /**
16
+ * @deprecated Use Navigation.endRegisterComponent() instead.
17
+ */
100
18
  static endRegisterComponent() {
101
- if (ReactRegistry.registerEnded) {
102
- console.warn(`Please don't call ReactRegistry#endRegisterComponent multiple times.`)
103
- return
104
- }
105
- ReactRegistry.registerEnded = true
106
- NavigationModule.endRegisterReactComponent()
19
+ Navigation.endRegisterComponent()
107
20
  }
108
21
 
109
- static registerComponent(appKey: string, getComponentFunc: ComponentProvider, routeConfig?: RouteConfig) {
110
- if (routeConfig) {
111
- router.registerRoute(appKey, routeConfig)
112
- }
113
-
114
- let WrappedComponent = getComponentFunc()
115
- if (wrap) {
116
- WrappedComponent = wrap(WrappedComponent)
117
- }
118
-
119
- // build static options
120
- let options: object = bindBarButtonItemClickEvent((WrappedComponent as any).navigationItem) || {}
121
- NavigationModule.registerReactComponent(appKey, options)
122
-
123
- let RootComponent = withNavigator(appKey)(WrappedComponent)
124
- AppRegistry.registerComponent(appKey, () => RootComponent)
125
- }
126
- }
127
-
128
- export function withNavigationItem(item: NavigationItem) {
129
- return function (WrappedComponent: React.ComponentType<any>): React.ComponentType<any> {
130
- let navigationItem = (WrappedComponent as any).navigationItem
131
- if (navigationItem) {
132
- ;(WrappedComponent as any).navigationItem = { ...navigationItem, ...item }
133
- } else {
134
- ;(WrappedComponent as any).navigationItem = item
135
- }
136
- return WrappedComponent
22
+ /**
23
+ *
24
+ * @deprecated Use Navigation.registerComponent() instead.
25
+ * @param appKey
26
+ * @param getComponentFunc
27
+ * @param routeConfig
28
+ */
29
+ static registerComponent(
30
+ appKey: string,
31
+ getComponentFunc: ComponentProvider,
32
+ routeConfig?: RouteConfig,
33
+ ) {
34
+ Navigation.registerComponent(appKey, getComponentFunc, routeConfig)
137
35
  }
138
36
  }
package/src/Route.ts ADDED
@@ -0,0 +1,88 @@
1
+ export type Visibility = 'visible' | 'invisible' | 'pending'
2
+
3
+ export interface Screen extends Layout {
4
+ screen: {
5
+ moduleName: string
6
+ props?: object
7
+ options?: {}
8
+ }
9
+ }
10
+
11
+ export interface Stack extends Layout {
12
+ stack: {
13
+ children: Array<BuildInLayout | Layout>
14
+ options?: {}
15
+ }
16
+ }
17
+
18
+ export interface Tabs extends Layout {
19
+ tabs: {
20
+ children: Array<BuildInLayout | Layout>
21
+ options?: {
22
+ selectedIndex?: number
23
+ tabBarModuleName?: string
24
+ sizeIndeterminate?: boolean
25
+ }
26
+ }
27
+ }
28
+
29
+ export interface Drawer extends Layout {
30
+ drawer: {
31
+ children: [BuildInLayout | Layout, BuildInLayout | Layout]
32
+ options?: {
33
+ maxDrawerWidth?: number
34
+ minDrawerMargin?: number
35
+ menuInteractive?: boolean
36
+ }
37
+ }
38
+ }
39
+
40
+ export type BuildInLayout = Screen | Stack | Tabs | Drawer
41
+
42
+ export interface Layout {
43
+ [index: string]: {}
44
+ }
45
+
46
+ export type RouteMode = 'modal' | 'present' | 'push'
47
+ export type LayoutMode = 'modal' | 'present' | 'normal'
48
+
49
+ export interface Route {
50
+ sceneId: string
51
+ moduleName: string
52
+ mode: LayoutMode
53
+ }
54
+
55
+ export interface RouteGraph {
56
+ layout: string
57
+ sceneId: string
58
+ mode: LayoutMode
59
+ children?: RouteGraph[]
60
+ }
61
+
62
+ export interface RouteConfig {
63
+ path?: string
64
+ dependency?: string
65
+ mode?: RouteMode
66
+ }
67
+
68
+ export interface RouteInfo {
69
+ moduleName: string
70
+ dependencies: string[]
71
+ mode: RouteMode
72
+ props: object
73
+ options: object
74
+ }
75
+
76
+ export interface RouteData {
77
+ moduleName: string
78
+ regexp?: RegExp
79
+ path?: string
80
+ dependency?: string
81
+ mode: RouteMode
82
+ }
83
+
84
+ export interface RouteHandler {
85
+ (graph: RouteGraph, route: RouteInfo, next: RouteHandler): Promise<boolean>
86
+ }
87
+
88
+ export type RouteInterceptor = (path: string) => boolean | Promise<boolean>
@@ -0,0 +1,50 @@
1
+ import Event from '../Event'
2
+
3
+ type BarButtonClickEventListener = (sceneId: string, value: Function) => void
4
+
5
+ export default class BarButtonEventHandler {
6
+ private actions: Record<string, Function> = {}
7
+ private keyPairs: Record<string, string[]> = {}
8
+ private actionIdGenerator = 0
9
+
10
+ bindBarButtonClickEvent(sceneId: string, item: object | null | undefined): object | null {
11
+ if (item === null || item === undefined) {
12
+ return null
13
+ }
14
+
15
+ return JSON.parse(
16
+ JSON.stringify(item, (key, value) => {
17
+ if (key !== 'action' || typeof value !== 'function') {
18
+ return value
19
+ }
20
+
21
+ const actionKey = 'ON_BAR_BUTTON_ITEM_CLICK_' + this.actionIdGenerator++
22
+
23
+ if (!this.keyPairs[sceneId]) {
24
+ this.keyPairs[sceneId] = []
25
+ }
26
+ this.keyPairs[sceneId].push(actionKey)
27
+ this.actions[actionKey] = value
28
+
29
+ return actionKey
30
+ }),
31
+ )
32
+ }
33
+
34
+ unbindBarButtonClickEvent(sceneId: string): void {
35
+ const keys = this.keyPairs[sceneId]
36
+ delete this.keyPairs[sceneId]
37
+ if (keys) {
38
+ keys.forEach(key => delete this.actions[key])
39
+ }
40
+ }
41
+
42
+ setBarButtonClickEventListener(handler: BarButtonClickEventListener) {
43
+ Event.listenBarButtonItemClick((sceneId: string, action: string) => {
44
+ const listeners = this.actions
45
+ if (listeners[action]) {
46
+ handler(sceneId, listeners[action])
47
+ }
48
+ })
49
+ }
50
+ }
@@ -0,0 +1,56 @@
1
+ import NavigationModule from '../NavigationModule'
2
+ import { NavigationItem } from '../Options'
3
+ import { BuildInLayout, Layout } from '../Route'
4
+
5
+ export interface DispatchParams {
6
+ animated?: boolean
7
+ moduleName?: string
8
+ layout?: BuildInLayout | Layout
9
+ popToRoot?: boolean
10
+ requestCode?: number
11
+ props?: object
12
+ options?: NavigationItem
13
+ [index: string]: any
14
+ }
15
+
16
+ interface InterceptParams {
17
+ sceneId: string
18
+ from?: number | string
19
+ to?: number | string
20
+ }
21
+
22
+ export type NavigationInterceptor = (
23
+ action: string,
24
+ params: InterceptParams,
25
+ ) => boolean | Promise<boolean>
26
+
27
+ export default class DispatchCommandHandler {
28
+ private interceptor: NavigationInterceptor = () => false
29
+
30
+ async dispatch(sceneId: string, action: string, params: DispatchParams): Promise<boolean> {
31
+ let intercepted = false
32
+ const { from, to } = params
33
+ if (this.interceptor) {
34
+ const result = this.interceptor(action, {
35
+ sceneId,
36
+ from,
37
+ to,
38
+ })
39
+ if (result instanceof Promise) {
40
+ intercepted = await result
41
+ } else {
42
+ intercepted = result
43
+ }
44
+ }
45
+
46
+ if (!intercepted) {
47
+ return NavigationModule.dispatch(sceneId, action, params)
48
+ }
49
+
50
+ return false
51
+ }
52
+
53
+ setInterceptor(interceptor: NavigationInterceptor) {
54
+ this.interceptor = interceptor
55
+ }
56
+ }
@@ -0,0 +1,49 @@
1
+ import Event from '../Event'
2
+ import NavigationModule from '../NavigationModule'
3
+ import { BuildInLayout, Layout } from '../Route'
4
+
5
+ export default class LayoutCommandHandler {
6
+ private willSetRoot = () => {}
7
+ private didSetRoot = () => {}
8
+
9
+ private canInvokeWillSetRoot = true
10
+ private tagGenerator = 0
11
+
12
+ constructor() {}
13
+
14
+ handleRootLayoutChange() {
15
+ Event.listenWillSetRoot(() => {
16
+ if (this.canInvokeWillSetRoot) {
17
+ this.willSetRoot()
18
+ }
19
+ })
20
+
21
+ Event.listenDidSetRoot(() => {
22
+ this.didSetRoot()
23
+ this.canInvokeWillSetRoot = true
24
+ })
25
+ }
26
+
27
+ setRoot(layout: BuildInLayout | Layout, sticky = false) {
28
+ this.willSetRoot()
29
+ this.canInvokeWillSetRoot = false
30
+
31
+ const flag = this.tagGenerator++
32
+ NavigationModule.setRoot(layout, sticky, flag)
33
+
34
+ return new Promise<void>(resolve => {
35
+ const subscription = Event.listenDidSetRoot((tag: number) => {
36
+ // FIXME: 此处恐有内存泄漏
37
+ if (tag === flag) {
38
+ subscription.remove()
39
+ resolve()
40
+ }
41
+ })
42
+ })
43
+ }
44
+
45
+ setRootLayoutUpdateListener(willSetRoot = () => {}, didSetRoot = () => {}) {
46
+ this.willSetRoot = willSetRoot
47
+ this.didSetRoot = didSetRoot
48
+ }
49
+ }
@@ -0,0 +1,67 @@
1
+ import Event from '../Event'
2
+ import NavigationModule from '../NavigationModule'
3
+ interface IndexType {
4
+ [index: string]: any
5
+ }
6
+
7
+ export type ResultType = IndexType | null
8
+ export type ResultEventListener<T extends ResultType> = (resultCode: number, data: T) => void
9
+
10
+ const { RESULT_CANCEL } = NavigationModule.getConstants()
11
+ export default class ResultEventHandler {
12
+ private listeners: Record<string, Record<number, ResultEventListener<any>>> = {}
13
+
14
+ constructor() {}
15
+
16
+ handleComponentResult() {
17
+ Event.listenComponentResult(
18
+ (sceneId: string, requestCode: number, resultCode: number, data: any) => {
19
+ const listener = this.listeners[sceneId]?.[requestCode]
20
+ if (listener) {
21
+ delete this.listeners[sceneId][requestCode]
22
+ listener(resultCode, data)
23
+ }
24
+ },
25
+ )
26
+ }
27
+
28
+ private addResultEventListener(
29
+ sceneId: string,
30
+ requestCode: number,
31
+ listener: ResultEventListener<any>,
32
+ ) {
33
+ if (!this.listeners[sceneId]) {
34
+ this.listeners[sceneId] = {}
35
+ }
36
+ const previousListener = this.listeners[sceneId][requestCode]
37
+ if (previousListener) {
38
+ previousListener(RESULT_CANCEL, null)
39
+ }
40
+ this.listeners[sceneId][requestCode] = listener
41
+ }
42
+
43
+ private removeResultEventListener(sceneId: string, requestCode: number) {
44
+ if (this.listeners[sceneId]) {
45
+ delete this.listeners[sceneId][requestCode]
46
+ }
47
+ }
48
+
49
+ invalidateResultEventListener(sceneId: string) {
50
+ const listeners = this.listeners[sceneId]
51
+ if (listeners) {
52
+ Object.values(listeners).forEach(listener => listener(RESULT_CANCEL, null))
53
+ }
54
+
55
+ delete this.listeners[sceneId]
56
+ }
57
+
58
+ waitResult<T extends ResultType>(sceneId: string, requestCode: number): Promise<[number, T]> {
59
+ return new Promise<[number, T]>(resolve => {
60
+ const listener = (resultCode: number, data: T) => {
61
+ this.removeResultEventListener(sceneId, requestCode)
62
+ resolve([resultCode, data])
63
+ }
64
+ this.addResultEventListener(sceneId, requestCode, listener)
65
+ })
66
+ }
67
+ }
@@ -0,0 +1,44 @@
1
+ import { Visibility } from '../Route'
2
+ import Event from '../Event'
3
+
4
+ export type VisibilityEventListener = (visibility: Visibility) => void
5
+ export type GlobalVisibilityEventListener = (sceneId: string, visibility: Visibility) => void
6
+
7
+ export default class VisibilityEventHandler {
8
+ private listeners: Record<string, VisibilityEventListener[]> = {}
9
+ private globalListeners: GlobalVisibilityEventListener[] = []
10
+
11
+ constructor() {}
12
+
13
+ handleComponentVisibility() {
14
+ Event.listenComponentVisibility((sceneId: string, visibility: Visibility) => {
15
+ const listeners = this.listeners[sceneId]
16
+ if (listeners) {
17
+ listeners.forEach(listener => listener(visibility))
18
+ }
19
+
20
+ this.globalListeners.forEach(listener => listener(sceneId, visibility))
21
+ })
22
+ }
23
+
24
+ addGlobalVisibilityEventListener(listener: GlobalVisibilityEventListener) {
25
+ this.globalListeners.push(listener)
26
+ }
27
+
28
+ removeGlobalVisibilityEventListener(listener: GlobalVisibilityEventListener) {
29
+ this.globalListeners = this.globalListeners.filter(l => l !== listener)
30
+ }
31
+
32
+ addVisibilityEventListener(sceneId: string, listener: VisibilityEventListener) {
33
+ if (!this.listeners[sceneId]) {
34
+ this.listeners[sceneId] = []
35
+ }
36
+ this.listeners[sceneId].push(listener)
37
+ }
38
+
39
+ removeVisibilityEventListener(sceneId: string, listener: VisibilityEventListener) {
40
+ if (this.listeners[sceneId]) {
41
+ this.listeners[sceneId] = this.listeners[sceneId].filter(l => l !== listener)
42
+ }
43
+ }
44
+ }