UNPKG

5.3 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = useOnAction;
7
8var React = _interopRequireWildcard(require("react"));
9
10var _NavigationBuilderContext = _interopRequireDefault(require("./NavigationBuilderContext"));
11
12var _useOnPreventRemove = _interopRequireWildcard(require("./useOnPreventRemove"));
13
14function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
16function _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
18function _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 * Hook to handle actions for a navigator, including state updates and bubbling.
22 *
23 * Bubbling an action is achieved in 2 ways:
24 * 1. To bubble action to parent, we expose the action handler in context and then access the parent context
25 * 2. To bubble action to child, child adds event listeners subscribing to actions from parent
26 *
27 * When the action handler handles as action, it returns `true`, otherwise `false`.
28 */
29function useOnAction(_ref) {
30 let {
31 router,
32 getState,
33 setState,
34 key,
35 actionListeners,
36 beforeRemoveListeners,
37 routerConfigOptions,
38 emitter
39 } = _ref;
40 const {
41 onAction: onActionParent,
42 onRouteFocus: onRouteFocusParent,
43 addListener: addListenerParent,
44 onDispatchAction
45 } = React.useContext(_NavigationBuilderContext.default);
46 const routerConfigOptionsRef = React.useRef(routerConfigOptions);
47 React.useEffect(() => {
48 routerConfigOptionsRef.current = routerConfigOptions;
49 });
50 const onAction = React.useCallback(function (action) {
51 let visitedNavigators = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : new Set();
52 const state = getState(); // Since actions can bubble both up and down, they could come to the same navigator again
53 // We keep track of navigators which have already tried to handle the action and return if it's already visited
54
55 if (visitedNavigators.has(state.key)) {
56 return false;
57 }
58
59 visitedNavigators.add(state.key);
60
61 if (typeof action.target !== 'string' || action.target === state.key) {
62 let result = router.getStateForAction(state, action, routerConfigOptionsRef.current); // If a target is specified and set to current navigator, the action shouldn't bubble
63 // So instead of `null`, we use the state object for such cases to signal that action was handled
64
65 result = result === null && action.target === state.key ? state : result;
66
67 if (result !== null) {
68 onDispatchAction(action, state === result);
69
70 if (state !== result) {
71 const isPrevented = (0, _useOnPreventRemove.shouldPreventRemove)(emitter, beforeRemoveListeners, state.routes, result.routes, action);
72
73 if (isPrevented) {
74 return true;
75 }
76
77 setState(result);
78 }
79
80 if (onRouteFocusParent !== undefined) {
81 // Some actions such as `NAVIGATE` also want to bring the navigated route to focus in the whole tree
82 // This means we need to focus all of the parent navigators of this navigator as well
83 const shouldFocus = router.shouldActionChangeFocus(action);
84
85 if (shouldFocus && key !== undefined) {
86 onRouteFocusParent(key);
87 }
88 }
89
90 return true;
91 }
92 }
93
94 if (onActionParent !== undefined) {
95 // Bubble action to the parent if the current navigator didn't handle it
96 if (onActionParent(action, visitedNavigators)) {
97 return true;
98 }
99 } // If the action wasn't handled by current navigator or a parent navigator, let children handle it
100
101
102 for (let i = actionListeners.length - 1; i >= 0; i--) {
103 const listener = actionListeners[i];
104
105 if (listener(action, visitedNavigators)) {
106 return true;
107 }
108 }
109
110 return false;
111 }, [actionListeners, beforeRemoveListeners, emitter, getState, key, onActionParent, onDispatchAction, onRouteFocusParent, router, setState]);
112 (0, _useOnPreventRemove.default)({
113 getState,
114 emitter,
115 beforeRemoveListeners
116 });
117 React.useEffect(() => addListenerParent === null || addListenerParent === void 0 ? void 0 : addListenerParent('action', onAction), [addListenerParent, onAction]);
118 return onAction;
119}
120//# sourceMappingURL=useOnAction.js.map
\No newline at end of file