mirror of
https://github.com/nasa/openmct.git
synced 2025-01-03 03:46:42 +00:00
Merge pull request #1301 from nasa/search-indexing-1279
Add modelService compatibility adapater
This commit is contained in:
commit
736c89cfc6
@ -25,6 +25,7 @@ define([
|
|||||||
'./actions/ActionDialogDecorator',
|
'./actions/ActionDialogDecorator',
|
||||||
'./directives/MCTView',
|
'./directives/MCTView',
|
||||||
'./services/Instantiate',
|
'./services/Instantiate',
|
||||||
|
'./services/MissingModelCompatibilityDecorator',
|
||||||
'./capabilities/APICapabilityDecorator',
|
'./capabilities/APICapabilityDecorator',
|
||||||
'./policies/AdapterCompositionPolicy',
|
'./policies/AdapterCompositionPolicy',
|
||||||
'./runs/AlternateCompositionInitializer'
|
'./runs/AlternateCompositionInitializer'
|
||||||
@ -33,6 +34,7 @@ define([
|
|||||||
ActionDialogDecorator,
|
ActionDialogDecorator,
|
||||||
MCTView,
|
MCTView,
|
||||||
Instantiate,
|
Instantiate,
|
||||||
|
MissingModelCompatibilityDecorator,
|
||||||
APICapabilityDecorator,
|
APICapabilityDecorator,
|
||||||
AdapterCompositionPolicy,
|
AdapterCompositionPolicy,
|
||||||
AlternateCompositionInitializer
|
AlternateCompositionInitializer
|
||||||
@ -75,6 +77,12 @@ define([
|
|||||||
provides: "actionService",
|
provides: "actionService",
|
||||||
implementation: ActionDialogDecorator,
|
implementation: ActionDialogDecorator,
|
||||||
depends: ["openmct"]
|
depends: ["openmct"]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: "decorator",
|
||||||
|
provides: "modelService",
|
||||||
|
implementation: MissingModelCompatibilityDecorator,
|
||||||
|
depends: ["openmct"]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
policies: [
|
policies: [
|
||||||
|
94
src/adapter/services/MissingModelCompatibilityDecorator.js
Normal file
94
src/adapter/services/MissingModelCompatibilityDecorator.js
Normal file
@ -0,0 +1,94 @@
|
|||||||
|
/*****************************************************************************
|
||||||
|
* 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
|
||||||
|
) {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Compatibility decorator for New API.
|
||||||
|
*
|
||||||
|
* When the model service returns no results, this attempts to load
|
||||||
|
* the model from the new Object API and returns that instead. In order
|
||||||
|
* to prevent infinite recursion, this only tries to fetch from the API
|
||||||
|
* a single time.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
function MissingModelCompatibilityDecorator(api, modelService) {
|
||||||
|
this.api = api;
|
||||||
|
this.modelService = modelService;
|
||||||
|
this.apiFetching = {}; // to prevent loops, if we have already
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetch a set of ids from the public api and return a promise for their
|
||||||
|
* models. If a model is requested twice, respond with a missing result.
|
||||||
|
*/
|
||||||
|
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;
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return a promise for model results based on provided ids. Will attempt
|
||||||
|
* to fetch any missing results from the object api.
|
||||||
|
*/
|
||||||
|
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;
|
||||||
|
});
|
||||||
|
|
@ -150,10 +150,6 @@ define([
|
|||||||
throw new Error('Provider does not support [' + method + '].');
|
throw new Error('Provider does not support [' + method + '].');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (method === 'get') {
|
|
||||||
return provider.get(identifier.key);
|
|
||||||
}
|
|
||||||
|
|
||||||
return provider[method].apply(provider, arguments);
|
return provider[method].apply(provider, arguments);
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user