mirror of
https://github.com/nasa/openmct.git
synced 2025-05-12 13:33:14 +00:00
[Persistence] Load models from multiple spaces
This commit is contained in:
parent
8759fdbd95
commit
ba6e542d08
@ -63,7 +63,12 @@
|
|||||||
"provides": "modelService",
|
"provides": "modelService",
|
||||||
"type": "provider",
|
"type": "provider",
|
||||||
"implementation": "models/PersistedModelProvider.js",
|
"implementation": "models/PersistedModelProvider.js",
|
||||||
"depends": [ "persistenceService", "$q", "PERSISTENCE_SPACE" ]
|
"depends": [
|
||||||
|
"persistenceService",
|
||||||
|
"$q",
|
||||||
|
"PERSISTENCE_SPACE",
|
||||||
|
"ADDITIONAL_PERSISTENCE_SPACES"
|
||||||
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"provides": "modelService",
|
"provides": "modelService",
|
||||||
@ -215,6 +220,17 @@
|
|||||||
"composition": []
|
"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
|
* @param {PersistenceService} persistenceService the service in which
|
||||||
* domain object models are persisted.
|
* domain object models are persisted.
|
||||||
* @param $q Angular's $q service, for working with promises
|
* @param $q Angular's $q service, for working with promises
|
||||||
* @param {string} SPACE the name of the persistence space from which
|
* @param {string} space the name of the persistence space(s)
|
||||||
* models should be retrieved.
|
* 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.persistenceService = persistenceService;
|
||||||
this.$q = $q;
|
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) {
|
PersistedModelProvider.prototype.getModels = function (ids) {
|
||||||
var persistenceService = this.persistenceService,
|
var persistenceService = this.persistenceService,
|
||||||
$q = this.$q,
|
$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) {
|
function loadModel(id) {
|
||||||
|
return $q.all(spaces.map(function (space) {
|
||||||
return persistenceService.readObject(space, id);
|
return persistenceService.readObject(space, id);
|
||||||
|
})).then(function (models) {
|
||||||
|
return models.reduce(takeMostRecent);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Package the result as id->model
|
// Package the result as id->model
|
||||||
|
Loading…
x
Reference in New Issue
Block a user