hybrid-navigation 2.13.0 → 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.
@@ -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
  }
@@ -1,6 +1,6 @@
1
- import { Navigator } from '../Navigator'
2
- import { isScreenGraph } from './screen'
1
+ import { isTargetLocateIn } from './screen'
3
2
  import { LayoutMode, RouteGraph, RouteHandler, RouteInfo } from '../Route'
3
+ import Navigation from '../Navigation'
4
4
 
5
5
  export interface TabsGraph extends RouteGraph {
6
6
  layout: 'tabs'
@@ -13,37 +13,27 @@ export interface TabsGraph extends RouteGraph {
13
13
  export function isTabsGraph(graph: RouteGraph): graph is TabsGraph {
14
14
  return graph.layout === 'tabs'
15
15
  }
16
+ export class TabsRouteHandler implements RouteHandler {
17
+ async process(graph: RouteGraph, target: RouteInfo): Promise<[boolean, RouteGraph]> {
18
+ if (!isTabsGraph(graph)) {
19
+ throw new Error(`${graph} is NOT a TabsGraph`)
20
+ }
16
21
 
17
- export async function tabsRouteHandler(graph: RouteGraph, route: RouteInfo, next: RouteHandler) {
18
- if (!isTabsGraph(graph)) {
19
- return false
20
- }
21
-
22
- const { children, selectedIndex } = graph
23
- const { dependencies, moduleName } = route
24
- const expectedModuleNames = [...dependencies, moduleName]
22
+ const { children, selectedIndex } = graph
25
23
 
26
- for (let i = 0; i < children.length; i++) {
27
- const existingModuleNames: string[] = []
28
- extractModuleNames(children[i], existingModuleNames)
29
- if (expectedModuleNames.some(name => existingModuleNames.includes(name))) {
30
- if (selectedIndex !== i) {
31
- const navigator = Navigator.of(children[i].sceneId)
32
- await navigator.switchTab(i, true)
24
+ for (let i = 0; i < children.length; i++) {
25
+ if (isTargetLocateIn(children[i], target)) {
26
+ if (selectedIndex !== i) {
27
+ await Navigation.dispatch(children[selectedIndex].sceneId, 'switchTab', {
28
+ from: selectedIndex,
29
+ to: i,
30
+ popToRoot: true,
31
+ })
32
+ }
33
+ return [true, children[i]]
33
34
  }
34
- return next(children[i], route, next)
35
35
  }
36
- }
37
- return false
38
- }
39
36
 
40
- function extractModuleNames(graph: RouteGraph, set: string[]) {
41
- if (isScreenGraph(graph)) {
42
- set.push(graph.moduleName)
43
- } else {
44
- const children = graph.children!
45
- for (let i = 0; i < children.length; i++) {
46
- extractModuleNames(children[i], set)
47
- }
37
+ return [false, graph]
48
38
  }
49
39
  }