garage44.eu/jeroen/midas

logclone: git clone https://garage44.eu/jeroen/midas.git

.. /web/src/lib/reactive.ts

raw

import {state} from '@garage44/mithril'

/**
 * A component's own reactive state (atlas's `lib/reactive.ts`): a view that
 * reads a field redraws when it is written — from a handler, a fetch, a timer —
 * with no `m.redraw()`. Typed as the plain shape, as there.
 */
export function local<T extends object>(initial: T): T {
    return state(initial) as unknown as T
}

/** One field of a `local()` object as a component `model`. */
export function bind<T extends object, K extends keyof T>(s: T, key: K): {value: T[K]} {
    return {
        get value() {
            return s[key]
        },
        set value(next: T[K]) {
            s[key] = next
        },
    }
}