UNPKG

2.66 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.CHILD_STATE = void 0;
7exports.default = useRouteCache;
8
9var React = _interopRequireWildcard(require("react"));
10
11function _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); }
12
13function _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; }
14
15/**
16 * Utilites such as `getFocusedRouteNameFromRoute` need to access state.
17 * So we need a way to suppress the warning for those use cases.
18 * This is fine since they are internal utilities and this is not public API.
19 */
20const CHILD_STATE = Symbol('CHILD_STATE');
21/**
22 * Hook to cache route props for each screen in the navigator.
23 * This lets add warnings and modifications to the route object but keep references between renders.
24 */
25
26exports.CHILD_STATE = CHILD_STATE;
27
28function useRouteCache(routes) {
29 // Cache object which holds route objects for each screen
30 const cache = React.useMemo(() => ({
31 current: new Map()
32 }), []);
33
34 if (process.env.NODE_ENV === 'production') {
35 // We don't want the overhead of creating extra maps every render in prod
36 return routes;
37 }
38
39 cache.current = routes.reduce((acc, route) => {
40 const previous = cache.current.get(route);
41
42 if (previous) {
43 // If a cached route object already exists, reuse it
44 acc.set(route, previous);
45 } else {
46 const {
47 state,
48 ...proxy
49 } = route;
50 Object.defineProperty(proxy, CHILD_STATE, {
51 enumerable: false,
52 value: state
53 });
54 acc.set(route, proxy);
55 }
56
57 return acc;
58 }, new Map());
59 return Array.from(cache.current.values());
60}
61//# sourceMappingURL=useRouteCache.js.map
\No newline at end of file