constant-ts
v1.0.0
Published
Constant ========
Downloads
11
Readme
Constant
A class that represents immutable data.
Constant<T> objects contain inner copy of original model of class T and provide read-only access to fields.
Usage
For class
class TreeNode
{
parent: TreeNode;
value: string;
}Lets create some data structure:
let root = new TreeNode();
root.value = "root";
let child = new TreeNode();
child.parent = root;
child.value = "child";Probably it should be stored in some immutable store (like Redux one :wink:).
To provide new state with a garant of lack of mutation on original model we use Constant<TreeNode>:
let childConstant = new Constant(child);
...
console.log(childConstant); // is Constant<TreeNode> for child
let childValue = childConstant.get(c => c.value);
console.log(childValue); // "child"
let rootConstant = childConstant.get(c => c.root);
let rootValue = rootConstant.get(c => c.value);
console.log(rootValue); // "root"
