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