/*! * Mode Atom — TypeScript Definitions * MIT License — Copyright (c) 2025 Silvio Corigliano */ /** A reactive source speaking the atom protocol ({ get, sub }), plus writes. */ export interface Atom { get(): T; set(next: T): void; update(fn: (v: T) => T): void; upd(fn: (v: T) => T): void; /** Subscribe; `runNow` (default true) fires the callback immediately. Returns an unsubscribe. */ sub(fn: (v: T) => void, runNow?: boolean): () => void; } export interface Computed extends Atom { /** Release the subscriptions to the dependencies. */ dispose(): void; } /** Minimal reactive dependency (anything with `sub`). */ export interface Subscribable { sub(fn: (...args: any[]) => void, runNow?: boolean): () => void; } /** Create a writable reactive cell. */ export function atom(initial: T): Atom; /** Derive a read value from dependencies; recomputes when any dependency changes. */ export function computed(getter: () => T, deps: Subscribable[]): Computed; /** Run `fn` now and again whenever a dependency changes; returns a stop function. */ export function effect(fn: () => void | (() => void), deps: Subscribable[]): () => void;