mirror of
https://github.com/nasa/openmct.git
synced 2025-06-01 23:20:50 +00:00
[Persistence] Don't clone in cache
Don't clone objects in the persistence cache; instead, ensure that one instance per object is used/given wherever possible. Goal is to keep object state in sync between visible instances for WTD-791 et al.
This commit is contained in:
parent
bc5d657015
commit
2a11204ee6
@ -21,16 +21,10 @@ define(
|
|||||||
var spaces = CACHE_SPACES || [], // List of spaces to cache
|
var spaces = CACHE_SPACES || [], // List of spaces to cache
|
||||||
cache = {}; // Where objects will be stored
|
cache = {}; // Where objects will be stored
|
||||||
|
|
||||||
// Utility function; avoid sharing one instance everywhere.
|
|
||||||
function clone(value) {
|
|
||||||
// Only clone truthy values (no need to clone undefined, false...)
|
|
||||||
return value && JSON.parse(JSON.stringify(value));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Place value in the cache for space, if there is one.
|
// Place value in the cache for space, if there is one.
|
||||||
function addToCache(space, key, value) {
|
function addToCache(space, key, value) {
|
||||||
if (cache[space]) {
|
if (cache[space]) {
|
||||||
cache[space][key] = { value: clone(value) };
|
cache[space][key] = { value: value };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -110,7 +104,7 @@ define(
|
|||||||
*/
|
*/
|
||||||
readObject: function (space, key) {
|
readObject: function (space, key) {
|
||||||
return (cache[space] && cache[space][key]) ?
|
return (cache[space] && cache[space][key]) ?
|
||||||
fastPromise(clone(cache[space][key].value)) :
|
fastPromise(cache[space][key].value) :
|
||||||
persistenceService.readObject(space, key)
|
persistenceService.readObject(space, key)
|
||||||
.then(putCache(space, key));
|
.then(putCache(space, key));
|
||||||
},
|
},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user