mirror of
https://github.com/nasa/openmct.git
synced 2025-01-12 07:52:42 +00:00
[Core] Update models in-place
In model cache, update cached model instances instead of replacing them; this avoids situations where two different model instances escape the model service because the second request was made before the results from the first had been cached. WTD-1033.
This commit is contained in:
parent
ad3bb355dd
commit
7e5d363daa
@ -15,6 +15,37 @@ define(
|
|||||||
var cache = {},
|
var cache = {},
|
||||||
cached = {};
|
cached = {};
|
||||||
|
|
||||||
|
// Update the cached instance of a model to a new value.
|
||||||
|
// We update in-place to ensure there is only ever one instance
|
||||||
|
// of any given model exposed by the modelService as a whole.
|
||||||
|
function updateModel(id, model) {
|
||||||
|
var oldModel = cache[id];
|
||||||
|
|
||||||
|
// Same object instance is a possibility, so don't copy
|
||||||
|
if (oldModel === model) {
|
||||||
|
return model;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If we'd previously cached an undefined value, or are now
|
||||||
|
// seeing undefined, replace the item in the cache entirely.
|
||||||
|
if (oldModel === undefined || model === undefined) {
|
||||||
|
cache[id] = model;
|
||||||
|
return model;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Otherwise, empty out the old model...
|
||||||
|
Object.keys(oldModel).forEach(function (k) {
|
||||||
|
delete oldModel[k];
|
||||||
|
});
|
||||||
|
|
||||||
|
// ...and replace it with the contents of the new model.
|
||||||
|
Object.keys(model).forEach(function (k) {
|
||||||
|
oldModel[k] = model[k];
|
||||||
|
});
|
||||||
|
|
||||||
|
return oldModel;
|
||||||
|
}
|
||||||
|
|
||||||
// Fast-resolving promise
|
// Fast-resolving promise
|
||||||
function fastPromise(value) {
|
function fastPromise(value) {
|
||||||
return (value || {}).then ? value : {
|
return (value || {}).then ? value : {
|
||||||
@ -26,7 +57,7 @@ define(
|
|||||||
|
|
||||||
// Store this model in the cache
|
// Store this model in the cache
|
||||||
function cacheModel(id, model) {
|
function cacheModel(id, model) {
|
||||||
cache[id] = model;
|
cache[id] = cached[id] ? updateModel(id, model) : model;
|
||||||
cached[id] = true;
|
cached[id] = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user