@mui/internal-docs-infra
Version:
MUI Infra - internal documentation creation tools.
33 lines (31 loc) • 1.13 kB
JavaScript
'use client';
import * as React from 'react';
import { PreloadContext } from "./PreloadContext.mjs";
/**
* Scopes a cross-instance preload cache (typically at a layout). Descendants
* call `usePreload` to start dynamic imports of shared helpers keyed by a
* stable string; the first call per key runs the factory and every other
* instance reuses its promise - so a helper a chunk's data implies (a transform
* fn, say) is fetched once, in parallel with the content, across the page.
*/
import { jsx as _jsx } from "react/jsx-runtime";
export function PreloadProvider({
children
}) {
// One cache per provider instance, stable across renders.
const [cache] = React.useState(() => new Map());
const preload = React.useCallback((key, factory) => {
const existing = cache.get(key);
if (existing) {
// Same key -> same factory type by contract; the consumer owns the keying.
return existing;
}
const promise = factory();
cache.set(key, promise);
return promise;
}, [cache]);
return /*#__PURE__*/_jsx(PreloadContext.Provider, {
value: preload,
children: children
});
}