mirror of
https://github.com/nasa/openmct.git
synced 2025-03-21 11:35:59 +00:00
[Persistence] Load models from multiple spaces
This commit is contained in:
parent
8759fdbd95
commit
ba6e542d08
@ -63,7 +63,12 @@
|
||||
"provides": "modelService",
|
||||
"type": "provider",
|
||||
"implementation": "models/PersistedModelProvider.js",
|
||||
"depends": [ "persistenceService", "$q", "PERSISTENCE_SPACE" ]
|
||||
"depends": [
|
||||
"persistenceService",
|
||||
"$q",
|
||||
"PERSISTENCE_SPACE",
|
||||
"ADDITIONAL_PERSISTENCE_SPACES"
|
||||
]
|
||||
},
|
||||
{
|
||||
"provides": "modelService",
|
||||
@ -215,6 +220,17 @@
|
||||
"composition": []
|
||||
}
|
||||
}
|
||||
],
|
||||
"constants": [
|
||||
{
|
||||
"key": "PERSISTENCE_SPACE",
|
||||
"value": "mct"
|
||||
},
|
||||
{
|
||||
"key": "ADDITIONAL_PERSISTENCE_SPACES",
|
||||
"value": [],
|
||||
"description": "An array of additional persistence spaces to load models from."
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
|
@ -39,23 +39,37 @@ define(
|
||||
* @param {PersistenceService} persistenceService the service in which
|
||||
* domain object models are persisted.
|
||||
* @param $q Angular's $q service, for working with promises
|
||||
* @param {string} SPACE the name of the persistence space from which
|
||||
* models should be retrieved.
|
||||
* @param {string} space the name of the persistence space(s)
|
||||
* from which models should be retrieved.
|
||||
* @param {string} spaces additional persistence spaces to use
|
||||
*/
|
||||
function PersistedModelProvider(persistenceService, $q, space) {
|
||||
function PersistedModelProvider(persistenceService, $q, space, spaces) {
|
||||
this.persistenceService = persistenceService;
|
||||
this.$q = $q;
|
||||
this.space = space;
|
||||
this.spaces = [space].concat(spaces || []);
|
||||
}
|
||||
|
||||
// Take the most recently modified model, for cases where
|
||||
// multiple persistence spaces return models.
|
||||
function takeMostRecent(modelA, modelB) {
|
||||
return (!modelA || modelA.modified === undefined) ? modelB :
|
||||
(!modelB || modelB.modified === undefined) ? modelA :
|
||||
modelA.modified > modelB.modified ? modelA :
|
||||
modelB;
|
||||
}
|
||||
|
||||
PersistedModelProvider.prototype.getModels = function (ids) {
|
||||
var persistenceService = this.persistenceService,
|
||||
$q = this.$q,
|
||||
space = this.space;
|
||||
spaces = this.spaces;
|
||||
|
||||
// Load a single object model from persistence
|
||||
// Load a single object model from any persistence spaces
|
||||
function loadModel(id) {
|
||||
return persistenceService.readObject(space, id);
|
||||
return $q.all(spaces.map(function (space) {
|
||||
return persistenceService.readObject(space, id);
|
||||
})).then(function (models) {
|
||||
return models.reduce(takeMostRecent);
|
||||
});
|
||||
}
|
||||
|
||||
// Package the result as id->model
|
||||
|
Loading…
x
Reference in New Issue
Block a user