Line data Source code
1 : import * as React from 'react';
2 :
3 : import type { QueryResultCache } from '../query/query_result_cache.js';
4 : import { createOPFSQueryResultCache } from './opfs_query_result_cache.js';
5 :
6 1 : const CACHE_CTX = React.createContext<QueryResultCache | null>(null);
7 :
8 : /// The query result cache of the app, null without a provider
9 1 : export function useQueryResultCache(): QueryResultCache | null {
10 1 : return React.useContext(CACHE_CTX);
11 1 : }
12 :
13 : /// Provide the query result cache of the app, by default the one in the origin private file system
14 1 : export function QueryResultCacheProvider(props: { cache?: QueryResultCache; children: React.ReactElement }) {
15 1 : const cache = React.useMemo(() => props.cache ?? createOPFSQueryResultCache(), [props.cache]);
16 1 : return <CACHE_CTX.Provider value={cache}>{props.children}</CACHE_CTX.Provider>;
17 1 : }
|