Skip to content

igorDolzh/atom-observable

Repository files navigation

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.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published