Referencia de API
Funciones principales y formas de retorno exportadas por global-rx-state.
createRxState
Si hay un argumento, es el modo de tupla anónima, y si se pasa un nombre, es el modo de objeto nombrado. El modo nombrado admite llamadas dentro del componente.
// Anonymous tuple mode: module top-level only
const [setCount, getCount, useCount, countSubject, countReady] =
createRxState(0);
// Named object mode
const { setCount, getCount, useCount, countSubject, countReady } =
createRxState(0, "count", { storage: "auto" });createRxStateTuple
Se utiliza cuando el nombre se determina en tiempo de ejecución pero se necesita el orden de retorno de la tupla. La opción de almacenamiento se puede usar junto con la clave nombrada.
const [setDraft, getDraft, useDraft, draftSubject, draftReady] =
createRxStateTuple(
{ content: "" },
`draft:${tabName}`,
{ storage: "sessionstorage" },
);createRxReducer / createRxReducerTuple
const { dispatchCart, getCart, useCart, cartSubject, cartReady } =
createRxReducer(initialCart, cartReducer, "cart", { storage: "auto" });
const [dispatchFilter, getFilter, useFilter] =
createRxReducerTuple(
initialFilterState,
filterReducer,
`workspace-filter:${workspaceId}`,
);valores de almacenamiento
RxStateStorage incluye alias compatibles con el backend de persistencia. sessionstorage y sessionStorage utilizan el sessionStorage del navegador.
type RxStateStorage =
| "in-memory"
| "auto"
| "indexeddb"
| "IndexedDB"
| "websql"
| "WebSQL"
| "localstorage"
| "localStorage"
| "sessionstorage"
| "sessionStorage";
type RxStateResolvedStorage =
| "in-memory"
| "indexeddb"
| "websql"
| "localstorage"
| "sessionstorage";
interface RxStateStorageOptions {
storage?: RxStateStorage;
name?: string;
storeName?: string;
keyPrefix?: string;
}autoselecciona el driver localForage posible en el orden de IndexedDB, WebSQL, localStorage. El almacenamiento resuelto de la API de consulta devuelve el backend seleccionado real, no auto.
Utilidades de almacenamiento
El almacenamiento de estado nombrado y el reductor se puede verificar con tres utilidades.
function hasRxState(
name: string,
storageOptions: RxStateStorageOptions,
): Promise<boolean>;
function findRxStateStorages(
name: string,
): Promise<RxStateResolvedStorage[]>;
function getRxStateStorageInfo(
name: string,
storageOptions?: RxStateStorageOptions,
): Promise<RxStateStorageInfo[]>;hasRxState verifica si hay valores en la configuración de almacenamiento especificada. findRxStateStorages devuelve el backend real verificado sin duplicados, y getRxStateStorageInfo siempre devuelve la ubicación de almacenamiento verificada como un arreglo.
interface RxStateStorageInfo {
key: string;
storageKey: string;
fullKey: string;
storage: RxStateResolvedStorage;
name: string;
storeName: string;
keyPrefix: string;
}storageKey es la clave del item real y fullKey es la clave completa que se puede verificar en el backend que expone la clave plana. Si hay el mismo nombre en varias configuraciones de almacenamiento, se devolverán múltiples elementos en el resultado de getRxStateStorageInfo.
function hasRxReducer(
name: string,
storageOptions: RxStateStorageOptions,
): Promise<boolean>;
function findRxReducerStorages(
name: string,
): Promise<RxStateResolvedStorage[]>;
function getRxReducerStorageInfo(
name: string,
storageOptions?: RxStateStorageOptions,
): Promise<RxStateStorageInfo[]>;