mirror of
https://github.com/nasa/openmct.git
synced 2025-06-19 15:43:48 +00:00
[Persistence] Reuse instances in cache
Reuse/update a single object instance in the persistence cache, such that updates can be trivially reflected everywhere. WTD-791.
This commit is contained in:
@ -21,12 +21,40 @@ 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
|
||||||
|
|
||||||
|
// Update the cached instance of an object to a new value
|
||||||
|
function replaceValue(valueHolder, newValue) {
|
||||||
|
var v = valueHolder.value;
|
||||||
|
|
||||||
|
// If it's a JS object, we want to replace contents, so that
|
||||||
|
// everybody gets the same instance.
|
||||||
|
if (typeof v === 'object' && v !== null) {
|
||||||
|
// Only update contents if these are different instances
|
||||||
|
if (v !== newValue) {
|
||||||
|
// Clear prior contents
|
||||||
|
Object.keys(v).forEach(function (k) {
|
||||||
|
delete v[k];
|
||||||
|
});
|
||||||
|
// Shallow-copy contents
|
||||||
|
Object.keys(newValue).forEach(function (k) {
|
||||||
|
v[k] = newValue[k];
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Otherwise, just store the new value
|
||||||
|
valueHolder.value = newValue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 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]) {
|
||||||
|
if (cache[space][key]) {
|
||||||
|
replaceValue(cache[space][key], value);
|
||||||
|
} else {
|
||||||
cache[space][key] = { value: value };
|
cache[space][key] = { value: value };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Create a function for putting value into a cache;
|
// Create a function for putting value into a cache;
|
||||||
// useful for then-chaining.
|
// useful for then-chaining.
|
||||||
|
Reference in New Issue
Block a user