1 | import { CommonActions } from '@react-navigation/routers';
|
2 | import * as React from 'react';
|
3 | import NavigationBuilderContext from './NavigationBuilderContext';
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | export default function useNavigationCache(_ref) {
|
11 | let {
|
12 | state,
|
13 | getState,
|
14 | navigation,
|
15 | setOptions,
|
16 | router,
|
17 | emitter
|
18 | } = _ref;
|
19 | const {
|
20 | stackRef
|
21 | } = React.useContext(NavigationBuilderContext);
|
22 |
|
23 |
|
24 |
|
25 | const cache = React.useMemo(() => ({
|
26 | current: {}
|
27 | }),
|
28 | [getState, navigation, setOptions, router, emitter]);
|
29 | const actions = { ...router.actionCreators,
|
30 | ...CommonActions
|
31 | };
|
32 | cache.current = state.routes.reduce((acc, route) => {
|
33 | const previous = cache.current[route.key];
|
34 |
|
35 | if (previous) {
|
36 |
|
37 | acc[route.key] = previous;
|
38 | } else {
|
39 |
|
40 | const {
|
41 | emit,
|
42 | ...rest
|
43 | } = navigation;
|
44 |
|
45 | const dispatch = thunk => {
|
46 | const action = typeof thunk === 'function' ? thunk(getState()) : thunk;
|
47 |
|
48 | if (action != null) {
|
49 | navigation.dispatch({
|
50 | source: route.key,
|
51 | ...action
|
52 | });
|
53 | }
|
54 | };
|
55 |
|
56 | const withStack = callback => {
|
57 | let isStackSet = false;
|
58 |
|
59 | try {
|
60 | if (process.env.NODE_ENV !== 'production' && stackRef && !stackRef.current) {
|
61 |
|
62 | stackRef.current = new Error().stack;
|
63 | isStackSet = true;
|
64 | }
|
65 |
|
66 | callback();
|
67 | } finally {
|
68 | if (isStackSet && stackRef) {
|
69 | stackRef.current = undefined;
|
70 | }
|
71 | }
|
72 | };
|
73 |
|
74 | const helpers = Object.keys(actions).reduce((acc, name) => {
|
75 | acc[name] = function () {
|
76 | for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
77 | args[_key] = arguments[_key];
|
78 | }
|
79 |
|
80 | return withStack(() =>
|
81 | dispatch(actions[name](...args)));
|
82 | };
|
83 |
|
84 | return acc;
|
85 | }, {});
|
86 | acc[route.key] = { ...rest,
|
87 | ...helpers,
|
88 |
|
89 | ...emitter.create(route.key),
|
90 | dispatch: thunk => withStack(() => dispatch(thunk)),
|
91 | getParent: id => {
|
92 | if (id !== undefined && id === rest.getId()) {
|
93 |
|
94 |
|
95 | return acc[route.key];
|
96 | }
|
97 |
|
98 | return rest.getParent(id);
|
99 | },
|
100 | setOptions: options => setOptions(o => ({ ...o,
|
101 | [route.key]: { ...o[route.key],
|
102 | ...options
|
103 | }
|
104 | })),
|
105 | isFocused: () => {
|
106 | const state = getState();
|
107 |
|
108 | if (state.routes[state.index].key !== route.key) {
|
109 | return false;
|
110 | }
|
111 |
|
112 |
|
113 |
|
114 | return navigation ? navigation.isFocused() : true;
|
115 | }
|
116 | };
|
117 | }
|
118 |
|
119 | return acc;
|
120 | }, {});
|
121 | return cache.current;
|
122 | }
|
123 |
|
\ | No newline at end of file |