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
34 KiB
1 line
34 KiB
{"ast":null,"code":"import _slicedToArray from \"@babel/runtime/helpers/slicedToArray\";\nimport _asyncToGenerator from \"@babel/runtime/helpers/asyncToGenerator\";\nimport { findFocusedRoute, getActionFromState as getActionFromStateDefault, getPathFromState as getPathFromStateDefault, getStateFromPath as getStateFromPathDefault } from '@react-navigation/core';\nimport isEqual from 'fast-deep-equal';\nimport * as React from 'react';\nimport createMemoryHistory from \"./createMemoryHistory\";\nimport ServerContext from \"./ServerContext\";\nvar findMatchingState = function findMatchingState(a, b) {\n if (a === undefined || b === undefined || a.key !== b.key) {\n return [undefined, undefined];\n }\n var aHistoryLength = a.history ? a.history.length : a.routes.length;\n var bHistoryLength = b.history ? b.history.length : b.routes.length;\n var aRoute = a.routes[a.index];\n var bRoute = b.routes[b.index];\n var aChildState = aRoute.state;\n var bChildState = bRoute.state;\n if (aHistoryLength !== bHistoryLength || aRoute.key !== bRoute.key || aChildState === undefined || bChildState === undefined || aChildState.key !== bChildState.key) {\n return [a, b];\n }\n return findMatchingState(aChildState, bChildState);\n};\nvar series = function series(cb) {\n var handling = false;\n var queue = [];\n var callback = function () {\n var _ref2 = _asyncToGenerator(function* () {\n try {\n if (handling) {\n queue.unshift(callback);\n return;\n }\n handling = true;\n yield cb();\n } finally {\n handling = false;\n if (queue.length) {\n var last = queue.pop();\n last === null || last === void 0 ? void 0 : last();\n }\n }\n });\n return function callback() {\n return _ref2.apply(this, arguments);\n };\n }();\n return callback;\n};\nvar linkingHandlers = [];\nexport default function useLinking(ref, _ref) {\n var independent = _ref.independent,\n _ref$enabled = _ref.enabled,\n enabled = _ref$enabled === void 0 ? true : _ref$enabled,\n config = _ref.config,\n _ref$getStateFromPath = _ref.getStateFromPath,\n getStateFromPath = _ref$getStateFromPath === void 0 ? getStateFromPathDefault : _ref$getStateFromPath,\n _ref$getPathFromState = _ref.getPathFromState,\n getPathFromState = _ref$getPathFromState === void 0 ? getPathFromStateDefault : _ref$getPathFromState,\n _ref$getActionFromSta = _ref.getActionFromState,\n getActionFromState = _ref$getActionFromSta === void 0 ? getActionFromStateDefault : _ref$getActionFromSta;\n React.useEffect(function () {\n if (process.env.NODE_ENV === 'production') {\n return undefined;\n }\n if (independent) {\n return undefined;\n }\n if (enabled !== false && linkingHandlers.length) {\n console.error(['Looks like you have configured linking in multiple places. This is likely an error since deep links should only be handled in one place to avoid conflicts. Make sure that:', \"- You don't have multiple NavigationContainers in the app each with 'linking' enabled\", '- Only a single instance of the root component is rendered'].join('\\n').trim());\n }\n var handler = Symbol();\n if (enabled !== false) {\n linkingHandlers.push(handler);\n }\n return function () {\n var index = linkingHandlers.indexOf(handler);\n if (index > -1) {\n linkingHandlers.splice(index, 1);\n }\n };\n }, [enabled, independent]);\n var _React$useState = React.useState(createMemoryHistory),\n _React$useState2 = _slicedToArray(_React$useState, 1),\n history = _React$useState2[0];\n var enabledRef = React.useRef(enabled);\n var configRef = React.useRef(config);\n var getStateFromPathRef = React.useRef(getStateFromPath);\n var getPathFromStateRef = React.useRef(getPathFromState);\n var getActionFromStateRef = React.useRef(getActionFromState);\n React.useEffect(function () {\n enabledRef.current = enabled;\n configRef.current = config;\n getStateFromPathRef.current = getStateFromPath;\n getPathFromStateRef.current = getPathFromState;\n getActionFromStateRef.current = getActionFromState;\n });\n var server = React.useContext(ServerContext);\n var getInitialState = React.useCallback(function () {\n var value;\n if (enabledRef.current) {\n var _ref3;\n var _location = (_ref3 = server === null || server === void 0 ? void 0 : server.location) != null ? _ref3 : typeof window !== 'undefined' ? window.location : undefined;\n var path = _location ? _location.pathname + _location.search : undefined;\n if (path) {\n value = getStateFromPathRef.current(path, configRef.current);\n }\n }\n var thenable = {\n then: function then(onfulfilled) {\n return Promise.resolve(onfulfilled ? onfulfilled(value) : value);\n },\n catch: function _catch() {\n return thenable;\n }\n };\n return thenable;\n }, []);\n var previousIndexRef = React.useRef(undefined);\n var previousStateRef = React.useRef(undefined);\n var pendingPopStatePathRef = React.useRef(undefined);\n React.useEffect(function () {\n previousIndexRef.current = history.index;\n return history.listen(function () {\n var _previousIndexRef$cur;\n var navigation = ref.current;\n if (!navigation || !enabled) {\n return;\n }\n var path = location.pathname + location.search;\n var index = history.index;\n var previousIndex = (_previousIndexRef$cur = previousIndexRef.current) != null ? _previousIndexRef$cur : 0;\n previousIndexRef.current = index;\n pendingPopStatePathRef.current = path;\n var record = history.get(index);\n if ((record === null || record === void 0 ? void 0 : record.path) === path && record !== null && record !== void 0 && record.state) {\n navigation.resetRoot(record.state);\n return;\n }\n var state = getStateFromPathRef.current(path, configRef.current);\n if (state) {\n var rootState = navigation.getRootState();\n if (state.routes.some(function (r) {\n return !(rootState !== null && rootState !== void 0 && rootState.routeNames.includes(r.name));\n })) {\n console.warn(\"The navigation state parsed from the URL contains routes not present in the root navigator. This usually means that the linking configuration doesn't match the navigation structure. See https://reactnavigation.org/docs/configuring-links for more details on how to specify a linking configuration.\");\n return;\n }\n if (index > previousIndex) {\n var action = getActionFromStateRef.current(state, configRef.current);\n if (action !== undefined) {\n try {\n navigation.dispatch(action);\n } catch (e) {\n console.warn(\"An error occurred when trying to handle the link '\" + path + \"': \" + (typeof e === 'object' && e != null && 'message' in e ? e.message : e));\n }\n } else {\n navigation.resetRoot(state);\n }\n } else {\n navigation.resetRoot(state);\n }\n } else {\n navigation.resetRoot(state);\n }\n });\n }, [enabled, history, ref]);\n React.useEffect(function () {\n var _ref$current;\n if (!enabled) {\n return;\n }\n var getPathForRoute = function getPathForRoute(route, state) {\n if (route !== null && route !== void 0 && route.path) {\n var stateForPath = getStateFromPathRef.current(route.path, configRef.current);\n if (stateForPath) {\n var focusedRoute = findFocusedRoute(stateForPath);\n if (focusedRoute && focusedRoute.name === route.name && isEqual(focusedRoute.params, route.params)) {\n return route.path;\n }\n }\n }\n return getPathFromStateRef.current(state, configRef.current);\n };\n if (ref.current) {\n var state = ref.current.getRootState();\n if (state) {\n var route = findFocusedRoute(state);\n var path = getPathForRoute(route, state);\n if (previousStateRef.current === undefined) {\n previousStateRef.current = state;\n }\n history.replace({\n path: path,\n state: state\n });\n }\n }\n var onStateChange = function () {\n var _ref4 = _asyncToGenerator(function* () {\n var navigation = ref.current;\n if (!navigation || !enabled) {\n return;\n }\n var previousState = previousStateRef.current;\n var state = navigation.getRootState();\n if (!state) {\n return;\n }\n var pendingPath = pendingPopStatePathRef.current;\n var route = findFocusedRoute(state);\n var path = getPathForRoute(route, state);\n previousStateRef.current = state;\n pendingPopStatePathRef.current = undefined;\n var _findMatchingState = findMatchingState(previousState, state),\n _findMatchingState2 = _slicedToArray(_findMatchingState, 2),\n previousFocusedState = _findMatchingState2[0],\n focusedState = _findMatchingState2[1];\n if (previousFocusedState && focusedState && path !== pendingPath) {\n var historyDelta = (focusedState.history ? focusedState.history.length : focusedState.routes.length) - (previousFocusedState.history ? previousFocusedState.history.length : previousFocusedState.routes.length);\n if (historyDelta > 0) {\n history.push({\n path: path,\n state: state\n });\n } else if (historyDelta < 0) {\n var nextIndex = history.backIndex({\n path: path\n });\n var currentIndex = history.index;\n try {\n if (nextIndex !== -1 && nextIndex < currentIndex) {\n yield history.go(nextIndex - currentIndex);\n } else {\n yield history.go(historyDelta);\n }\n history.replace({\n path: path,\n state: state\n });\n } catch (e) {}\n } else {\n history.replace({\n path: path,\n state: state\n });\n }\n } else {\n history.replace({\n path: path,\n state: state\n });\n }\n });\n return function onStateChange() {\n return _ref4.apply(this, arguments);\n };\n }();\n return (_ref$current = ref.current) === null || _ref$current === void 0 ? void 0 : _ref$current.addListener('state', series(onStateChange));\n }, [enabled, history, ref]);\n return {\n getInitialState: getInitialState\n };\n}","map":{"version":3,"sources":["useLinking.tsx"],"names":["findFocusedRoute","getActionFromState","getActionFromStateDefault","getPathFromState","getPathFromStateDefault","getStateFromPath","getStateFromPathDefault","isEqual","React","createMemoryHistory","ServerContext","findMatchingState","a","b","undefined","key","aHistoryLength","history","length","routes","bHistoryLength","aRoute","index","bRoute","aChildState","state","bChildState","series","cb","handling","queue","callback","unshift","last","pop","linkingHandlers","useLinking","ref","independent","enabled","config","useEffect","process","env","NODE_ENV","console","error","join","trim","handler","Symbol","push","indexOf","splice","useState","enabledRef","useRef","configRef","getStateFromPathRef","getPathFromStateRef","getActionFromStateRef","current","server","useContext","getInitialState","useCallback","value","location","window","path","pathname","search","thenable","then","onfulfilled","Promise","resolve","catch","previousIndexRef","previousStateRef","pendingPopStatePathRef","listen","navigation","previousIndex","record","get","resetRoot","rootState","getRootState","some","r","routeNames","includes","name","warn","action","dispatch","e","message","getPathForRoute","route","stateForPath","focusedRoute","params","replace","onStateChange","previousState","pendingPath","previousFocusedState","focusedState","historyDelta","nextIndex","backIndex","currentIndex","go","addListener"],"mappings":";;AAAA,SACEA,gBAAgB,EAChBC,kBAAkB,IAAIC,yBAAyB,EAC/CC,gBAAgB,IAAIC,uBAAuB,EAC3CC,gBAAgB,IAAIC,uBAAuB,QAItC,wBAAwB;AAC/B,OAAOC,OAAO,MAAM,iBAAiB;AACrC,OAAO,KAAKC,KAAK,MAAM,OAAO;AAE9B,OAAOC,mBAAmB;AAC1B,OAAOC,aAAa;AASpB,IAAMC,iBAAiB,GAAG,SAApBA,iBAAiB,CACrBC,CAAgB,EAChBC,CAAgB,EACmB;EACnC,IAAID,CAAC,KAAKE,SAAS,IAAID,CAAC,KAAKC,SAAS,IAAIF,CAAC,CAACG,GAAG,KAAKF,CAAC,CAACE,GAAG,EAAE;IACzD,OAAO,CAACD,SAAS,EAAEA,SAAS,CAAC;EAC/B;EAGA,IAAME,cAAc,GAAGJ,CAAC,CAACK,OAAO,GAAGL,CAAC,CAACK,OAAO,CAACC,MAAM,GAAGN,CAAC,CAACO,MAAM,CAACD,MAAM;EACrE,IAAME,cAAc,GAAGP,CAAC,CAACI,OAAO,GAAGJ,CAAC,CAACI,OAAO,CAACC,MAAM,GAAGL,CAAC,CAACM,MAAM,CAACD,MAAM;EAErE,IAAMG,MAAM,GAAGT,CAAC,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oute, getActionFromState as getActionFromStateDefault, getPathFromState as getPathFromStateDefault, getStateFromPath as getStateFromPathDefault } from '@react-navigation/core';\nimport isEqual from 'fast-deep-equal';\nimport * as React from 'react';\nimport createMemoryHistory from './createMemoryHistory';\nimport ServerContext from './ServerContext';\n/**\n * Find the matching navigation state that changed between 2 navigation states\n * e.g.: a -> b -> c -> d and a -> b -> c -> e -> f, if history in b changed, b is the matching state\n */\nconst findMatchingState = (a, b) => {\n if (a === undefined || b === undefined || a.key !== b.key) {\n return [undefined, undefined];\n }\n\n // Tab and drawer will have `history` property, but stack will have history in `routes`\n const aHistoryLength = a.history ? a.history.length : a.routes.length;\n const bHistoryLength = b.history ? b.history.length : b.routes.length;\n const aRoute = a.routes[a.index];\n const bRoute = b.routes[b.index];\n const aChildState = aRoute.state;\n const bChildState = bRoute.state;\n\n // Stop here if this is the state object that changed:\n // - history length is different\n // - focused routes are different\n // - one of them doesn't have child state\n // - child state keys are different\n if (aHistoryLength !== bHistoryLength || aRoute.key !== bRoute.key || aChildState === undefined || bChildState === undefined || aChildState.key !== bChildState.key) {\n return [a, b];\n }\n return findMatchingState(aChildState, bChildState);\n};\n\n/**\n * Run async function in series as it's called.\n */\nconst series = cb => {\n // Whether we're currently handling a callback\n let handling = false;\n let queue = [];\n const callback = async () => {\n try {\n if (handling) {\n // If we're currently handling a previous event, wait before handling this one\n // Add the callback to the beginning of the queue\n queue.unshift(callback);\n return;\n }\n handling = true;\n await cb();\n } finally {\n handling = false;\n if (queue.length) {\n // If we have queued items, handle the last one\n const last = queue.pop();\n last === null || last === void 0 ? void 0 : last();\n }\n }\n };\n return callback;\n};\nlet linkingHandlers = [];\nexport default function useLinking(ref, _ref) {\n let {\n independent,\n enabled = true,\n config,\n getStateFromPath = getStateFromPathDefault,\n getPathFromState = getPathFromStateDefault,\n getActionFromState = getActionFromStateDefault\n } = _ref;\n React.useEffect(() => {\n if (process.env.NODE_ENV === 'production') {\n return undefined;\n }\n if (independent) {\n return undefined;\n }\n if (enabled !== false && linkingHandlers.length) {\n console.error(['Looks like you have configured linking in multiple places. This is likely an error since deep links should only be handled in one place to avoid conflicts. Make sure that:', \"- You don't have multiple NavigationContainers in the app each with 'linking' enabled\", '- Only a single instance of the root component is rendered'].join('\\n').trim());\n }\n const handler = Symbol();\n if (enabled !== false) {\n linkingHandlers.push(handler);\n }\n return () => {\n const index = linkingHandlers.indexOf(handler);\n if (index > -1) {\n linkingHandlers.splice(index, 1);\n }\n };\n }, [enabled, independent]);\n const [history] = React.useState(createMemoryHistory);\n\n // We store these options in ref to avoid re-creating getInitialState and re-subscribing listeners\n // This lets user avoid wrapping the items in `React.useCallback` or `React.useMemo`\n // Not re-creating `getInitialState` is important coz it makes it easier for the user to use in an effect\n const enabledRef = React.useRef(enabled);\n const configRef = React.useRef(config);\n const getStateFromPathRef = React.useRef(getStateFromPath);\n const getPathFromStateRef = React.useRef(getPathFromState);\n const getActionFromStateRef = React.useRef(getActionFromState);\n React.useEffect(() => {\n enabledRef.current = enabled;\n configRef.current = config;\n getStateFromPathRef.current = getStateFromPath;\n getPathFromStateRef.current = getPathFromState;\n getActionFromStateRef.current = getActionFromState;\n });\n const server = React.useContext(ServerContext);\n const getInitialState = React.useCallback(() => {\n let value;\n if (enabledRef.current) {\n const location = (server === null || server === void 0 ? void 0 : server.location) ?? (typeof window !== 'undefined' ? window.location : undefined);\n const path = location ? location.pathname + location.search : undefined;\n if (path) {\n value = getStateFromPathRef.current(path, configRef.current);\n }\n }\n const thenable = {\n then(onfulfilled) {\n return Promise.resolve(onfulfilled ? onfulfilled(value) : value);\n },\n catch() {\n return thenable;\n }\n };\n return thenable;\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, []);\n const previousIndexRef = React.useRef(undefined);\n const previousStateRef = React.useRef(undefined);\n const pendingPopStatePathRef = React.useRef(undefined);\n React.useEffect(() => {\n previousIndexRef.current = history.index;\n return history.listen(() => {\n const navigation = ref.current;\n if (!navigation || !enabled) {\n return;\n }\n const path = location.pathname + location.search;\n const index = history.index;\n const previousIndex = previousIndexRef.current ?? 0;\n previousIndexRef.current = index;\n pendingPopStatePathRef.current = path;\n\n // When browser back/forward is clicked, we first need to check if state object for this index exists\n // If it does we'll reset to that state object\n // Otherwise, we'll handle it like a regular deep link\n const record = history.get(index);\n if ((record === null || record === void 0 ? void 0 : record.path) === path && record !== null && record !== void 0 && record.state) {\n navigation.resetRoot(record.state);\n return;\n }\n const state = getStateFromPathRef.current(path, configRef.current);\n\n // We should only dispatch an action when going forward\n // Otherwise the action will likely add items to history, which would mess things up\n if (state) {\n // Make sure that the routes in the state exist in the root navigator\n // Otherwise there's an error in the linking configuration\n const rootState = navigation.getRootState();\n if (state.routes.some(r => !(rootState !== null && rootState !== void 0 && rootState.routeNames.includes(r.name)))) {\n console.warn(\"The navigation state parsed from the URL contains routes not present in the root navigator. This usually means that the linking configuration doesn't match the navigation structure. See https://reactnavigation.org/docs/configuring-links for more details on how to specify a linking configuration.\");\n return;\n }\n if (index > previousIndex) {\n const action = getActionFromStateRef.current(state, configRef.current);\n if (action !== undefined) {\n try {\n navigation.dispatch(action);\n } catch (e) {\n // Ignore any errors from deep linking.\n // This could happen in case of malformed links, navigation object not being initialized etc.\n console.warn(`An error occurred when trying to handle the link '${path}': ${typeof e === 'object' && e != null && 'message' in e ?\n // @ts-expect-error: we're already checking for this\n e.message : e}`);\n }\n } else {\n navigation.resetRoot(state);\n }\n } else {\n navigation.resetRoot(state);\n }\n } else {\n // if current path didn't return any state, we should revert to initial state\n navigation.resetRoot(state);\n }\n });\n }, [enabled, history, ref]);\n React.useEffect(() => {\n var _ref$current;\n if (!enabled) {\n return;\n }\n const getPathForRoute = (route, state) => {\n // If the `route` object contains a `path`, use that path as long as `route.name` and `params` still match\n // This makes sure that we preserve the original URL for wildcard routes\n if (route !== null && route !== void 0 && route.path) {\n const stateForPath = getStateFromPathRef.current(route.path, configRef.current);\n if (stateForPath) {\n const focusedRoute = findFocusedRoute(stateForPath);\n if (focusedRoute && focusedRoute.name === route.name && isEqual(focusedRoute.params, route.params)) {\n return route.path;\n }\n }\n }\n return getPathFromStateRef.current(state, configRef.current);\n };\n if (ref.current) {\n // We need to record the current metadata on the first render if they aren't set\n // This will allow the initial state to be in the history entry\n const state = ref.current.getRootState();\n if (state) {\n const route = findFocusedRoute(state);\n const path = getPathForRoute(route, state);\n if (previousStateRef.current === undefined) {\n previousStateRef.current = state;\n }\n history.replace({\n path,\n state\n });\n }\n }\n const onStateChange = async () => {\n const navigation = ref.current;\n if (!navigation || !enabled) {\n return;\n }\n const previousState = previousStateRef.current;\n const state = navigation.getRootState();\n\n // root state may not available, for example when root navigators switch inside the container\n if (!state) {\n return;\n }\n const pendingPath = pendingPopStatePathRef.current;\n const route = findFocusedRoute(state);\n const path = getPathForRoute(route, state);\n previousStateRef.current = state;\n pendingPopStatePathRef.current = undefined;\n\n // To detect the kind of state change, we need to:\n // - Find the common focused navigation state in previous and current state\n // - If only the route keys changed, compare history/routes.length to check if we go back/forward/replace\n // - If no common focused navigation state found, it's a replace\n const [previousFocusedState, focusedState] = findMatchingState(previousState, state);\n if (previousFocusedState && focusedState &&\n // We should only handle push/pop if path changed from what was in last `popstate`\n // Otherwise it's likely a change triggered by `popstate`\n path !== pendingPath) {\n const historyDelta = (focusedState.history ? focusedState.history.length : focusedState.routes.length) - (previousFocusedState.history ? previousFocusedState.history.length : previousFocusedState.routes.length);\n if (historyDelta > 0) {\n // If history length is increased, we should pushState\n // Note that path might not actually change here, for example, drawer open should pushState\n history.push({\n path,\n state\n });\n } else if (historyDelta < 0) {\n // If history length is decreased, i.e. entries were removed, we want to go back\n\n const nextIndex = history.backIndex({\n path\n });\n const currentIndex = history.index;\n try {\n if (nextIndex !== -1 && nextIndex < currentIndex) {\n // An existing entry for this path exists and it's less than current index, go back to that\n await history.go(nextIndex - currentIndex);\n } else {\n // We couldn't find an existing entry to go back to, so we'll go back by the delta\n // This won't be correct if multiple routes were pushed in one go before\n // Usually this shouldn't happen and this is a fallback for that\n await history.go(historyDelta);\n }\n\n // Store the updated state as well as fix the path if incorrect\n history.replace({\n path,\n state\n });\n } catch (e) {\n // The navigation was interrupted\n }\n } else {\n // If history length is unchanged, we want to replaceState\n history.replace({\n path,\n state\n });\n }\n } else {\n // If no common navigation state was found, assume it's a replace\n // This would happen if the user did a reset/conditionally changed navigators\n history.replace({\n path,\n state\n });\n }\n };\n\n // We debounce onStateChange coz we don't want multiple state changes to be handled at one time\n // This could happen since `history.go(n)` is asynchronous\n // If `pushState` or `replaceState` were called before `history.go(n)` completes, it'll mess stuff up\n return (_ref$current = ref.current) === null || _ref$current === void 0 ? void 0 : _ref$current.addListener('state', series(onStateChange));\n }, [enabled, history, ref]);\n return {\n getInitialState\n };\n}\n//# sourceMappingURL=useLinking.js.map"]},"metadata":{},"sourceType":"module"} |