repos / starfx

a micro-mvc framework for react apps
git clone https://github.com/neurosnap/starfx.git

starfx / src / store / slice
Eric Bower  ·  2025-06-06

any.ts

 1import type { AnyState } from "../../types.js";
 2import type { BaseSchema } from "../types.js";
 3
 4export interface AnyOutput<V, S extends AnyState> extends BaseSchema<V> {
 5  schema: "any";
 6  initialState: V;
 7  set: (v: V) => (s: S) => void;
 8  reset: () => (s: S) => void;
 9  select: (s: S) => V;
10}
11
12export function createAny<V, S extends AnyState = AnyState>({
13  name,
14  initialState,
15}: {
16  name: keyof S;
17  initialState: V;
18}): AnyOutput<V, S> {
19  return {
20    schema: "any",
21    name: name as string,
22    initialState,
23    set: (value) => (state) => {
24      // deno-lint-ignore no-explicit-any
25      (state as any)[name] = value;
26    },
27    reset: () => (state) => {
28      // deno-lint-ignore no-explicit-any
29      (state as any)[name] = initialState;
30    },
31    select: (state) => {
32      // deno-lint-ignore no-explicit-any
33      return (state as any)[name];
34    },
35  };
36}
37
38export function any<V>(initialState: V) {
39  return (name: string) => createAny<V, AnyState>({ name, initialState });
40}