[Telemetry] Listen for mutation

Listen for mutation from telemetry subscriptions,
WTD-1329.
This commit is contained in:
Victor Woeltjen
2015-06-24 12:24:43 -07:00
parent e0f672d40d
commit 7a531493d8

View File

@ -59,6 +59,7 @@ define(
telemetryObjects = [], telemetryObjects = [],
pool = lossless ? new TelemetryQueue() : new TelemetryTable(), pool = lossless ? new TelemetryQueue() : new TelemetryTable(),
metadatas, metadatas,
unlistenToMutation,
updatePending; updatePending;
// Look up domain objects which have telemetry capabilities. // Look up domain objects which have telemetry capabilities.
@ -153,6 +154,7 @@ define(
return objects; return objects;
} }
function initialize() {
// Get a reference to relevant objects (those with telemetry // Get a reference to relevant objects (those with telemetry
// capabilities) and subscribe to their telemetry updates. // capabilities) and subscribe to their telemetry updates.
// Keep a reference to their promised return values, as these // Keep a reference to their promised return values, as these
@ -163,6 +165,30 @@ define(
unsubscribePromise = telemetryObjectPromise unsubscribePromise = telemetryObjectPromise
.then(cacheObjectReferences) .then(cacheObjectReferences)
.then(subscribeAll); .then(subscribeAll);
}
function idsMatch(ids) {
return ids.every(function (id, index) {
return telemetryObjects[index].getId() === id;
});
}
function modelChange(model) {
if (!idsMatch((model || {}).composition || [])) {
// Reinitialize if composition has changed
initialize();
}
}
function addMutationListener() {
var mutation = domainObject.getCapability('mutation');
if (mutation) {
return mutation.listen(modelChange);
}
}
initialize();
unlistenToMutation = addMutationListener();
return { return {
/** /**
@ -172,6 +198,9 @@ define(
* @memberof TelemetrySubscription * @memberof TelemetrySubscription
*/ */
unsubscribe: function () { unsubscribe: function () {
if (unlistenToMutation) {
unlistenToMutation();
}
return unsubscribePromise.then(function (unsubscribes) { return unsubscribePromise.then(function (unsubscribes) {
return $q.all(unsubscribes.map(function (unsubscribe) { return $q.all(unsubscribes.map(function (unsubscribe) {
return unsubscribe(); return unsubscribe();