mirror of
https://github.com/nasa/openmct.git
synced 2024-12-19 21:27:52 +00:00
[Core] Add model cache
Add a cache for domain object models which prevents unnecessary reload of those objects. WTD-1033.
This commit is contained in:
parent
a640af6bf9
commit
2554f4ab01
@ -12,7 +12,6 @@
|
||||
"platform/features/plot",
|
||||
"platform/features/scrolling",
|
||||
"platform/forms",
|
||||
"platform/persistence/cache",
|
||||
"platform/persistence/queue",
|
||||
"platform/persistence/elastic",
|
||||
|
||||
|
@ -65,6 +65,11 @@
|
||||
"implementation": "models/PersistedModelProvider.js",
|
||||
"depends": [ "persistenceService", "$q", "PERSISTENCE_SPACE" ]
|
||||
},
|
||||
{
|
||||
"provides": "modelService",
|
||||
"type": "decorator",
|
||||
"implementation": "models/CachingModelDecorator.js"
|
||||
},
|
||||
{
|
||||
"provides": "typeService",
|
||||
"type": "provider",
|
||||
|
84
platform/core/src/models/CachingModelDecorator.js
Normal file
84
platform/core/src/models/CachingModelDecorator.js
Normal file
@ -0,0 +1,84 @@
|
||||
/*global define*/
|
||||
|
||||
define(
|
||||
[],
|
||||
function () {
|
||||
"use strict";
|
||||
|
||||
/**
|
||||
* The caching model decorator maintains a cache of loaded domain
|
||||
* object models, and ensures that duplicate models for the same
|
||||
* object are not provided.
|
||||
* @constructor
|
||||
*/
|
||||
function CachingModelDecorator(modelService) {
|
||||
var cache = {},
|
||||
cached = {};
|
||||
|
||||
// Fast-resolving promise
|
||||
function fastPromise(value) {
|
||||
return (value || {}).then ? value : {
|
||||
then: function (callback) {
|
||||
return fastPromise(callback(value));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// Store this model in the cache
|
||||
function cacheModel(id, model) {
|
||||
cache[id] = model;
|
||||
cached[id] = true;
|
||||
}
|
||||
|
||||
// Check if an id is not in cache, for lookup filtering
|
||||
function notCached(id) {
|
||||
return !cached[id];
|
||||
}
|
||||
|
||||
// Store the provided models in our cache
|
||||
function cacheAll(models) {
|
||||
Object.keys(models).forEach(function (id) {
|
||||
cacheModel(id, models[id]);
|
||||
});
|
||||
}
|
||||
|
||||
// Expose the cache (for promise chaining)
|
||||
function giveCache() {
|
||||
return cache;
|
||||
}
|
||||
|
||||
return {
|
||||
/**
|
||||
* Get models for these specified string identifiers.
|
||||
* These will be given as an object containing keys
|
||||
* and values, where keys are object identifiers and
|
||||
* values are models.
|
||||
* This result may contain either a subset or a
|
||||
* superset of the total objects.
|
||||
*
|
||||
* @param {Array<string>} ids the string identifiers for
|
||||
* models of interest.
|
||||
* @returns {Promise<object>} a promise for an object
|
||||
* containing key-value pairs, where keys are
|
||||
* ids and values are models
|
||||
* @method
|
||||
*/
|
||||
getModels: function (ids) {
|
||||
var neededIds = ids.filter(notCached);
|
||||
|
||||
// Look up if we have unknown IDs
|
||||
if (neededIds.length > 0) {
|
||||
return modelService.getModels(neededIds)
|
||||
.then(cacheAll)
|
||||
.then(giveCache);
|
||||
}
|
||||
|
||||
// Otherwise, just expose the cache directly
|
||||
return fastPromise(cache);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
return CachingModelDecorator;
|
||||
}
|
||||
);
|
Loading…
Reference in New Issue
Block a user