shelving
Version:
Toolkit for using data in JavaScript.
25 lines (24 loc) • 1.62 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 { DataKey, Database } from "../util/data.js";
import type { ItemQuery } from "../util/item.js";
import type { Optional } from "../util/optional.js";
export interface DataContext<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: string): ItemStore<T, K>;
useItem<K extends DataKey<T>>(this: void, collection: Optional<K>, id: Optional<string>): ItemStore<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<T[K]>): QueryStore<T, K>;
useQuery<K extends DataKey<T>>(this: void, collection: Optional<K>, query: Optional<ItemQuery<T[K]>>): QueryStore<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<T extends Database>(provider: AbstractProvider<T>): DataContext<T>;