1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.default = useOnAction;
|
7 |
|
8 | var React = _interopRequireWildcard(require("react"));
|
9 |
|
10 | var _NavigationBuilderContext = _interopRequireDefault(require("./NavigationBuilderContext"));
|
11 |
|
12 | var _useOnPreventRemove = _interopRequireWildcard(require("./useOnPreventRemove"));
|
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 |
|
26 |
|
27 |
|
28 |
|
29 | function 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();
|
53 |
|
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);
|
63 |
|
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 |
|
82 |
|
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 |
|
96 | if (onActionParent(action, visitedNavigators)) {
|
97 | return true;
|
98 | }
|
99 | }
|
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 |
|
\ | No newline at end of file |