hybrid-navigation 2.13.0 → 2.13.3

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 (77) hide show
  1. package/README.md +4 -4
  2. package/android/build.gradle +1 -1
  3. package/android/src/main/java/com/reactnative/hybridnavigation/Garden.java +1 -1
  4. package/android/src/main/java/com/reactnative/hybridnavigation/GardenModule.java +1 -1
  5. package/android/src/main/java/com/reactnative/hybridnavigation/GlobalStyle.java +1 -1
  6. package/android/src/main/java/com/reactnative/hybridnavigation/HBDReactRootView.java +1 -1
  7. package/android/src/main/java/com/reactnative/hybridnavigation/NavigationModule.java +10 -11
  8. package/android/src/main/java/com/reactnative/hybridnavigation/NavigatorRegistry.java +0 -2
  9. package/android/src/main/java/com/reactnative/hybridnavigation/ReactAppCompatActivity.java +32 -64
  10. package/android/src/main/java/com/reactnative/hybridnavigation/ReactAppCompatActivityDelegate.java +1 -1
  11. package/android/src/main/java/com/reactnative/hybridnavigation/ReactBridgeManager.java +8 -8
  12. package/android/src/main/java/com/reactnative/hybridnavigation/ReactFragment.java +1 -1
  13. package/android/src/main/java/com/reactnative/hybridnavigation/ReactFrameLayout.java +1 -1
  14. package/android/src/main/java/com/reactnative/hybridnavigation/ReactTabBarProvider.java +1 -1
  15. package/android/src/main/java/com/reactnative/hybridnavigation/Utils.java +1 -1
  16. package/android/src/main/java/com/reactnative/hybridnavigation/navigator/ScreenNavigator.java +1 -2
  17. package/ios/HybridNavigation/HBDUtils.m +3 -3
  18. package/ios/HybridNavigation/Navigator/HBDNavigatorRegistry.m +2 -2
  19. package/ios/HybridNavigation/ReactBridge/HBDGardenModule.m +8 -8
  20. package/ios/HybridNavigation/ReactBridge/HBDNavigationModule.m +22 -15
  21. package/ios/HybridNavigation/ReactBridge/HBDReactBridgeManager.h +2 -3
  22. package/ios/HybridNavigation/ReactBridge/HBDReactBridgeManager.m +35 -29
  23. package/ios/HybridNavigation/ViewController/HBDNavigationController.m +2 -2
  24. package/ios/HybridNavigation/ViewController/HBDViewController.m +1 -1
  25. package/ios/HybridNavigation/ViewController/UIViewController+HBD.m +1 -1
  26. package/lib/Event.d.ts +0 -2
  27. package/lib/Event.js +0 -9
  28. package/lib/Garden.d.ts +3 -0
  29. package/lib/Garden.js +3 -0
  30. package/lib/GardenModule.d.ts +4 -0
  31. package/lib/GardenModule.js +19 -1
  32. package/lib/Navigation.d.ts +4 -10
  33. package/lib/Navigation.js +8 -28
  34. package/lib/NavigationComponentWrap.d.ts +1 -1
  35. package/lib/NavigationModule.d.ts +1 -1
  36. package/lib/NavigationModule.js +6 -2
  37. package/lib/Navigator.d.ts +5 -2
  38. package/lib/Navigator.js +35 -15
  39. package/lib/ReactRegistry.d.ts +3 -0
  40. package/lib/ReactRegistry.js +3 -0
  41. package/lib/Route.d.ts +13 -8
  42. package/lib/handler/DispatchCommandHandler.d.ts +2 -0
  43. package/lib/handler/LayoutCommandHandler.d.ts +1 -3
  44. package/lib/handler/LayoutCommandHandler.js +2 -19
  45. package/lib/hooks.d.ts +4 -0
  46. package/lib/hooks.js +4 -0
  47. package/lib/index.d.ts +2 -5
  48. package/lib/index.js +3 -11
  49. package/lib/router/drawer.d.ts +3 -1
  50. package/lib/router/drawer.js +17 -17
  51. package/lib/router/index.d.ts +19 -15
  52. package/lib/router/index.js +137 -125
  53. package/lib/router/screen.d.ts +5 -1
  54. package/lib/router/screen.js +29 -0
  55. package/lib/router/stack.d.ts +5 -1
  56. package/lib/router/stack.js +59 -46
  57. package/lib/router/tabs.d.ts +3 -1
  58. package/lib/router/tabs.js +18 -28
  59. package/package.json +1 -1
  60. package/src/Event.ts +0 -11
  61. package/src/Garden.ts +4 -0
  62. package/src/GardenModule.ts +27 -1
  63. package/src/Navigation.ts +12 -37
  64. package/src/NavigationComponentWrap.tsx +1 -3
  65. package/src/NavigationModule.ts +7 -3
  66. package/src/Navigator.ts +36 -16
  67. package/src/ReactRegistry.tsx +3 -0
  68. package/src/Route.ts +14 -9
  69. package/src/handler/DispatchCommandHandler.ts +2 -0
  70. package/src/handler/LayoutCommandHandler.ts +2 -22
  71. package/src/hooks.ts +4 -1
  72. package/src/index.ts +3 -29
  73. package/src/router/drawer.ts +20 -17
  74. package/src/router/index.ts +145 -131
  75. package/src/router/screen.ts +34 -1
  76. package/src/router/stack.ts +61 -48
  77. package/src/router/tabs.ts +19 -29
package/src/hooks.ts CHANGED
@@ -1,6 +1,5 @@
1
1
  import React, { useContext, useEffect, useRef, useState } from 'react'
2
2
  import Navigation from './Navigation'
3
-
4
3
  import { Navigator, NavigationContext } from './Navigator'
5
4
 
6
5
  export function useVisible() {
@@ -52,6 +51,10 @@ export function useNavigator(): Navigator {
52
51
  return useContext<Navigator>(NavigationContext)
53
52
  }
54
53
 
54
+ /**
55
+ * @deprecated Use Navigation related methods instead.
56
+ * @returns
57
+ */
55
58
  export function useGarden() {
56
59
  const ctx = useContext<Navigator>(NavigationContext)
57
60
  return ctx.garden
package/src/index.ts CHANGED
@@ -1,38 +1,12 @@
1
- import {
2
- withNavigation,
3
- withNavigationItem,
4
- NavigationProps,
5
- InjectedProps,
6
- } from './NavigationComponentWrap'
7
-
1
+ import { withNavigation } from './NavigationComponentWrap'
8
2
  import Navigation from './Navigation'
9
3
 
10
4
  Navigation.setNavigationComponentWrap(withNavigation)
11
5
 
12
- function statusBarHeight() {
13
- return Navigation.statusBarHeight()
14
- }
15
-
16
- function toolbarHeight() {
17
- return Navigation.toolbarHeight()
18
- }
19
-
20
- function topBarHeight() {
21
- return Navigation.topBarHeight()
22
- }
23
-
24
6
  export default Navigation
25
-
26
7
  export { RESULT_OK, RESULT_CANCEL } from './Navigation'
27
-
28
- export {
29
- withNavigationItem,
30
- NavigationProps,
31
- InjectedProps,
32
- statusBarHeight,
33
- toolbarHeight,
34
- topBarHeight,
35
- }
8
+ export { statusBarHeight, toolbarHeight, topBarHeight } from './GardenModule'
9
+ export { withNavigationItem, NavigationProps, InjectedProps } from './NavigationComponentWrap'
36
10
 
37
11
  export * from './Navigator'
38
12
  export * from './Garden'
@@ -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
  }
@@ -1,8 +1,4 @@
1
1
  import { pathToRegexp, match } from 'path-to-regexp'
2
- import { stackRouteHandler } from './stack'
3
- import { tabsRouteHandler } from './tabs'
4
- import { drawerRouteHandler } from './drawer'
5
- import { Navigator } from '../Navigator'
6
2
  import {
7
3
  RouteConfig,
8
4
  RouteData,
@@ -13,176 +9,194 @@ import {
13
9
  } from '../Route'
14
10
  import { NavigationItem } from '../Options'
15
11
  import Navigation from '../Navigation'
12
+ import { StackRouteHandler } from './stack'
13
+ import { ScreenRouteHandler } from './screen'
14
+ import { TabsRouteHandler } from './tabs'
15
+ import { DrawerRouteHandler } from './drawer'
16
16
 
17
17
  interface IndexType {
18
18
  [index: string]: any
19
19
  }
20
20
 
21
- let routeDatas = new Map<string, RouteData>()
22
- let interceptors = new Set<RouteInterceptor>()
23
- let handlers = new Set<RouteHandler>([drawerRouteHandler, tabsRouteHandler, stackRouteHandler])
24
-
25
- const traverseHandlers: RouteHandler = async (
26
- graph: RouteGraph,
27
- route: RouteInfo,
28
- next: RouteHandler,
29
- ) => {
30
- for (let handler of handlers.values()) {
31
- if (await handler(graph, route, next)) {
32
- return true
33
- }
34
- }
35
- return false
36
- }
37
-
38
- function addInterceptor(func: RouteInterceptor) {
39
- interceptors.add(func)
40
- }
41
-
42
- function removeInterceptor(func: RouteInterceptor) {
43
- interceptors.delete(func)
21
+ function pathParams(path: string) {
22
+ const pathMatch = match<IndexType>(path, {
23
+ encode: encodeURI,
24
+ decode: decodeURIComponent,
25
+ })(path)
26
+ return pathMatch ? pathMatch.params : {}
44
27
  }
45
28
 
46
- function use(handler: RouteHandler) {
47
- handlers.add(handler)
29
+ function queryParams(queryString: string) {
30
+ return (queryString || '').split('&').reduce((result: IndexType, item: string) => {
31
+ if (item !== '') {
32
+ const nextResult = result || {}
33
+ const [key, value] = item.split('=')
34
+ nextResult[key] = value
35
+ return nextResult
36
+ }
37
+ return result
38
+ }, {})
48
39
  }
49
40
 
50
- function registerRoute(moduleName: string, route: RouteConfig) {
51
- let { path, dependency, mode } = route
52
- let regexp: RegExp | undefined
53
- if (path) {
54
- if (!path.startsWith('/')) {
55
- route.path = '/' + route.path
56
- }
57
- regexp = pathToRegexp(path)
41
+ class Router {
42
+ constructor() {
43
+ this.registerRouteHandler('screen', new ScreenRouteHandler())
44
+ this.registerRouteHandler('stack', new StackRouteHandler())
45
+ this.registerRouteHandler('tabs', new TabsRouteHandler())
46
+ this.registerRouteHandler('drawer', new DrawerRouteHandler())
58
47
  }
59
- mode = mode || 'push'
60
- routeDatas.set(moduleName, { moduleName, mode, path, regexp, dependency })
61
- }
62
48
 
63
- let isRouteConfigInflated = false
49
+ private handlers = new Map<string, RouteHandler>()
64
50
 
65
- function inflateRouteConfigs() {
66
- const routeConfigs = Navigation.routeConfigs()
67
- for (const [moduleName, config] of routeConfigs) {
68
- registerRoute(moduleName, config)
51
+ registerRouteHandler(layout: string, handler: RouteHandler) {
52
+ this.handlers.set(layout, handler)
69
53
  }
70
- }
71
54
 
72
- async function open(path: string, props: object = {}, options: NavigationItem = {}) {
73
- if (!isRouteConfigInflated) {
74
- inflateRouteConfigs()
75
- isRouteConfigInflated = true
55
+ private convertConfigToData(moduleName: string, route: RouteConfig) {
56
+ let { path, dependency, mode } = route
57
+ let regexp: RegExp | undefined
58
+ if (path) {
59
+ if (!path.startsWith('/')) {
60
+ route.path = '/' + route.path
61
+ }
62
+ regexp = pathToRegexp(path)
63
+ }
64
+ mode = mode || 'push'
65
+ return { moduleName, mode, path, regexp, dependency }
76
66
  }
77
67
 
78
- let intercepted = false
79
- for (let interceptor of interceptors.values()) {
80
- const result = interceptor(path)
81
- if (result instanceof Promise) {
82
- intercepted = await result
83
- } else {
84
- intercepted = result
85
- }
68
+ private isRouteConfigInflated = false
69
+ private routeDatas = new Map<string, RouteData>()
86
70
 
87
- if (intercepted) {
71
+ private inflateRouteData() {
72
+ if (this.isRouteConfigInflated) {
88
73
  return
89
74
  }
75
+ this.isRouteConfigInflated = true
76
+ const routeConfigs = Navigation.routeConfigs()
77
+ for (const [moduleName, config] of routeConfigs) {
78
+ const data = this.convertConfigToData(moduleName, config)
79
+ this.routeDatas.set(moduleName, data)
80
+ }
90
81
  }
91
82
 
92
- const route = pathToRoute(path, props, options)
83
+ private interceptors: RouteInterceptor[] = []
93
84
 
94
- if (!route) {
95
- return
85
+ addInterceptor(interceptor: RouteInterceptor) {
86
+ this.interceptors.push(interceptor)
96
87
  }
97
88
 
98
- const graphArray = await Navigator.routeGraph()
99
- if (graphArray.length > 1) {
100
- for (let index = graphArray.length - 1; index > 0; index--) {
101
- const { mode: layoutMode } = graphArray[index]
102
- const navigator = await Navigator.current()
103
- if (layoutMode === 'present') {
104
- await navigator.dismiss()
105
- } else if (layoutMode === 'modal') {
106
- await navigator.hideModal()
107
- } else {
108
- console.warn('尚未处理的 layout mode:' + layoutMode)
89
+ removeInterceptor(interceptor: RouteInterceptor) {
90
+ this.interceptors = this.interceptors.filter(item => item !== interceptor)
91
+ }
92
+
93
+ private async intercept(path: string) {
94
+ for (let interceptor of this.interceptors.values()) {
95
+ const result = interceptor(path)
96
+ if (result instanceof Promise) {
97
+ return await result
109
98
  }
99
+ return result
110
100
  }
111
101
  }
112
102
 
113
- if (!(await traverseHandlers(graphArray[0], route, traverseHandlers))) {
114
- const navigator = await Navigator.current()
115
- navigator.closeMenu()
116
- const { moduleName, mode: routeMode, props: initialProps } = route
117
- if (routeMode === 'present') {
118
- navigator.present(moduleName, initialProps)
119
- } else if (routeMode === 'modal') {
120
- navigator.showModal(moduleName, initialProps)
121
- } else {
122
- // default push
123
- navigator.push(moduleName, initialProps)
103
+ private pathToRoute(path: string, props: object, options: NavigationItem): RouteInfo | undefined {
104
+ for (const data of this.routeDatas.values()) {
105
+ if (!data.regexp) {
106
+ continue
107
+ }
108
+
109
+ const [pathName, queryString] = path.split('?')
110
+
111
+ if (data.regexp.exec(pathName)) {
112
+ const moduleName = data.moduleName
113
+ const params = pathParams(pathName)
114
+ const query = queryParams(queryString)
115
+ props = { ...query, ...params, ...props }
116
+ const dependencies = this.routeDependencies(data)
117
+ const mode = data.mode
118
+ return { moduleName, props, dependencies, mode, options }
119
+ }
124
120
  }
125
121
  }
126
- }
127
122
 
128
- function pathToRoute(path: string, props: object, options: NavigationItem): RouteInfo | undefined {
129
- for (const data of routeDatas.values()) {
130
- if (!data.regexp) {
131
- continue
123
+ private routeDependencies(routeData: RouteData) {
124
+ let dependencies: string[] = []
125
+ let data: RouteData | undefined = routeData
126
+ while (data && data.dependency) {
127
+ dependencies.push(data.dependency)
128
+ data = this.routeDatas.get(data.dependency)
132
129
  }
130
+ return dependencies.reverse()
131
+ }
133
132
 
134
- const [pathName, queryString] = path.split('?')
133
+ private async processRoute(route: RouteInfo, graph: RouteGraph): Promise<boolean> {
134
+ const handler = this.handlers.get(graph.layout)
135
+ if (!handler) {
136
+ return false
137
+ }
135
138
 
136
- if (data.regexp.exec(pathName)) {
137
- const moduleName = data.moduleName
138
- const params = pathParams(pathName)
139
- const query = queryParams(queryString)
140
- props = { ...query, ...params, ...props }
141
- const dependencies = routeDependencies(data)
142
- const mode = data.mode
143
- return { moduleName, props, dependencies, mode, options }
139
+ const [consumed, childGraph] = await handler.process(graph, route)
140
+ if (consumed && childGraph) {
141
+ return await this.processRoute(route, childGraph)
144
142
  }
143
+ return consumed
145
144
  }
146
- }
147
145
 
148
- function pathParams(path: string) {
149
- const pathMatch = match<IndexType>(path, {
150
- encode: encodeURI,
151
- decode: decodeURIComponent,
152
- })(path)
153
- return pathMatch ? pathMatch.params : {}
154
- }
146
+ async open(path: string, props: object = {}, options: NavigationItem = {}) {
147
+ this.inflateRouteData()
155
148
 
156
- function queryParams(queryString: string) {
157
- return (queryString || '').split('&').reduce((result: IndexType, item: string) => {
158
- if (item !== '') {
159
- const nextResult = result || {}
160
- const [key, value] = item.split('=')
161
- nextResult[key] = value
162
- return nextResult
149
+ const intercepted = await this.intercept(path)
150
+ if (intercepted) {
151
+ return
152
+ }
153
+
154
+ const route = this.pathToRoute(path, props, options)
155
+ if (!route) {
156
+ return
157
+ }
158
+
159
+ const graphArray = await Navigation.routeGraph()
160
+ if (graphArray.length > 1) {
161
+ for (let index = graphArray.length - 1; index > 0; index--) {
162
+ const { mode: layoutMode } = graphArray[index]
163
+ const { sceneId, moduleName } = await Navigation.currentRoute()
164
+ if (layoutMode === 'present') {
165
+ await Navigation.dispatch(sceneId, 'dismiss', { from: moduleName })
166
+ } else if (layoutMode === 'modal') {
167
+ await Navigation.dispatch(sceneId, 'hideModal', { from: moduleName })
168
+ } else {
169
+ console.warn('尚未处理的 layout mode:' + layoutMode)
170
+ }
171
+ }
163
172
  }
164
- return result
165
- }, {})
166
- }
167
173
 
168
- function routeDependencies(routeData: RouteData) {
169
- let dependencies: string[] = []
170
- let data: RouteData | undefined = routeData
171
- while (data && data.dependency) {
172
- dependencies.push(data.dependency)
173
- data = routeDatas.get(data.dependency)
174
+ const routeGraph = graphArray[0]
175
+
176
+ const consumed = await this.processRoute(route, routeGraph)
177
+ if (consumed) {
178
+ return
179
+ }
180
+
181
+ const { sceneId, moduleName: from } = await Navigation.currentRoute()
182
+ Navigation.dispatch(sceneId, 'closeMenu', { from })
183
+ const { moduleName, mode: routeMode, props: initialProps } = route
184
+ let action = 'push'
185
+ if (routeMode === 'present') {
186
+ action = 'present'
187
+ } else if (routeMode === 'modal') {
188
+ action = 'showModal'
189
+ }
190
+ Navigation.dispatch(sceneId, action, {
191
+ moduleName,
192
+ props: initialProps,
193
+ from,
194
+ to: moduleName,
195
+ })
174
196
  }
175
- return dependencies.reverse()
176
197
  }
177
198
 
178
- export const router = {
179
- addInterceptor,
180
- removeInterceptor,
181
- registerRoute,
182
- use,
183
- open,
184
- pathToRoute,
185
- }
199
+ export const router = new Router()
186
200
 
187
201
  export * from './drawer'
188
202
  export * from './screen'
@@ -1,4 +1,4 @@
1
- import { LayoutMode, RouteGraph } from '../Route'
1
+ import { LayoutMode, RouteGraph, RouteHandler, RouteInfo } from '../Route'
2
2
 
3
3
  export interface ScreenGraph extends RouteGraph {
4
4
  layout: 'screen'
@@ -10,3 +10,36 @@ export interface ScreenGraph extends RouteGraph {
10
10
  export function isScreenGraph(graph: RouteGraph): graph is ScreenGraph {
11
11
  return graph.layout === 'screen'
12
12
  }
13
+
14
+ function extractModuleNames(graph: RouteGraph, set: string[]) {
15
+ if (isScreenGraph(graph)) {
16
+ set.push(graph.moduleName)
17
+ } else {
18
+ const children = graph.children!
19
+ for (let i = 0; i < children.length; i++) {
20
+ extractModuleNames(children[i], set)
21
+ }
22
+ }
23
+ }
24
+
25
+ export function isTargetLocateIn(graph: RouteGraph, target: RouteInfo) {
26
+ const existings: string[] = []
27
+ extractModuleNames(graph, existings)
28
+ const { dependencies, moduleName } = target
29
+ const expectations = [...dependencies, moduleName]
30
+ return expectations.some(name => existings.includes(name))
31
+ }
32
+
33
+ export class ScreenRouteHandler implements RouteHandler {
34
+ process(graph: RouteGraph, target: RouteInfo): Promise<[boolean, RouteGraph | null]> {
35
+ if (!isScreenGraph(graph)) {
36
+ throw new Error(`${graph} is Not a ScreenGraph`)
37
+ }
38
+
39
+ if (graph.moduleName === target.moduleName) {
40
+ return Promise.resolve([true, null])
41
+ }
42
+
43
+ return Promise.resolve([false, graph])
44
+ }
45
+ }
@@ -1,6 +1,6 @@
1
- import { isScreenGraph } from './screen'
2
- import { Navigator } from '../Navigator'
1
+ import { ScreenGraph, isScreenGraph, isTargetLocateIn } from './screen'
3
2
  import { LayoutMode, RouteGraph, RouteHandler, RouteInfo } from '../Route'
3
+ import Navigation from '../Navigation'
4
4
 
5
5
  export interface StackGraph extends RouteGraph {
6
6
  layout: 'stack'
@@ -13,68 +13,81 @@ export function isStackGraph(graph: RouteGraph): graph is StackGraph {
13
13
  return graph.layout === 'stack'
14
14
  }
15
15
 
16
- export async function stackRouteHandler(graph: RouteGraph, route: RouteInfo, next: RouteHandler) {
17
- if (!isStackGraph(graph)) {
18
- return false
19
- }
20
-
21
- const { mode, moduleName, dependencies, props } = route
16
+ export class StackRouteHandler implements RouteHandler {
17
+ async process(graph: RouteGraph, target: RouteInfo): Promise<[boolean, RouteGraph | null]> {
18
+ if (!isStackGraph(graph)) {
19
+ throw new Error(`${graph} is Not a StackGraph`)
20
+ }
22
21
 
23
- if (mode !== 'push') {
24
- return false
25
- }
22
+ const { children } = graph
26
23
 
27
- const { children } = graph
28
- const expectedModuleNames = [...dependencies, moduleName]
29
- let expectedIndex = -1
30
- let childIndex = -1
24
+ for (let i = children.length - 1; i > -1; i--) {
25
+ const childGraph = children[i]
26
+ if (isTargetLocateIn(childGraph, target)) {
27
+ if (isScreenGraph(childGraph)) {
28
+ this.navigateTo(childGraph, target)
29
+ return [true, null]
30
+ }
31
31
 
32
- for (let i = children.length - 1; i > -1; i--) {
33
- const existingModuleNames: string[] = []
34
- extractModuleNames(children[i], existingModuleNames)
35
- expectedIndex = expectedModuleNames.findIndex(name => existingModuleNames.includes(name))
36
- if (expectedIndex !== -1) {
37
- childIndex = i
38
- break
32
+ await this.popToIfNeeded(childGraph, i === children.length - 1)
33
+ return [true, childGraph]
34
+ }
39
35
  }
36
+
37
+ return [false, graph]
40
38
  }
41
39
 
42
- if (expectedIndex !== -1) {
43
- if (!isScreenGraph(children[childIndex])) {
44
- await next(children[childIndex], route, next)
40
+ private async popToIfNeeded(childGraph: RouteGraph, isLastChildAtStack: boolean) {
41
+ if (!isLastChildAtStack) {
42
+ await Navigation.dispatch(childGraph.sceneId, 'popTo', {
43
+ moduleName: childGraph.sceneId,
44
+ })
45
45
  }
46
+ }
47
+
48
+ private navigateTo(childGraph: ScreenGraph, target: RouteInfo) {
49
+ const { mode, moduleName, dependencies, props } = target
50
+ const expectations = [...dependencies, moduleName]
51
+ const index = expectations.findIndex(name => name === childGraph.moduleName)
52
+ const pendings = expectations.slice(index + 1)
53
+ console.log('pendings', pendings, 'moduleName', childGraph.moduleName)
54
+ const sceneId = childGraph.sceneId
46
55
 
47
- let pendingModuleNames = expectedModuleNames.slice(expectedIndex + 1)
48
- const navigator = Navigator.of(children[children.length - 1].sceneId)
56
+ const moduleExisting = pendings.length === 0
49
57
 
50
- if (pendingModuleNames.length === 0) {
58
+ if (moduleExisting) {
51
59
  if (JSON.stringify(props) === '{}') {
52
- await navigator.popTo(moduleName)
60
+ Navigation.dispatch(sceneId, 'popTo', {
61
+ moduleName,
62
+ from: childGraph.moduleName,
63
+ to: moduleName,
64
+ })
53
65
  } else {
54
- navigator.redirectTo(moduleName, props)
66
+ Navigation.dispatch(sceneId, 'redirectTo', {
67
+ moduleName,
68
+ from: childGraph.moduleName,
69
+ to: moduleName,
70
+ props,
71
+ })
55
72
  }
56
73
  } else {
57
- for (let i = 0; i < pendingModuleNames.length; i++) {
58
- if (i === pendingModuleNames.length - 1) {
59
- navigator.push(moduleName, props)
74
+ for (let i = 0; i < pendings.length; i++) {
75
+ const isLast = i === pendings.length - 1
76
+ if (isLast) {
77
+ Navigation.dispatch(sceneId, 'push', {
78
+ moduleName,
79
+ from: childGraph.moduleName,
80
+ to: moduleName,
81
+ props,
82
+ })
60
83
  } else {
61
- navigator.push(pendingModuleNames[i], {}, {})
84
+ Navigation.dispatch(sceneId, 'push', {
85
+ moduleName: pendings[i],
86
+ from: childGraph.moduleName,
87
+ to: pendings[i],
88
+ })
62
89
  }
63
90
  }
64
91
  }
65
- return true
66
- }
67
-
68
- return false
69
- }
70
-
71
- function extractModuleNames(graph: RouteGraph, set: string[]) {
72
- if (isScreenGraph(graph)) {
73
- set.push(graph.moduleName)
74
- } else {
75
- const children = graph.children!
76
- for (let i = 0; i < children.length; i++) {
77
- extractModuleNames(children[i], set)
78
- }
79
92
  }
80
93
  }