repos / starfx

supercharged async flow control library.
git clone https://github.com/neurosnap/starfx.git

starfx / store / slice
Eric Bower · 14 Dec 23

any.ts

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