UNPKG

one

Version:

One is a new React Framework that makes Vite serve both native and web.

48 lines (47 loc) 1.58 kB
import { useStateForPath } from "@react-navigation/core"; import { createContext, useRef } from "react"; import { getRouteInfo } from "./getRouteInfo.mjs"; import { jsx } from "react/jsx-runtime"; const RouteInfoContext = createContext(void 0); function RouteInfoContextProvider({ children }) { const currentState = useStateForPath(); const currentStateRef = useRef(void 0); const lastStateRef = useRef(void 0); const cachedRouteInfoValueRef = useRef(void 0); const lazilyCalculatedRouteInfo = useRef(void 0); if (currentState && currentStateRef.current !== currentState) { lazilyCalculatedRouteInfo.current = makeLazilyCalculatedRouteInfo({ currentStateRef, lastStateRef, cachedRouteInfoValueRef }); } currentStateRef.current = currentState; return /* @__PURE__ */jsx(RouteInfoContext.Provider, { value: currentState ? lazilyCalculatedRouteInfo.current : void 0, children }); } function makeLazilyCalculatedRouteInfo({ currentStateRef, lastStateRef, cachedRouteInfoValueRef }) { return new Proxy({}, { get(_, p) { const state = currentStateRef.current; if (!state) { throw new Error("[lazilyCalculatedRouteInfo] cannot get state"); } if (!cachedRouteInfoValueRef.current || lastStateRef.current !== state) { cachedRouteInfoValueRef.current = getRouteInfo(state); lastStateRef.current = state; } return cachedRouteInfoValueRef.current[p]; } }); } export { RouteInfoContext, RouteInfoContextProvider }; //# sourceMappingURL=RouteInfoContext.mjs.map