All cache data is immutable. Once an item enters the cache it is frozen and cannot be changed. This is so that identity checks can perform ultra fast against frozen entities. For example React can make use of such simple identity checks in shouldComponentUpdate.
Some code
let item = {uid:1}Object.isFrozen(item) // falseOne.put(item);Object.isFrozen(item) // true - the item and the cache item are the samelet cached =One.get(item)cached === item // true
If you later want to edit a reference of the object you can get an editable copy from the cache. This gives you a separate deep clone of the object that is now editable
let item = {uid:1}One.put(item)let editable =One.getEdit(1) // or One.getEdit(item1);Object.isFrozen(editable) // falseitem === editable // false// Editeditable.text ="test"One.put(editable)let edited =One.get(1);edited.text ="text"// trueObject.isFrozen(edited) // trueitem ===One.get(item) // false// same asOne.isDirty(item) // true - can be used to trigger a React render// butOne.isDirty(edited) // false
Editing an item changes all of its instances in the cache and replaces each with the same memory instance. The cache optimizes this by maintaining metadata about each item's references.
let item = {uid:1}let item2 = {uid:1, child:item}One.put(item2);One.get(1) === item // trueOne.get(2) === item2 // truelet editable =One.getEdit(1);editable.text ="test";One.put(editable); // also updates item2 reference to itemlet result =One.get(2);console.log(JSON.stringify(result.item)) // {uid:1, text:"test"}
For debugging purposes you may use the print() method on the cache. This prints the following to the console See here link needed for a detailed explanation.