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
12 KiB
1 line
12 KiB
{"ast":null,"code":"import _objectWithoutProperties from \"@babel/runtime/helpers/objectWithoutProperties\";\nimport _defineProperty from \"@babel/runtime/helpers/defineProperty\";\nvar _excluded = [\"emit\"];\nfunction ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }\nfunction _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }\nimport { CommonActions } from '@react-navigation/routers';\nimport * as React from 'react';\nimport NavigationBuilderContext from \"./NavigationBuilderContext\";\nexport default function useNavigationCache(_ref) {\n var state = _ref.state,\n getState = _ref.getState,\n navigation = _ref.navigation,\n _setOptions = _ref.setOptions,\n router = _ref.router,\n emitter = _ref.emitter;\n var _React$useContext = React.useContext(NavigationBuilderContext),\n stackRef = _React$useContext.stackRef;\n var cache = React.useMemo(function () {\n return {\n current: {}\n };\n }, [getState, navigation, _setOptions, router, emitter]);\n var actions = _objectSpread(_objectSpread({}, router.actionCreators), CommonActions);\n cache.current = state.routes.reduce(function (acc, route) {\n var previous = cache.current[route.key];\n if (previous) {\n acc[route.key] = previous;\n } else {\n var emit = navigation.emit,\n rest = _objectWithoutProperties(navigation, _excluded);\n var _dispatch = function dispatch(thunk) {\n var action = typeof thunk === 'function' ? thunk(getState()) : thunk;\n if (action != null) {\n navigation.dispatch(_objectSpread({\n source: route.key\n }, action));\n }\n };\n var withStack = function withStack(callback) {\n var isStackSet = false;\n try {\n if (process.env.NODE_ENV !== 'production' && stackRef && !stackRef.current) {\n stackRef.current = new Error().stack;\n isStackSet = true;\n }\n callback();\n } finally {\n if (isStackSet && stackRef) {\n stackRef.current = undefined;\n }\n }\n };\n var helpers = Object.keys(actions).reduce(function (acc, name) {\n acc[name] = function () {\n for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {\n args[_key] = arguments[_key];\n }\n return withStack(function () {\n return _dispatch(actions[name].apply(actions, args));\n });\n };\n return acc;\n }, {});\n acc[route.key] = _objectSpread(_objectSpread(_objectSpread(_objectSpread({}, rest), helpers), emitter.create(route.key)), {}, {\n dispatch: function dispatch(thunk) {\n return withStack(function () {\n return _dispatch(thunk);\n });\n },\n getParent: function getParent(id) {\n if (id !== undefined && id === rest.getId()) {\n return acc[route.key];\n }\n return rest.getParent(id);\n },\n setOptions: function setOptions(options) {\n return _setOptions(function (o) {\n return _objectSpread(_objectSpread({}, o), {}, _defineProperty({}, route.key, _objectSpread(_objectSpread({}, o[route.key]), options)));\n });\n },\n isFocused: function isFocused() {\n var state = getState();\n if (state.routes[state.index].key !== route.key) {\n return false;\n }\n return navigation ? navigation.isFocused() : true;\n }\n });\n }\n return acc;\n }, {});\n return cache.current;\n}","map":{"version":3,"sources":["useNavigationCache.tsx"],"names":["CommonActions","React","NavigationBuilderContext","useNavigationCache","state","getState","navigation","setOptions","router","emitter","stackRef","useContext","cache","useMemo","current","actions","actionCreators","routes","reduce","acc","route","previous","key","emit","rest","dispatch","thunk","action","source","withStack","callback","isStackSet","process","env","NODE_ENV","Error","stack","undefined","helpers","Object","keys","name","args","create","getParent","id","getId","options","o","isFocused","index"],"mappings":";;;;;AAAA,SACEA,aAAa,QAKR,2BAA2B;AAClC,OAAO,KAAKC,KAAK,MAAM,OAAO;AAE9B,OAAOC,wBAAwB;AAwC/B,eAAe,SAASC,kBAAkB,CAAA,IAAA,EAWb;EAP3B,IACAC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AKG,SAAS,CAAC;YAAA,OAAMJ,SAAQ,CAACC,KAAK,CAAC;UAAA,EAAC;QAAA;QAC5DkB,SAAS,EAAGC,mBAAAA,EAAW,EAAK;UAC1B,IAAIA,EAAE,KAAKR,SAAS,IAAIQ,EAAE,KAAKrB,IAAI,CAACsB,KAAK,EAAE,EAAE;YAG3C,OAAO3B,GAAG,CAACC,KAAK,CAACE,GAAG,CAAC;UACvB;UAEA,OAAOE,IAAI,CAACoB,SAAS,CAACC,EAAE,CAAC;QAC3B,CAAC;QACDtC,UAAU,EAAGwC,oBAAAA,OAAe;UAAA,OAC1BxC,WAAU,CAAEyC,UAAAA,CAAC;YAAA,uCACRA,CAAC,2BACH5B,KAAK,CAACE,GAAG,kCAAQ0B,CAAC,CAAC5B,KAAK,CAACE,GAAG,CAAC,GAAKyB,OAAAA;UAAAA,CACnC,CAAC;QAAA;QACLE,SAAS,EAAE,qBAAM;UACf,IAAM7C,KAAK,GAAGC,QAAQ,EAAE;UAExB,IAAID,KAAK,CAACa,MAAM,CAACb,KAAK,CAAC8C,KAAK,CAAC,CAAC5B,GAAG,KAAKF,KAAK,CAACE,GAAG,EAAE;YAC/C,OAAO,KAAK;UACd;UAIA,OAAOhB,UAAU,GAAGA,UAAU,CAAC2C,SAAS,EAAE,GAAG,IAAI;QACnD;MAAA,EACD;IACH;IAEA,OAAO9B,GAAG;EACZ,CAAC,EAAE,CAAC,CAAC,CAAC;EAEN,OAAOP,KAAK,CAACE,OAAO;AACtB","sourceRoot":"../../src","sourcesContent":["import { CommonActions } from '@react-navigation/routers';\nimport * as React from 'react';\nimport NavigationBuilderContext from './NavigationBuilderContext';\n/**\n * Hook to cache navigation objects for each screen in the navigator.\n * It's important to cache them to make sure navigation objects don't change between renders.\n * This lets us apply optimizations like `React.memo` to minimize re-rendering screens.\n */\nexport default function useNavigationCache(_ref) {\n let {\n state,\n getState,\n navigation,\n setOptions,\n router,\n emitter\n } = _ref;\n const {\n stackRef\n } = React.useContext(NavigationBuilderContext);\n\n // Cache object which holds navigation objects for each screen\n // We use `React.useMemo` instead of `React.useRef` coz we want to invalidate it when deps change\n // In reality, these deps will rarely change, if ever\n const cache = React.useMemo(() => ({\n current: {}\n }),\n // eslint-disable-next-line react-hooks/exhaustive-deps\n [getState, navigation, setOptions, router, emitter]);\n const actions = {\n ...router.actionCreators,\n ...CommonActions\n };\n cache.current = state.routes.reduce((acc, route) => {\n const previous = cache.current[route.key];\n if (previous) {\n // If a cached navigation object already exists, reuse it\n acc[route.key] = previous;\n } else {\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n const {\n emit,\n ...rest\n } = navigation;\n const dispatch = thunk => {\n const action = typeof thunk === 'function' ? thunk(getState()) : thunk;\n if (action != null) {\n navigation.dispatch({\n source: route.key,\n ...action\n });\n }\n };\n const withStack = callback => {\n let isStackSet = false;\n try {\n if (process.env.NODE_ENV !== 'production' && stackRef && !stackRef.current) {\n // Capture the stack trace for devtools\n stackRef.current = new Error().stack;\n isStackSet = true;\n }\n callback();\n } finally {\n if (isStackSet && stackRef) {\n stackRef.current = undefined;\n }\n }\n };\n const helpers = Object.keys(actions).reduce((acc, name) => {\n acc[name] = function () {\n for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {\n args[_key] = arguments[_key];\n }\n return withStack(() =>\n // @ts-expect-error: name is a valid key, but TypeScript is dumb\n dispatch(actions[name](...args)));\n };\n return acc;\n }, {});\n acc[route.key] = {\n ...rest,\n ...helpers,\n // FIXME: too much work to fix the types for now\n ...emitter.create(route.key),\n dispatch: thunk => withStack(() => dispatch(thunk)),\n getParent: id => {\n if (id !== undefined && id === rest.getId()) {\n // If the passed id is the same as the current navigation id,\n // we return the cached navigation object for the relevant route\n return acc[route.key];\n }\n return rest.getParent(id);\n },\n setOptions: options => setOptions(o => ({\n ...o,\n [route.key]: {\n ...o[route.key],\n ...options\n }\n })),\n isFocused: () => {\n const state = getState();\n if (state.routes[state.index].key !== route.key) {\n return false;\n }\n\n // If the current screen is focused, we also need to check if parent navigator is focused\n // This makes sure that we return the focus state in the whole tree, not just this navigator\n return navigation ? navigation.isFocused() : true;\n }\n };\n }\n return acc;\n }, {});\n return cache.current;\n}\n//# sourceMappingURL=useNavigationCache.js.map"]},"metadata":{},"sourceType":"module"} |