hybrid-navigation 2.13.0-beta.3 → 2.13.1

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 (58) hide show
  1. package/android/src/main/java/com/reactnative/hybridnavigation/ReactFragment.java +3 -20
  2. package/lib/Event.d.ts +12 -16
  3. package/lib/Event.js +20 -66
  4. package/lib/Garden.d.ts +49 -6
  5. package/lib/Garden.js +56 -32
  6. package/lib/Navigation.d.ts +68 -0
  7. package/lib/Navigation.js +162 -0
  8. package/lib/NavigationComponentWrap.d.ts +19 -0
  9. package/lib/NavigationComponentWrap.js +46 -0
  10. package/lib/Navigator.d.ts +46 -32
  11. package/lib/Navigator.js +110 -169
  12. package/lib/ReactRegistry.d.ts +19 -13
  13. package/lib/ReactRegistry.js +21 -73
  14. package/lib/Route.d.ts +13 -8
  15. package/lib/handler/BarButtonEventHandler.d.ts +10 -0
  16. package/lib/handler/BarButtonEventHandler.js +38 -0
  17. package/lib/handler/DispatchCommandHandler.d.ts +26 -0
  18. package/lib/handler/DispatchCommandHandler.js +28 -0
  19. package/lib/handler/LayoutCommandHandler.d.ts +11 -0
  20. package/lib/handler/LayoutCommandHandler.js +39 -0
  21. package/lib/handler/ResultEventHandler.d.ts +15 -0
  22. package/lib/handler/ResultEventHandler.js +47 -0
  23. package/lib/handler/VisibilityEventHandler.d.ts +13 -0
  24. package/lib/handler/VisibilityEventHandler.js +32 -0
  25. package/lib/hooks.d.ts +4 -0
  26. package/lib/hooks.js +13 -18
  27. package/lib/index.d.ts +8 -1
  28. package/lib/index.js +26 -2
  29. package/lib/router/drawer.d.ts +3 -1
  30. package/lib/router/drawer.js +17 -17
  31. package/lib/router/index.d.ts +19 -15
  32. package/lib/router/index.js +140 -116
  33. package/lib/router/screen.d.ts +5 -1
  34. package/lib/router/screen.js +29 -0
  35. package/lib/router/stack.d.ts +5 -1
  36. package/lib/router/stack.js +59 -46
  37. package/lib/router/tabs.d.ts +3 -1
  38. package/lib/router/tabs.js +18 -28
  39. package/package.json +1 -1
  40. package/src/Event.ts +25 -90
  41. package/src/Garden.ts +60 -38
  42. package/src/Navigation.ts +253 -0
  43. package/src/NavigationComponentWrap.tsx +73 -0
  44. package/src/Navigator.ts +124 -233
  45. package/src/ReactRegistry.tsx +23 -99
  46. package/src/Route.ts +14 -9
  47. package/src/handler/BarButtonEventHandler.ts +50 -0
  48. package/src/handler/DispatchCommandHandler.ts +58 -0
  49. package/src/handler/LayoutCommandHandler.ts +49 -0
  50. package/src/handler/ResultEventHandler.ts +67 -0
  51. package/src/handler/VisibilityEventHandler.ts +44 -0
  52. package/src/hooks.ts +18 -30
  53. package/src/index.ts +38 -4
  54. package/src/router/drawer.ts +20 -17
  55. package/src/router/index.ts +150 -119
  56. package/src/router/screen.ts +34 -1
  57. package/src/router/stack.ts +61 -48
  58. package/src/router/tabs.ts +19 -29
@@ -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,58 @@
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
+ from?: string | number
9
+ to?: string | number
10
+ layout?: BuildInLayout | Layout
11
+ popToRoot?: boolean
12
+ requestCode?: number
13
+ props?: object
14
+ options?: NavigationItem
15
+ [index: string]: any
16
+ }
17
+
18
+ interface InterceptParams {
19
+ sceneId: string
20
+ from?: number | string
21
+ to?: number | string
22
+ }
23
+
24
+ export type NavigationInterceptor = (
25
+ action: string,
26
+ params: InterceptParams,
27
+ ) => boolean | Promise<boolean>
28
+
29
+ export default class DispatchCommandHandler {
30
+ private interceptor: NavigationInterceptor = () => false
31
+
32
+ async dispatch(sceneId: string, action: string, params: DispatchParams): Promise<boolean> {
33
+ let intercepted = false
34
+ const { from, to } = params
35
+ if (this.interceptor) {
36
+ const result = this.interceptor(action, {
37
+ sceneId,
38
+ from,
39
+ to,
40
+ })
41
+ if (result instanceof Promise) {
42
+ intercepted = await result
43
+ } else {
44
+ intercepted = result
45
+ }
46
+ }
47
+
48
+ if (!intercepted) {
49
+ return NavigationModule.dispatch(sceneId, action, params)
50
+ }
51
+
52
+ return false
53
+ }
54
+
55
+ setInterceptor(interceptor: NavigationInterceptor) {
56
+ this.interceptor = interceptor
57
+ }
58
+ }
@@ -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
+ }
package/src/hooks.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import React, { useContext, useEffect, useRef, useState } from 'react'
2
+ import Navigation from './Navigation'
2
3
 
3
- import Event from './Event'
4
4
  import { Navigator, NavigationContext } from './Navigator'
5
5
 
6
6
  export function useVisible() {
@@ -13,18 +13,9 @@ export function useVisibility() {
13
13
  const [visibility, setVisibility] = useState(navigator.visibility)
14
14
 
15
15
  useEffect(() => {
16
- const subscription = Event.listenComponentVisibility(
17
- (sceneId: string) => {
18
- if (sceneId === navigator.sceneId) {
19
- setVisibility('visible')
20
- }
21
- },
22
- (sceneId: string) => {
23
- if (sceneId === navigator.sceneId) {
24
- setVisibility('invisible')
25
- }
26
- },
27
- )
16
+ const subscription = Navigation.addVisibilityEventListener(navigator.sceneId, v => {
17
+ setVisibility(v)
18
+ })
28
19
  return () => subscription.remove()
29
20
  }, [navigator])
30
21
 
@@ -40,25 +31,18 @@ export function useVisibleEffect(effect: React.EffectCallback) {
40
31
  destructor.current = effect()
41
32
  }
42
33
 
43
- const subscription = Event.listenComponentVisibility(
44
- (sceneId: string) => {
45
- if (sceneId === navigator.sceneId) {
46
- destructor.current = effect()
47
- }
48
- },
49
- (sceneId: string) => {
50
- if (sceneId === navigator.sceneId && destructor.current) {
51
- destructor.current()
52
- destructor.current = undefined
53
- }
54
- },
55
- )
56
-
57
- return () => {
58
- if (destructor.current) {
59
- destructor.current()
34
+ const subscription = Navigation.addVisibilityEventListener(navigator.sceneId, visibility => {
35
+ if (visibility === 'visible') {
36
+ destructor.current = effect()
37
+ } else {
38
+ destructor.current && destructor.current()
60
39
  destructor.current = undefined
61
40
  }
41
+ })
42
+
43
+ return () => {
44
+ destructor.current && destructor.current()
45
+ destructor.current = undefined
62
46
  subscription.remove()
63
47
  }
64
48
  }, [effect, navigator])
@@ -68,6 +52,10 @@ export function useNavigator(): Navigator {
68
52
  return useContext<Navigator>(NavigationContext)
69
53
  }
70
54
 
55
+ /**
56
+ * @deprecated Use Navigation related methods instead.
57
+ * @returns
58
+ */
71
59
  export function useGarden() {
72
60
  const ctx = useContext<Navigator>(NavigationContext)
73
61
  return ctx.garden
package/src/index.ts CHANGED
@@ -1,3 +1,41 @@
1
+ import { Platform } from 'react-native'
2
+ import Navigation from './Navigation'
3
+ import Event from './Event'
4
+ import GardenModule from './GardenModule'
5
+ import { withNavigation } from './NavigationComponentWrap'
6
+
7
+ Navigation.setNavigationComponentWrap(withNavigation)
8
+
9
+ const { STATUSBAR_HEIGHT, TOOLBAR_HEIGHT } = GardenModule.getConstants()
10
+ let _statusBarHeight = STATUSBAR_HEIGHT
11
+ if (Platform.OS === 'ios') {
12
+ Event.listenStatusBarHeightChange(height => {
13
+ _statusBarHeight = height
14
+ })
15
+ }
16
+
17
+ function statusBarHeight() {
18
+ return _statusBarHeight
19
+ }
20
+
21
+ function toolbarHeight() {
22
+ return TOOLBAR_HEIGHT
23
+ }
24
+
25
+ function topBarHeight() {
26
+ return statusBarHeight() + toolbarHeight()
27
+ }
28
+
29
+ export default Navigation
30
+ export { statusBarHeight, toolbarHeight, topBarHeight }
31
+ export { RESULT_OK, RESULT_CANCEL } from './Navigation'
32
+ export {
33
+ withNavigation,
34
+ withNavigationItem,
35
+ NavigationProps,
36
+ InjectedProps,
37
+ } from './NavigationComponentWrap'
38
+
1
39
  export * from './Navigator'
2
40
  export * from './Garden'
3
41
  export * from './ReactRegistry'
@@ -6,7 +44,3 @@ export * from './router'
6
44
  export * from './DeepLink'
7
45
  export * from './Route'
8
46
  export * from './Options'
9
-
10
- import NavigationModule from './NavigationModule'
11
-
12
- export const { RESULT_OK, RESULT_CANCEL } = NavigationModule.getConstants()
@@ -1,6 +1,6 @@
1
- import { ScreenGraph } from './screen'
2
- import { Navigator } from '../Navigator'
1
+ import { ScreenGraph, isTargetLocateIn } from './screen'
3
2
  import { LayoutMode, RouteGraph, RouteHandler, RouteInfo } from '../Route'
3
+ import Navigation from '../Navigation'
4
4
 
5
5
  export interface DrawerGraph extends RouteGraph {
6
6
  layout: 'drawer'
@@ -13,22 +13,25 @@ export function isDrawerGraph(graph: RouteGraph): graph is DrawerGraph {
13
13
  return graph.layout === 'drawer'
14
14
  }
15
15
 
16
- export async function drawerRouteHandler(graph: RouteGraph, route: RouteInfo, next: RouteHandler) {
17
- if (!isDrawerGraph(graph)) {
18
- return false
19
- }
16
+ export class DrawerRouteHandler implements RouteHandler {
17
+ async process(graph: RouteGraph, target: RouteInfo): Promise<[boolean, RouteGraph | null]> {
18
+ if (!isDrawerGraph(graph)) {
19
+ throw new Error(`${graph} is Not a DrawerGraph`)
20
+ }
21
+
22
+ const { children } = graph
20
23
 
21
- const { moduleName, sceneId } = graph.children[1]
22
- if (moduleName === route.moduleName) {
23
- const navigator = Navigator.of(sceneId)
24
- navigator.openMenu()
25
- return true
26
- } else {
27
- let result = await next(graph.children[0], route, next)
28
- if (result) {
29
- const navigator = Navigator.of(sceneId)
30
- navigator.closeMenu()
24
+ const { moduleName, sceneId } = children[1]
25
+ if (moduleName === target.moduleName) {
26
+ await Navigation.dispatch(sceneId, 'openMenu')
27
+ return [true, null]
31
28
  }
32
- return result
29
+
30
+ if (isTargetLocateIn(children[0], target)) {
31
+ Navigation.dispatch(sceneId, 'closeMenu', { from: moduleName })
32
+ return [true, children[0]]
33
+ }
34
+
35
+ return [false, graph]
33
36
  }
34
37
  }