shelving
Version:
Toolkit for using data in JavaScript.
26 lines (25 loc) • 1.72 kB
TypeScript
import type { ReactElement, ReactNode } from "react";
import { ItemStore } from "../db/ItemStore.js";
import type { AbstractProvider } from "../db/Provider.js";
import { QueryStore } from "../db/QueryStore.js";
import type { Database, DataKey } from "../util/data.js";
import type { Identifier } from "../util/item.js";
import type { Nullish } from "../util/null.js";
import type { ItemQuery } from "../util/query.js";
export interface DataContext<I extends Identifier, T extends Database> {
/** Get an `ItemStore` for the specified collection item in the current `DataProvider` context and subscribe to any changes in it. */
useItem<K extends DataKey<T>>(this: void, collection: K, id: I): ItemStore<I, T, K>;
useItem<K extends DataKey<T>>(this: void, collection: Nullish<K>, id: Nullish<I>): ItemStore<I, T, K> | undefined;
/** Get an `QueryStore` for the specified collection query in the current `DataProvider` context and subscribe to any changes in it. */
useQuery<K extends DataKey<T>>(this: void, collection: K, query: ItemQuery<I, T[K]>): QueryStore<I, T, K>;
useQuery<K extends DataKey<T>>(this: void, collection: Nullish<K>, query: Nullish<ItemQuery<I, T[K]>>): QueryStore<I, T, K> | undefined;
readonly DataContext: ({ children }: {
children: ReactNode;
}) => ReactElement;
}
/**
* Create a data context
* - Allows React elements to call `useItem()` and `useQuery()` to access items/queries in a database provider.
* - If the database has a `CacheProvider` in its chain then in-memory data will be used in the returned stores.
*/
export declare function createDataContext<I extends Identifier, T extends Database>(provider: AbstractProvider<I, T>): DataContext<I, T>;