Get

  • get

  • getEdit

get(entityOrUid)

Gets an immutable item from the cache. The item is deeply frozen. No dependent entities inside the object are editable.

Parameters

  • entityOrUid Object | string either the object's unique id or a stale version of the object. The cache retrieves its uid and returns the Cache copy.

Returns

  • *, the cache copy of the object or null if none is present

getEdit(entityOrUid)

Same as get() only retrieves a new deeply editable instance of the item cached. This instance can be edited and put back into the Cache. This creates a new cached version of the entity. The previous version is still available via an undo() operation. Identity comparisons of this entity with previous versions fail.

One.get(uid) !== One.getEdit(uid)

This is valid event if the referenced entity is inside an array. It updates cache wide

let item1 = {uid:1}
let item2 = {uid:2, val:item1}
let item3 = {uid:3, children:[item1]};
One.put([item1, item2]);

let editable = One.getEdit(item2);
editable.val.text = "test";
One.put(editable);

// all item1 references are updated
One.get(1).text === "test" // true

let cached3 = One.get(3)
cached3.children[0].text === "test" // true

*IMPORTANT getEdit() dynamic

Getting an editable object from the cache only clones contained items that have NO UID. This is for performance reasons. Because of that you should always getEdit() the cache item "closest to your uid".

let item = {uid:1}
let item2 = {
  uid:2,
  ref:{value:"val"}, // no uid - object will be cloned
  itemRef: item // uid object - will not be cloned
}
One.put(item2)
let result = One.get(2)

result.ref === item2.ref // false item was cloned
Object.isFrozen(result.ref) // false item was cloned

result.itemRef === item // true itemRef is passed by reference - not cloned
Object.isFrozen(resut.itemRef) // true - must do One.getEdit(1) to edit itemRef

// note that this is completely safe
// get the item closest to the uid you are editing - here 1
let refResult = One.getEdit(1)
refResult.text = "test"
One.put(refResult) // will also update item2 in the cache

let item2Result = One.get(2)
item2Result === item2 // false - item2 cached was updated as a parent of item1
item2Result.itemRef.text === "test" // true

// you can still get to the previous version of item2
One.undo()
item2Result = One.get(2)
item2Result === item2 // true
item2Result.itemRef.text // undefined

Last updated