2025-12-07 19:32:53 +02:00
|
|
|
import React, { type JSX, type ReactNode } from 'react';
|
|
|
|
|
import { RuntimeAPIContext } from '@/contexts/runtimeAPIContext';
|
2026-07-30 09:02:14 +00:00
|
|
|
import type { FilesAPI, RuntimeAPIs } from '@/lib/api/types';
|
2026-07-21 20:52:20 +03:00
|
|
|
import { createContentCachedFiles } from '@/contexts/content-cache-owner';
|
2025-12-07 19:32:53 +02:00
|
|
|
|
2026-07-30 09:02:14 +00:00
|
|
|
type ContentCachedFiles = ReturnType<typeof createContentCachedFiles>;
|
|
|
|
|
|
2025-12-07 19:32:53 +02:00
|
|
|
export function RuntimeAPIProvider({ apis, children }: { apis: RuntimeAPIs; children: ReactNode }): JSX.Element {
|
2026-07-30 09:02:14 +00:00
|
|
|
// Effect-owned lifecycle: React Strict Mode dispose+remount must create a fresh
|
|
|
|
|
// owner. useMemo + dispose reused a dead owner and broke text-file opens
|
|
|
|
|
// (binaries skipped the pre-read, so they still appeared to work).
|
|
|
|
|
const [cachedOwner, setCachedOwner] = React.useState<ContentCachedFiles | null>(null);
|
|
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
|
const owner = createContentCachedFiles(apis.files);
|
|
|
|
|
setCachedOwner(owner);
|
|
|
|
|
return () => {
|
|
|
|
|
owner.dispose();
|
|
|
|
|
setCachedOwner((current) => (current === owner ? null : current));
|
|
|
|
|
};
|
|
|
|
|
}, [apis.files]);
|
|
|
|
|
|
|
|
|
|
const files: FilesAPI = cachedOwner?.files ?? apis.files;
|
2026-03-31 18:47:00 +03:00
|
|
|
const cachedApis = React.useMemo<RuntimeAPIs>(
|
|
|
|
|
() => ({
|
|
|
|
|
...apis,
|
2026-07-30 09:02:14 +00:00
|
|
|
files,
|
2026-03-31 18:47:00 +03:00
|
|
|
}),
|
2026-07-30 09:02:14 +00:00
|
|
|
[apis, files],
|
2026-03-31 18:47:00 +03:00
|
|
|
);
|
|
|
|
|
return <RuntimeAPIContext.Provider value={cachedApis}>{children}</RuntimeAPIContext.Provider>;
|
2025-12-07 19:32:53 +02:00
|
|
|
}
|