referential
Refer to mutable state safely.
Referential makes it easy to share mutable state safely. A Ref
to an object
or subtree always refers to the same underlying data regardless how it's
mutated.
Heavily optimized for reads.
Motivating example
var state = a: b: c: "world" var render = { var template = stateabc; return { return "Hello " + template }} stateabc = "This will be lost on you." console // Hello world
Install
$ npm install referential --save
Usage
refer = // Create a referenceref = // Get underlying value of reference // {a: 1} // 1 // Mutate stateref // {a: 1, b: 2}ref // {a: 1, b: 2, c: 3} // Extend stateref // {a: 1, b: 2, c: {d: 1, e: 2}}ref // {a: 1, b: 2, c: {d: 3, e: 2}} // Create entire tree as neededref // {a: 1, b: 2, c: 3, d: {e: {f: 4}}} // Get reference to subtreeref2 = ref // {f: 4} // Mutate subtree (and update parent)ref2 // {f: 4, g: 5} // Mutate parent (and update subtree)ref // {a: 1, b: 2, c: 3, d: {e: {f: 6, g: 5}}} // {f: 6, g: 5} // Clone ref, create new treeref3 = ref2ref3 // {f: 6, g: 6} // {f: 6, g: 5} // Bind to update event, should print `x was set to 2 from 1`ref4 = ref4ref4
Check the tests for more examples.