@hashbrownai/core
Version:
Runtime helpers for Hashbrown AI
312 lines • 12.4 kB
TypeScript
/**
* Represents a minimal Redux-like store.
* @typeParam State - The shape of the store's state.
* @param dispatch - Dispatches an action to update state.
* @param select - Selects a slice of state using a selector function.
* @param when - Registers a callback for one or more action types.
* @param whenOnce - Registers a one-time callback for action types.
* @param teardown - Cleans up any registered effects.
*/
export interface Store<State> {
dispatch: (action: AnyAction) => void;
read: <T>(selector: (state: State) => T) => T;
select: <T>(selector: (state: State) => T, onChange: (value: T) => void) => () => void;
when: <Actions extends readonly ActionCreator[], Handler extends (action: ActionType<Actions[number]>) => void>(...params: [...Actions, Handler]) => () => void;
whenOnce: <Actions extends readonly ActionCreator[], Handler extends (action: ActionType<Actions[number]>) => void>(...params: [...Actions, Handler]) => () => void;
createSignal: <Result>(selector: (state: State) => Result) => StateSignal<Result>;
runEffects: () => () => void;
}
/**
* ================================
* === Scheduler ===
* ================================
*/
export interface Scheduler {
scheduleTask(fn: () => void): number;
cancelTask(id: number): void;
}
/**
* Synchronous “trampoline” scheduler.
*
* All work executes in the same macrotask, but stack-safe:
* tasks scheduled from inside other tasks are queued
* and processed after the current one finishes.
*/
export declare class TrampolineScheduler implements Scheduler {
private nextId;
private queue;
private active;
private flush;
scheduleTask(fn: () => void): number;
cancelTask(id: number): void;
}
/**
* ================================
* === Actions ===
* ================================
*/
/**
* A generic action object.
*
* @typedef AnyAction
* @param type - The action type identifier.
* @param payload - Optional payload for the action.
*/
type AnyAction = {
type: string;
payload: any;
} | {
type: string;
};
/**
* Defines a function type that produces payload objects for action creators.
*
* @typedef PropsFunction
* @param payload - The payload for the action.
* @returns The processed payload or void.
*/
type PropsFunction = (payload: any) => any | (() => void);
/**
* Extracts the payload type from a PropsFunction.
*
* @typeParam T - A function type that defines action payload.
* @typedef Payload
*/
type Payload<T extends PropsFunction> = T extends () => void ? void : T extends (payload: infer P) => infer P ? P : never;
/**
* Factory type for creating strongly-typed action creator functions.
*
* @typeParam K - The action type string.
* @typeParam T - A PropsFunction defining the payload shape.
* @typedef ActionCreator
*/
type ActionCreator<K extends string = string, T extends PropsFunction = PropsFunction> = {
type: K;
} & (Payload<T> extends void ? () => {
type: K;
} : (payload: Payload<T>) => {
type: K;
payload: Payload<T>;
});
/**
* Infers the action object type produced by an ActionCreator.
*
* @typeParam T - The ActionCreator type.
* @typedef ActionType
*/
type ActionType<T> = T extends ActionCreator<infer K, infer R> ? R extends (payload: infer P) => infer P ? P extends void ? {
type: K;
} : {
type: K;
payload: P;
} : never : never;
/**
* A mapping of action name keys to their corresponding ActionCreator functions.
*
* @typeParam GroupName - The prefix group name for action types.
* @typeParam T - An object of payload function definitions.
* @typedef ActionCreators
*/
type ActionCreators<GroupName extends string, T extends {
[key: string]: PropsFunction;
}> = {
[K in keyof T]: K extends string ? ActionCreator<`[${GroupName}] ${K}`, T[K]> : never;
};
/**
* Creates a payload projector function that returns its argument.
*
* @typeParam T - The payload type.
* @returns Function that returns the provided payload.
*/
export declare function props<T>(): (payload: T) => T;
/**
* Creates an action creator with no payload.
*
* @returns Function that produces an action with only a type.
*/
export declare function emptyProps(): () => void;
/**
* Generates a group of action creator functions with a common type prefix.
*
* @typeParam GroupName - The modifier for action types (e.g., feature name).
* @typeParam T - An object whose values are payload creator functions.
* @param name - The group prefix name.
* @param group - An object mapping action names to payload functions.
* @returns A set of action creators.
*/
export declare function createActionGroup<GroupName extends string, T extends {
[key: string]: (...args: any[]) => any;
}>(name: GroupName, group: T): ActionCreators<GroupName, T>;
/**
* ================================
* === Reducers ===
* ================================
*/
/**
* Creates a reducer function that responds to specified action types.
*
* @typeParam State - The type of the slice of state.
* @typeParam Actions - An array of ActionCreator types to handle.
* @param params - One or more action creators followed by a reducer handler.
* @returns A reducer function.
*/
export declare function on<State, Actions extends readonly ActionCreator[], Handler extends (state: State, action: ActionType<Actions[number]>) => State>(...params: [...Actions, Handler]): (state: State, action: AnyAction) => State;
/**
* Combines multiple reducer functions into a single root reducer.
*
* @typeParam State - The combined state shape.
* @param initialState - The initial state when undefined is passed.
* @param reducers - One or more reducer functions.
* @returns The root reducer.
*/
export declare function createReducer<State>(initialState: State, ...reducers: readonly ((state: State, action: {
type: string;
}) => State)[]): (state: State | undefined, action: {
type: string;
}) => State;
/**
* ================================
* === Effects ===
* ================================
*/
/**
* Defines an effect that can subscribe to store actions and return a cleanup function.
*
* @param effectFn - Function that receives the store and returns a teardown callback.
* @returns The provided effect function.
*/
type EffectFn = (store: Store<any>) => () => void;
/**
* Creates an effect function that can subscribe to store actions and return a cleanup function.
*
* @param effectFn - Function that receives the store and returns a teardown callback.
* @returns The provided effect function.
*/
export declare function createEffect(effectFn: EffectFn): EffectFn;
/**
* ================================
* === Selectors ===
* ================================
*/
interface SelectConfig {
debugName?: string;
}
/**
* Creates a memoized selector from one or more input selectors and a projector function.
* @param params - Input selector functions followed by a projector.
* @returns A selector function that returns computed state.
*/
export declare function select<S, T0, R>(t0: (state: S) => T0, projectFn: (t0: T0) => R, config?: SelectConfig): (state: S) => R;
export declare function select<S, T0, T1, R>(t0: (state: S) => T0, t1: (state: S) => T1, projectFn: (t0: T0, t1: T1) => R, config?: SelectConfig): (state: S) => R;
export declare function select<S, T0, T1, T2, R>(t0: (state: S) => T0, t1: (state: S) => T1, t2: (state: S) => T2, projectFn: (t0: T0, t1: T1, t2: T2) => R, config?: SelectConfig): (state: S) => R;
export declare function select<S, T0, T1, T2, T3, R>(t0: (state: S) => T0, t1: (state: S) => T1, t2: (state: S) => T2, t3: (state: S) => T3, projectFn: (t0: T0, t1: T1, t2: T2, t3: T3) => R, config?: SelectConfig): (state: S) => R;
export declare function select<S, T0, T1, T2, T3, T4, R>(t0: (state: S) => T0, t1: (state: S) => T1, t2: (state: S) => T2, t3: (state: S) => T3, t4: (state: S) => T4, projectFn: (t0: T0, t1: T1, t2: T2, t3: T3, t4: T4) => R, config?: SelectConfig): (state: S) => R;
export declare function select<S, T0, T1, T2, T3, T4, T5, R>(t0: (state: S) => T0, t1: (state: S) => T1, t2: (state: S) => T2, t3: (state: S) => T3, t4: (state: S) => T4, t5: (state: S) => T5, projectFn: (t0: T0, t1: T1, t2: T2, t3: T3, t4: T4, t5: T5) => R, config?: SelectConfig): (state: S) => R;
export declare function select<S, T0, T1, T2, T3, T4, T5, T6, R>(t0: (state: S) => T0, t1: (state: S) => T1, t2: (state: S) => T2, t3: (state: S) => T3, t4: (state: S) => T4, t5: (state: S) => T5, t6: (state: S) => T6, projectFn: (t0: T0, t1: T1, t2: T2, t3: T3, t4: T4, t5: T5, t6: T6) => R, config?: SelectConfig): (state: S) => R;
/**
* ================================
* === State Observable ===
* ================================
*/
/**
* @public
*/
export interface StateSignal<State> {
(): State;
subscribe(onChange: (value: State) => void): () => void;
}
/**
* ================================
* === Store ===
* ================================
*/
/**
* Creates a store with reducers and effects.
* @typeParam Reducers - An object mapping keys to reducer functions.
* @typeParam State - The resulting state shape inferred from Reducers.
* @param config - Configuration object.
* @returns The initialized store instance.
*/
export declare function createStore<Reducers extends {
[key: string]: (state: any | undefined, action: AnyAction) => unknown;
}, State extends {
[K in keyof Reducers]: Reducers[K] extends (state: infer R | undefined, action: any) => infer R ? R : never;
}>(config: {
debugName?: string;
reducers: Reducers;
effects: EffectFn[];
projectStateForDevtools?: (state: State) => object;
}): Store<State>;
/**
* ================================
* === Entities ===
* ================================
*/
/**
* Maintains a normalized collection of entities.
* @typeParam Entity - The type of the entity.
* @param ids - Array of entity IDs.
* @param entities - Map of IDs to entity objects.
*/
export interface EntityState<Entity> {
ids: string[];
entities: Record<string, Entity>;
}
/**
* Describes a partial update to a single entity.
* @typeParam Entity - The entity type.
* @param id - The target entity ID.
* @param updates - The fields to update.
*/
export interface EntityChange<Entity> {
id: string;
updates: Partial<Entity>;
}
/**
* Provides methods to manage a collection of entities.
* @typeParam Entity - The entity type.
* @param updateOne - Updates a single entity.
* @param updateMany - Updates multiple entities.
* @param addOne - Adds a single new entity.
* @param addMany - Adds multiple new entities.
* @param removeOne - Removes a single entity by ID.
* @param removeMany - Removes multiple entities by ID.
*/
export interface EntityAdapter<Entity> {
updateOne: (state: EntityState<Entity>, changes: EntityChange<Entity>) => EntityState<Entity>;
updateMany: (state: EntityState<Entity>, changes: EntityChange<Entity>[]) => EntityState<Entity>;
addOne: (state: EntityState<Entity>, entity: Entity) => EntityState<Entity>;
addMany: (state: EntityState<Entity>, entities: Entity[]) => EntityState<Entity>;
removeOne: (state: EntityState<Entity>, id: string) => EntityState<Entity>;
removeMany: (state: EntityState<Entity>, ids: string[]) => EntityState<Entity>;
}
/**
* Creates an EntityAdapter for performing immutable updates on entity collections.
* @typeParam Entity - The entity type.
* @param config - Configuration with a selectId function.
* @returns Adapter with CRUD methods for entity state.
*/
export declare function createEntityAdapter<Entity>(config: {
selectId: (entity: Entity) => string;
}): EntityAdapter<Entity>;
/**
* ================================
* === Devtools ===
* ================================
*/
export interface DevtoolsChromeExtension {
connect: (options: {
name: string;
}) => DevtoolsChromeExtensionConnection;
}
export interface DevtoolsChromeExtensionConnection {
unsubscribe: () => void;
send: (action: AnyAction, state: object) => void;
init: (state: object) => void;
error: (message: string) => void;
}
export declare function connectToChromeExtension(options: {
name: string;
}): DevtoolsChromeExtensionConnection | undefined;
export {};
//# sourceMappingURL=micro-ngrx.d.ts.map