[API] Support telemetry request strategies

https://github.com/nasa/openmct/pull/1212#discussion_r81880741
This commit is contained in:
Victor Woeltjen 2016-10-06 18:00:32 -07:00
parent 13b5e7c00e
commit fbe9621387

View File

@ -156,7 +156,8 @@ define([
* @memberof module:openmct * @memberof module:openmct
*/ */
function TelemetryAPI() { function TelemetryAPI() {
this.providers = []; this.providersByStrategy = {};
this.defaultProviders = [];
} }
/** /**
@ -170,7 +171,7 @@ define([
* @memberof module:openmct.TelemetryAPI~TelemetryProvider# * @memberof module:openmct.TelemetryAPI~TelemetryProvider#
*/ */
TelemetryAPI.prototype.canProvideTelemetry = function (domainObject) { TelemetryAPI.prototype.canProvideTelemetry = function (domainObject) {
return this.providers.some(function (provider) { return this.defaultProviders.some(function (provider) {
return provider.canProvideTelemetry(domainObject); return provider.canProvideTelemetry(domainObject);
}); });
}; };
@ -187,8 +188,36 @@ define([
* default provider (when no strategy is requested or no * default provider (when no strategy is requested or no
* matching strategy is found.) * matching strategy is found.)
*/ */
TelemetryAPI.prototype.addProvider = function (provider) { TelemetryAPI.prototype.addProvider = function (provider, strategy) {
this.providers.push(provider); if (!strategy) {
this.defaultProviders.push(provider);
} else {
this.providersByStrategy[strategy] =
this.providersByStrategy[strategy] || [];
this.providersByStrategy[strategy].push(provider);
}
};
/**
* @private
*/
TelemetryAPI.prototype.findProvider = function (domainObject, options) {
var strategy = options.strategy;
function supportsDomainObject(provider) {
return provider.canProvideTelemetry(domainObject);
}
if (strategy) {
var eligibleProviders =
(this.providersByStrategy[strategy] || [])
.filter(supportsDomainObject);
if (eligibleProviders.length > 0) {
return eligibleProviders[0];
}
}
return this.defaultProviders.filter(supportsDomainObject)[0];
}; };
/** /**
@ -207,10 +236,7 @@ define([
* telemetry data * telemetry data
*/ */
TelemetryAPI.prototype.request = function (domainObject, options) { TelemetryAPI.prototype.request = function (domainObject, options) {
var provider = this.providers.reduce(function (result, p) { var provider = this.findProvider(domainObject, options);
return (p.canProvideTelemetry(domainObject)) ?
p : result;
}, undefined);
return provider ? return provider ?
provider.request(domainObject, options) : provider.request(domainObject, options) :
Promise.reject([]); Promise.reject([]);
@ -278,17 +304,13 @@ define([
* @memberof module:openmct.TelemetryAPI~TelemetryProvider# * @memberof module:openmct.TelemetryAPI~TelemetryProvider#
*/ */
_.forEach({ _.forEach({
request: undefined,
subscribe: undefined, subscribe: undefined,
properties: [], properties: [],
formatter: undefined, formatter: undefined,
limitEvaluator: undefined limitEvaluator: undefined
}, function (defaultValue, method) { }, function (defaultValue, method) {
TelemetryAPI.prototype[method] = function (domainObject) { TelemetryAPI.prototype[method] = function (domainObject) {
var provider = this.providers.reduce(function (result, p) { var provider = this.findProvider(domainObject, options);
return (p.canProvideTelemetry(domainObject)) ?
p : result;
}, undefined);
return provider ? return provider ?
provider[method].apply(provider, arguments) : provider[method].apply(provider, arguments) :
defaultValue; defaultValue;