import React, { createContext, useContext, useMemo, useSyncExternalStore } from 'react'; import type { FC, PropsWithChildren } from 'react'; const uuid = () => Math.round((Math.random() + 1) * Date.now()).toString(36); const UNIQ = Symbol('BUILD'); type Listen = (cb: (data: T) => void) => VoidFunction; type Reduce = (data: T) => T; type Creator = (get: () => T, set: Change, use: UseAtom) => A; export type Change = (ch: Reduce | T) => void; interface ReadonlyState { get: () => T; listen: Listen; use: () => T; } interface ValueState extends ReadonlyState { change: Change; } interface ActionState extends ReadonlyState { actions: A; } export type AnyAtom = ValueAtom | ActionAtom | ComputedAtom; export interface Query { (atom: ComputedAtom): ReadonlyState; (atom: ActionAtom): ActionState; (atom: ValueAtom): ValueState; (atom: AnyAtom): ReadonlyState; } export interface UseAtom { (atom: ComputedAtom): T; (atom: ActionAtom): [T, A]; (atom: ValueAtom): [T, Change]; } export type UseData = (atom: AnyAtom) => T; function generate(val: T) { const listener = new Set<(val: T) => void>(); const change: Change = (ch) => { const next = (typeof ch === 'function') ? (ch as Reduce)(val) : ch; if (Object.is(val, next)) return; val = next; listener.forEach(call => call(next)); }; const listen: Listen = (call) => { listener.add(call); return () => listener.delete(call); }; const get = () => val; const use = () => useSyncExternalStore(listen, get, get); return { get, change, listen, use }; } class ValueAtom { public readonly type = 'v' as const; public readonly key = uuid(); public proxy?: (get: () => T, set: Change) => Change; constructor(private init: T) {} public [UNIQ](): ValueState { const state = generate(this.init); if (this.proxy) { state.change = this.proxy(state.get, state.change); } return state; } public useDataOnly(): T { return useContext(Context)(this).use(); } public useData(): [T, Change] { const { use, change } = useContext(Context)(this); return [use(), change]; } public useChange(): Change { return useContext(Context)(this).change; } } const _q_2_u_ = new WeakMap(); function buildUseFromQuery(query: Query): UseAtom { if (_q_2_u_.has(query)) return _q_2_u_.get(query); function use(atom: AnyAtom) { switch (atom.type) { case 'c': return query(atom).get(); case 'v': { const { get, change } = query(atom); return [get(), change]; } case 'a': { const { get, actions } = query(atom); return [get(), actions]; } } } return (_q_2_u_.set(query, use), use); } class ActionAtom { public readonly type = 'a' as const; public readonly key = uuid(); constructor( private readonly init: T, private readonly creator: Creator) {} public [UNIQ](query: Query): ActionState { const state = generate(this.init); const use = buildUseFromQuery(query); const actions = this.creator(state.get, state.change, use); Object.assign(state, { actions }); return state as any; } public useDataOnly(): T { return useContext(Context)(this).use(); } public useData(): [T, A] { const { use, actions } = useContext(Context)(this); return [use(), actions]; } public useChange(): A { return useContext(Context)(this).actions; } } class ComputedAtom { public readonly type = 'c' as const; public readonly key = uuid(); constructor(private readonly calc: (use: UseData) => T) {} public [UNIQ](query: Query): ReadonlyState { const deps: AnyAtom[] = []; let use = (atom: AnyAtom) => (deps.push(atom), query(atom).get()); const state = generate(this.calc(use)); if (deps.length > 0) { use = (atom: AnyAtom) => query(atom).get(); const update = () => state.change(this.calc(use)); deps.forEach((a) => query(a).listen(update)); } return state; } public useData(): T { return useContext(Context)(this).use(); } } export function atom(initial: (use: UseData) => T): ComputedAtom; export function atom(initial: T, creator: Creator): ActionAtom; export function atom(initial: T): ValueAtom; export function atom(a: any, b?: any) { if (typeof a === 'function') return new ComputedAtom(a); if (b) return new ActionAtom(a, b); return new ValueAtom(a); } function build(): Query { const map: { [k: string]: any } = {}; return function query(atom: AnyAtom) { return map[atom.key] || (map[atom.key] = atom[UNIQ](query)); } } const Context = createContext(build()); const Root = Context.Provider; export const WithStore: FC = (p) => {p.children}; export function mutate(init: (use: UseAtom) => T) { const cache = new WeakMap(); return { use(): T { const query = useContext(Context); if (cache.has(query)) return cache.get(query); const result = init(buildUseFromQuery(query)); return (cache.set(query, result), result); } } }