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