shelving 1.268.1 → 1.268.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { type ReactElement } from "react";
|
|
2
2
|
import type { Endpoint } from "../api/endpoint/Endpoint.js";
|
|
3
|
+
import { APICache } from "../api/index.js";
|
|
3
4
|
import type { APIProvider } from "../api/provider/APIProvider.js";
|
|
4
5
|
import type { EndpointStore } from "../api/store/EndpointStore.js";
|
|
5
6
|
import type { ChildProps } from "../ui/index.js";
|
|
@@ -13,8 +14,10 @@ export interface APIContext<P, R> {
|
|
|
13
14
|
/** React hook to return an `EndpointStore` for the specified endpoint/payload in the current `APIProvider` context. */
|
|
14
15
|
useAPI<PP extends P, RR extends R>(this: void, endpoint: Endpoint<PP, RR>, payload: PP): EndpointStore<PP, RR>;
|
|
15
16
|
useAPI<PP extends P, RR extends R>(this: void, endpoint: Nullish<Endpoint<PP, RR>>, payload: PP): EndpointStore<PP, RR> | undefined;
|
|
16
|
-
/**
|
|
17
|
+
/** Create a new `APICache` and provide it as context to child nodes, to allow them to use `useAPI()` */
|
|
17
18
|
readonly APIContext: (props: ChildProps) => ReactElement;
|
|
19
|
+
/** Return the underlying `APICache` for `<APIContext>` */
|
|
20
|
+
requireAPICache(): APICache<P, R>;
|
|
18
21
|
}
|
|
19
22
|
/**
|
|
20
23
|
* Create an API context.
|
|
@@ -15,15 +15,18 @@ import { useStore } from "./useStore.js";
|
|
|
15
15
|
*/
|
|
16
16
|
export function createAPIContext(provider) {
|
|
17
17
|
const CacheContext = createContext(undefined);
|
|
18
|
-
function
|
|
18
|
+
function requireAPICache(caller = requireAPICache) {
|
|
19
19
|
const cache = use(CacheContext);
|
|
20
20
|
if (!cache)
|
|
21
|
-
throw new RequiredError(`
|
|
22
|
-
return
|
|
21
|
+
throw new RequiredError(`Must be used inside <APIContext>`, { caller });
|
|
22
|
+
return cache;
|
|
23
|
+
}
|
|
24
|
+
function useAPI(endpoint, payload) {
|
|
25
|
+
return useStore(endpoint ? requireAPICache(useAPI).get(endpoint).get(payload) : undefined);
|
|
23
26
|
}
|
|
24
27
|
function APIContext({ children }) {
|
|
25
28
|
const cache = useInstance(APICache, provider);
|
|
26
29
|
return _jsx(CacheContext, { value: cache, children: children });
|
|
27
30
|
}
|
|
28
|
-
return { useAPI, APIContext };
|
|
31
|
+
return { requireAPICache, useAPI, APIContext };
|
|
29
32
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { type ReactElement, type ReactNode } from "react";
|
|
2
|
+
import { DBCache } from "../db/cache/DBCache.js";
|
|
2
3
|
import type { Collection } from "../db/collection/Collection.js";
|
|
3
4
|
import type { DBProvider } from "../db/provider/DBProvider.js";
|
|
4
5
|
import type { ItemStore } from "../db/store/ItemStore.js";
|
|
@@ -23,10 +24,12 @@ export interface DBContext<I extends Identifier, T extends Data> {
|
|
|
23
24
|
query: Nullish<Query<Item<II, TT>>>): QueryStore<II, TT> | undefined;
|
|
24
25
|
useQuery<II extends I, TT extends T>(collection: Collection<string, II, TT>, //
|
|
25
26
|
query: Query<Item<II, TT>>): QueryStore<II, TT>;
|
|
26
|
-
/**
|
|
27
|
+
/** Create a new `DBCache` and provide it as context to child nodes, to allow them to use `useItem()` and `useQuery()` */
|
|
27
28
|
readonly DBContext: ({ children }: {
|
|
28
29
|
children: ReactNode;
|
|
29
30
|
}) => ReactElement;
|
|
31
|
+
/** Return the underlying `DBCache` for `<DBContext>` */
|
|
32
|
+
requireDBCache(): DBCache<I, T>;
|
|
30
33
|
}
|
|
31
34
|
/**
|
|
32
35
|
* Create a data context
|
package/react/createDBContext.js
CHANGED
|
@@ -15,23 +15,23 @@ import { useStore } from "./useStore.js";
|
|
|
15
15
|
*/
|
|
16
16
|
export function createDBContext(provider) {
|
|
17
17
|
const CacheContext = createContext(undefined);
|
|
18
|
-
function
|
|
19
|
-
id) {
|
|
18
|
+
function requireDBCache(caller = requireDBCache) {
|
|
20
19
|
const cache = use(CacheContext);
|
|
21
20
|
if (!cache)
|
|
22
|
-
throw new RequiredError(
|
|
23
|
-
return
|
|
21
|
+
throw new RequiredError(`Must be used inside <DBContext>`, { caller });
|
|
22
|
+
return cache;
|
|
23
|
+
}
|
|
24
|
+
function useItem(collection, //
|
|
25
|
+
id) {
|
|
26
|
+
return useStore(collection && id ? requireDBCache(useItem).getItem(collection, id) : undefined);
|
|
24
27
|
}
|
|
25
28
|
function useQuery(collection, //
|
|
26
29
|
query) {
|
|
27
|
-
|
|
28
|
-
if (!cache)
|
|
29
|
-
throw new RequiredError("useQuery() can only be used inside <DBContext>", { caller: useQuery });
|
|
30
|
-
return useStore(collection && query ? cache.getQuery(collection, query) : undefined);
|
|
30
|
+
return useStore(collection && query ? requireDBCache(useQuery).getQuery(collection, query) : undefined);
|
|
31
31
|
}
|
|
32
32
|
function DBContext({ children }) {
|
|
33
33
|
const cache = useInstance(DBCache, provider);
|
|
34
34
|
return _jsx(CacheContext, { value: cache, children: children });
|
|
35
35
|
}
|
|
36
|
-
return { useItem, useQuery, DBContext };
|
|
36
|
+
return { requireDBCache, useItem, useQuery, DBContext };
|
|
37
37
|
}
|