Immutable data

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) // false

One.put(item);
Object.isFrozen(item) // true - the item and the cache item are the same

let 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) // false
item === editable // false

// Edit
editable.text = "test"
One.put(editable)

let edited = One.get(1);
edited.text = "text" // true
Object.isFrozen(edited) // true

item === One.get(item) // false

// same as
One.isDirty(item) // true - can be used to trigger a React render

// but
One.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 // true
One.get(2) === item2 // true

let editable = One.getEdit(1);
editable.text = "test";
One.put(editable); // also updates item2 reference to item

let 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.

------ One -------
STACK:
-> 0:
[{
  "entity": {
    "uid": 2,
    "child": {
      "uid": 1
    }
  },
  "ref_from": [],
  "ref_to": {
    "1": [
      "child"
    ]
  }
},
{
  "entity": {
    "uid": 1
  },
  "ref_from": {
    "2": [
      "child"
    ]
  },
  "ref_to": {}
}],

CONFIG:{
  "prop": {
    "uidName": "uid",
    "maxHistoryStates": 1000
  }
}

QUEUE:{}

HISTORY:{
  "index": 0,
  "node": 0,
  "length": 1,
  "hasPrev": false,
  "hasNext": false
}

REPO SIZE:1
===================

Last updated