Eric Bower
·
17 Aug 24
types.ts
1import type { LoaderOutput } from "./slice/loaders.ts";
2import type { TableOutput } from "./slice/table.ts";
3import type { Operation, Patch, Scope } from "../deps.ts";
4import { BaseCtx } from "../mod.ts";
5import type { AnyAction, AnyState } from "../types.ts";
6import { createRun } from "./run.ts";
7
8export type StoreUpdater<S extends AnyState> = (s: S) => S | void;
9
10export type Listener = () => void;
11
12export interface UpdaterCtx<S extends AnyState> extends BaseCtx {
13 updater: StoreUpdater<S> | StoreUpdater<S>[];
14 patches: Patch[];
15}
16
17declare global {
18 interface SymbolConstructor {
19 readonly observable: symbol;
20 }
21}
22
23export interface BaseSchema<TOutput> {
24 initialState: TOutput;
25 schema: string;
26 name: string;
27}
28
29export type Output<O extends { [key: string]: BaseSchema<unknown> }> = {
30 [key in keyof O]: O[key]["initialState"];
31};
32
33export interface FxMap {
34 loaders: <M extends AnyState>(s: string) => LoaderOutput<M, AnyState>;
35 cache: (s: string) => TableOutput<any, AnyState>;
36 [key: string]: (name: string) => BaseSchema<unknown>;
37}
38
39export type FxSchema<S extends AnyState, O extends FxMap = FxMap> =
40 & {
41 [key in keyof O]: ReturnType<O[key]>;
42 }
43 & { update: FxStore<S>["update"] };
44
45export interface FxStore<S extends AnyState> {
46 getScope: () => Scope;
47 getState: () => S;
48 subscribe: (fn: Listener) => () => void;
49 update: (u: StoreUpdater<S> | StoreUpdater<S>[]) => Operation<UpdaterCtx<S>>;
50 reset: (ignoreList?: (keyof S)[]) => Operation<UpdaterCtx<S>>;
51 run: ReturnType<typeof createRun>;
52 // deno-lint-ignore no-explicit-any
53 dispatch: (a: AnyAction | AnyAction[]) => any;
54 replaceReducer: (r: (s: S, a: AnyAction) => S) => void;
55 getInitialState: () => S;
56 // deno-lint-ignore no-explicit-any
57 [Symbol.observable]: () => any;
58}
59
60export interface QueryState {
61 cache: TableOutput<any, any>["initialState"];
62 loaders: LoaderOutput<any, any>["initialState"];
63}