atomo
Clojure's Atom implementation in JavaScript.
From the Clojure docs:
Atoms provide a way to manage shared, synchronous, independent state.
On top of the above, atoms provide also validation and observation capabilities.
Installation
$ npm install atomo
Usage
Atoms are references to values that may change over time. The most basic operation on an atom is querying its current value:
var a = ; var anAtom = a;anAtom === 42// true
An atom's value can be set to another value:
anAtom; anAtom === "foo"// true
Alternatively, an atom's value can be transitioned to another value providing a function:
{ return x + 1; } var anAtom = a;anAtom;anAtom === 42// true
Validation
Atoms support validation through a validation function, and they will throw an exception whenever we try to set the atom's value to an illegal value.
{ return x === 42 }; var anAtom = a; anAtom; // Error!anAtom; // Error!
Observation
Atoms support adding and removing watches for listening to value changes. Watches are called with three arguments: the atom, the old value and the new value.
var anAtom = a; var { console;}; anAtom; anAtom;// Atom changed from 42 to 43 anAtom;// Atom changed from 43 to 42 anAtom; anAtom;anAtom;
License
BSD 2-clause license, Copyright 2014 - 2015 Alejandro Gómez.