[Telemetry] Separate out delegator

Separate out handling of delegation related to telemetry,
WTD-806.
This commit is contained in:
Victor Woeltjen
2015-04-16 16:01:51 -07:00
parent d16340ac4e
commit 774c4dec1d
3 changed files with 84 additions and 20 deletions

View File

@ -0,0 +1,45 @@
/*global define*/
define(
[],
function () {
"use strict";
/**
* Used to handle telemetry delegation associated with a
* given domain object.
*/
function TelemetryDelegator($q) {
return {
/**
* Promise telemetry-providing objects associated with
* this domain object (either the domain object itself,
* or the objects it delegates)
* @returns {Promise.<DomainObject[]>} domain objects with
* a telemetry capability
*/
promiseTelemetryObjects: function (domainObject) {
// If object has been cleared, there are no relevant
// telemetry-providing domain objects.
if (!domainObject) {
return $q.when([]);
}
// Otherwise, try delegation first, and attach the
// object itself if it has a telemetry capability.
return $q.when(domainObject.useCapability(
"delegation",
"telemetry"
)).then(function (result) {
var head = domainObject.hasCapability("telemetry") ?
[ domainObject ] : [],
tail = result || [];
return head.concat(tail);
});
}
};
}
return TelemetryDelegator;
}
);

View File

@ -0,0 +1,34 @@
/*global define*/
define(
['./TelemetryDelegator'],
function (TelemetryDelegator) {
"use strict";
/**
* A TelemetryRequester provides an easy interface to request
* telemetry associated with a set of domain objects.
*
* @constructor
* @param $q Angular's $q
*/
function TelemetryRequester($q) {
var delegator = new TelemetryDelegator($q);
// Look up domain objects which have telemetry capabilities.
// This will either be the object in view, or object that
// this object delegates its telemetry capability to.
function promiseRelevantObjects(domainObject) {
return delegator.promiseTelemetryObjects(domainObject);
}
return {
};
}
return TelemetryRequester;
}
);

View File

@ -1,8 +1,8 @@
/*global define*/ /*global define*/
define( define(
['./TelemetryQueue', './TelemetryTable'], ['./TelemetryQueue', './TelemetryTable', './TelemetryDelegator'],
function (TelemetryQueue, TelemetryTable) { function (TelemetryQueue, TelemetryTable, TelemetryDelegator) {
"use strict"; "use strict";
@ -31,7 +31,8 @@ define(
* the callback once, with access to the latest data * the callback once, with access to the latest data
*/ */
function TelemetrySubscription($q, $timeout, domainObject, callback, lossless) { function TelemetrySubscription($q, $timeout, domainObject, callback, lossless) {
var unsubscribePromise, var delegator = new TelemetryDelegator($q),
unsubscribePromise,
latestValues = {}, latestValues = {},
telemetryObjects = [], telemetryObjects = [],
pool = lossless ? new TelemetryQueue() : new TelemetryTable(), pool = lossless ? new TelemetryQueue() : new TelemetryTable(),
@ -42,23 +43,7 @@ define(
// This will either be the object in view, or object that // This will either be the object in view, or object that
// this object delegates its telemetry capability to. // this object delegates its telemetry capability to.
function promiseRelevantObjects(domainObject) { function promiseRelevantObjects(domainObject) {
// If object has been cleared, there are no relevant return delegator.promiseTelemetryObjects(domainObject);
// telemetry-providing domain objects.
if (!domainObject) {
return $q.when([]);
}
// Otherwise, try delegation first, and attach the
// object itself if it has a telemetry capability.
return $q.when(domainObject.useCapability(
"delegation",
"telemetry"
)).then(function (result) {
var head = domainObject.hasCapability("telemetry") ?
[ domainObject ] : [],
tail = result || [];
return head.concat(tail);
});
} }
function updateValuesFromPool() { function updateValuesFromPool() {