You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1 line
10 KiB
1 line
10 KiB
{"ast":null,"code":"import * as React from 'react';\nimport NavigationBuilderContext from \"./NavigationBuilderContext\";\nimport useOnPreventRemove, { shouldPreventRemove } from \"./useOnPreventRemove\";\nexport default function useOnAction(_ref) {\n var router = _ref.router,\n getState = _ref.getState,\n setState = _ref.setState,\n key = _ref.key,\n actionListeners = _ref.actionListeners,\n beforeRemoveListeners = _ref.beforeRemoveListeners,\n routerConfigOptions = _ref.routerConfigOptions,\n emitter = _ref.emitter;\n var _React$useContext = React.useContext(NavigationBuilderContext),\n onActionParent = _React$useContext.onAction,\n onRouteFocusParent = _React$useContext.onRouteFocus,\n addListenerParent = _React$useContext.addListener,\n onDispatchAction = _React$useContext.onDispatchAction;\n var routerConfigOptionsRef = React.useRef(routerConfigOptions);\n React.useEffect(function () {\n routerConfigOptionsRef.current = routerConfigOptions;\n });\n var onAction = React.useCallback(function (action) {\n var visitedNavigators = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : new Set();\n var state = getState();\n if (visitedNavigators.has(state.key)) {\n return false;\n }\n visitedNavigators.add(state.key);\n if (typeof action.target !== 'string' || action.target === state.key) {\n var result = router.getStateForAction(state, action, routerConfigOptionsRef.current);\n result = result === null && action.target === state.key ? state : result;\n if (result !== null) {\n onDispatchAction(action, state === result);\n if (state !== result) {\n var isPrevented = shouldPreventRemove(emitter, beforeRemoveListeners, state.routes, result.routes, action);\n if (isPrevented) {\n return true;\n }\n setState(result);\n }\n if (onRouteFocusParent !== undefined) {\n var shouldFocus = router.shouldActionChangeFocus(action);\n if (shouldFocus && key !== undefined) {\n onRouteFocusParent(key);\n }\n }\n return true;\n }\n }\n if (onActionParent !== undefined) {\n if (onActionParent(action, visitedNavigators)) {\n return true;\n }\n }\n for (var i = actionListeners.length - 1; i >= 0; i--) {\n var listener = actionListeners[i];\n if (listener(action, visitedNavigators)) {\n return true;\n }\n }\n return false;\n }, [actionListeners, beforeRemoveListeners, emitter, getState, key, onActionParent, onDispatchAction, onRouteFocusParent, router, setState]);\n useOnPreventRemove({\n getState: getState,\n emitter: emitter,\n beforeRemoveListeners: beforeRemoveListeners\n });\n React.useEffect(function () {\n return addListenerParent === null || addListenerParent === void 0 ? void 0 : addListenerParent('action', onAction);\n }, [addListenerParent, onAction]);\n return onAction;\n}","map":{"version":3,"sources":["useOnAction.tsx"],"names":["React","NavigationBuilderContext","useOnPreventRemove","shouldPreventRemove","useOnAction","router","getState","setState","key","actionListeners","beforeRemoveListeners","routerConfigOptions","emitter","onAction","onActionParent","onRouteFocus","onRouteFocusParent","addListener","addListenerParent","onDispatchAction","useContext","routerConfigOptionsRef","useRef","useEffect","current","useCallback","action","visitedNavigators","Set","state","has","add","target","result","getStateForAction","isPrevented","routes","undefined","shouldFocus","shouldActionChangeFocus","i","length","listener"],"mappings":"AAOA,OAAO,KAAKA,KAAK,MAAM,OAAO;AAE9B,OAAOC,wBAAwB;AAM/B,OAAOC,kBAAkB,IAAIC,mBAAmB;AAsBhD,eAAe,SAASC,WAAW,CAAA,IAAA,EASvB;EATwB,IAClCC,MAAM,GAQE,IAAA,CARRA,MAAM;IACNC,QAAQ,GAOA,IAAA,CAPRA,QAAQ;IACRC,QAAQ,GAMA,IAAA,CANRA,QAAQ;IACRC,GAAG,GAKK,IAAA,CALRA,GAAG;IACHC,eAAe,GAIP,IAAA,CAJRA,eAAe;IACfC,qBAAqB,GAGb,IAAA,CAHRA,qBAAqB;IACrBC,mBAAmB,GAEX,IAAA,CAFRA,mBAAmB;IACnBC,OAAAA,GACQ,IAAA,CADRA,OAAAA;EAEA,wBAKIZ,KAAK,CAACoB,UAAU,CAACnB,wBAAwB,CAAC;IAJlCa,cAAc,qBAAxBD,QAAQ;IACMG,kBAAkB,qBAAhCD,YAAY;IACCG,iBAAiB,qBAA9BD,WAAW;IACXE,gBAAAA,qBAAAA,gBAAAA;EAGF,IAAME,sBAAsB,GAC1BrB,KAAK,CAACs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derContext from './NavigationBuilderContext';\nimport useOnPreventRemove, { shouldPreventRemove } from './useOnPreventRemove';\n/**\n * Hook to handle actions for a navigator, including state updates and bubbling.\n *\n * Bubbling an action is achieved in 2 ways:\n * 1. To bubble action to parent, we expose the action handler in context and then access the parent context\n * 2. To bubble action to child, child adds event listeners subscribing to actions from parent\n *\n * When the action handler handles as action, it returns `true`, otherwise `false`.\n */\nexport default function useOnAction(_ref) {\n let {\n router,\n getState,\n setState,\n key,\n actionListeners,\n beforeRemoveListeners,\n routerConfigOptions,\n emitter\n } = _ref;\n const {\n onAction: onActionParent,\n onRouteFocus: onRouteFocusParent,\n addListener: addListenerParent,\n onDispatchAction\n } = React.useContext(NavigationBuilderContext);\n const routerConfigOptionsRef = React.useRef(routerConfigOptions);\n React.useEffect(() => {\n routerConfigOptionsRef.current = routerConfigOptions;\n });\n const onAction = React.useCallback(function (action) {\n let visitedNavigators = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : new Set();\n const state = getState();\n\n // Since actions can bubble both up and down, they could come to the same navigator again\n // We keep track of navigators which have already tried to handle the action and return if it's already visited\n if (visitedNavigators.has(state.key)) {\n return false;\n }\n visitedNavigators.add(state.key);\n if (typeof action.target !== 'string' || action.target === state.key) {\n let result = router.getStateForAction(state, action, routerConfigOptionsRef.current);\n\n // If a target is specified and set to current navigator, the action shouldn't bubble\n // So instead of `null`, we use the state object for such cases to signal that action was handled\n result = result === null && action.target === state.key ? state : result;\n if (result !== null) {\n onDispatchAction(action, state === result);\n if (state !== result) {\n const isPrevented = shouldPreventRemove(emitter, beforeRemoveListeners, state.routes, result.routes, action);\n if (isPrevented) {\n return true;\n }\n setState(result);\n }\n if (onRouteFocusParent !== undefined) {\n // Some actions such as `NAVIGATE` also want to bring the navigated route to focus in the whole tree\n // This means we need to focus all of the parent navigators of this navigator as well\n const shouldFocus = router.shouldActionChangeFocus(action);\n if (shouldFocus && key !== undefined) {\n onRouteFocusParent(key);\n }\n }\n return true;\n }\n }\n if (onActionParent !== undefined) {\n // Bubble action to the parent if the current navigator didn't handle it\n if (onActionParent(action, visitedNavigators)) {\n return true;\n }\n }\n\n // If the action wasn't handled by current navigator or a parent navigator, let children handle it\n for (let i = actionListeners.length - 1; i >= 0; i--) {\n const listener = actionListeners[i];\n if (listener(action, visitedNavigators)) {\n return true;\n }\n }\n return false;\n }, [actionListeners, beforeRemoveListeners, emitter, getState, key, onActionParent, onDispatchAction, onRouteFocusParent, router, setState]);\n useOnPreventRemove({\n getState,\n emitter,\n beforeRemoveListeners\n });\n React.useEffect(() => addListenerParent === null || addListenerParent === void 0 ? void 0 : addListenerParent('action', onAction), [addListenerParent, onAction]);\n return onAction;\n}\n//# sourceMappingURL=useOnAction.js.map"]},"metadata":{},"sourceType":"module"} |