atom-obvervable

0.1.0 • Public • Published

js-atom

Atom contains a value which can change over time.

interface Atom<T, A: mixed[]> {
  deref(): T,
  reset(value: T): Atom<T>,
  swap(fn: (currentValue: T, ...args:A) => T, ...args: A): Atom<T>,
  watch(fn: (newValue: T, oldValue: T) => void): () => void
}

atom

type CreateAtom = <T>(value: T) => Atom<T>

Creates new atom.

deref

type Deref = <T>(ref: Atom<T>) => T

Unwrap atom's value

reset

type Reset = <T>(ref: Atom<T>, value: T) => Atom<T>

Rewrites atom's value. Returns atom.

swap

type Swap = <T, A: mixed[]>(
  ref: Atom<T>,
  transform: (currentValue: T, ...args: A) => T,
  ...args: A
) => Atom<T>

Rewrites atom's value through transformer function which takes old value as the first argument and returns new value. Other aruments passed to swap are also passed to transformer function.

watch

type Watch = <T>(
  ref: Atom<T>,
  callback: (newValue: T, oldValue: T) => void
) => () => void

Set a watcher for the atom. Callback will fire every time atom changes through reset or swap. Returns unsub function.

Readme

Keywords

none

Package Sidebar

Install

npm i atom-obvervable

Weekly Downloads

1

Version

0.1.0

License

none

Last publish

Collaborators

  • igordolzh