[Compat] model service adapter

Provide adapter that ensures model service can fetch models
provided via new API.

Fixes https://github.com/nasa/openmct/issues/1279
This commit is contained in:
Pete Richards 2016-10-27 11:56:57 -07:00
parent 8cacff37ab
commit 81de6119fe
2 changed files with 85 additions and 0 deletions

View File

@ -25,6 +25,7 @@ define([
'./actions/ActionDialogDecorator',
'./directives/MCTView',
'./services/Instantiate',
'./services/MissingModelCompatibilityDecorator',
'./capabilities/APICapabilityDecorator',
'./policies/AdapterCompositionPolicy',
'./runs/AlternateCompositionInitializer'
@ -33,6 +34,7 @@ define([
ActionDialogDecorator,
MCTView,
Instantiate,
MissingModelCompatibilityDecorator,
APICapabilityDecorator,
AdapterCompositionPolicy,
AlternateCompositionInitializer
@ -75,6 +77,12 @@ define([
provides: "actionService",
implementation: ActionDialogDecorator,
depends: ["openmct"]
},
{
type: "decorator",
provides: "modelService",
implementation: MissingModelCompatibilityDecorator,
depends: ["openmct"]
}
],
policies: [

View File

@ -0,0 +1,77 @@
/*****************************************************************************
* Open MCT, Copyright (c) 2014-2016, United States Government
* as represented by the Administrator of the National Aeronautics and Space
* Administration. All rights reserved.
*
* Open MCT is licensed under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* Open MCT includes source code licensed under additional open source
* licenses. See the Open Source Licenses file (LICENSES.md) included with
* this source code distribution or the Licensing information page available
* at runtime from the About dialog for additional information.
*****************************************************************************/
define([
'../../api/objects/object-utils'
], function (
objectUtils
) {
function MissingModelCompatibilityDecorator(api, modelService) {
this.api = api;
this.modelService = modelService;
this.apiFetching = {}; // to prevent loops, if we have already
}
MissingModelCompatibilityDecorator.prototype.apiFetch = function (ids) {
var results = {},
promises = ids.map(function (id) {
if (this.apiFetching[id]) {
return Promise.resolve();
}
this.apiFetching[id] = true;
return this.api.objects.get(objectUtils.parseKeyString(id))
.then(function (newDO) {
results[id] = objectUtils.toOldFormat(newDO);
});
}, this);
return Promise.all(promises).then(function () {
return results;
});
};
MissingModelCompatibilityDecorator.prototype.getModels = function (ids) {
return this.modelService.getModels(ids)
.then(function (models) {
var missingIds = ids.filter(function (id) {
return !models[id];
});
if (!missingIds.length) {
return models;
}
return this.apiFetch(missingIds)
.then(function (apiResults) {
Object.keys(apiResults).forEach(function (k) {
models[k] = apiResults[k];
});
return models;
});
}.bind(this));
};
return MissingModelCompatibilityDecorator;
});