@thi.ng/atom
About
Clojure inspired mutable wrappers for (usually) immutable values, with support for watches.
TODO
Installation
yarn add @thi.ng/atom
Usage examples
Atom
import * as atom from "@thi.ng/atom";
const a = new atom.Atom(23);
a.deref();
a.addWatch("foo", (id, prev, curr) => console.log(`${id}: ${prev} -> ${curr}`));
a.swap((val)=> val + 1);
a.reset(42);
Cursor
main = new atom.Atom({ a: { b: { c: 23 }, d: { e: 42 } }, f: 66 });
cursor = new atom.Cursor(main, "a.b.c");
cursor = new atom.Cursor(main, ["a","b","c"]);
cursor = new atom.Cursor(
main,
(s) => s.a.b.c,
(s, x) => ({...s, a: {...s.a, b: {...s.a.b, c: x}}})
);
cursor.addWatch("foo", console.log);
cursor.deref()
cursor.swap(x => x + 1);
main.deref()
Authors
License
© 2018 Karsten Schmidt // Apache Software License 2.0