Merge remote-tracking branch 'origin/open1329' into open1343
This commit is contained in:
@@ -59,6 +59,7 @@ define(
|
||||
telemetryObjects = [],
|
||||
pool = lossless ? new TelemetryQueue() : new TelemetryTable(),
|
||||
metadatas,
|
||||
unlistenToMutation,
|
||||
updatePending;
|
||||
|
||||
// Look up domain objects which have telemetry capabilities.
|
||||
@@ -146,23 +147,59 @@ define(
|
||||
telemetryObjects = objects;
|
||||
metadatas = objects.map(lookupMetadata);
|
||||
// Fire callback, as this will be the first time that
|
||||
// telemetry objects are available
|
||||
// telemetry objects are available, or these objects
|
||||
// will have changed.
|
||||
if (callback) {
|
||||
callback();
|
||||
}
|
||||
return objects;
|
||||
}
|
||||
|
||||
// Get a reference to relevant objects (those with telemetry
|
||||
// capabilities) and subscribe to their telemetry updates.
|
||||
// Keep a reference to their promised return values, as these
|
||||
// will be unsubscribe functions. (This must be a promise
|
||||
// because delegation is supported, and retrieving delegate
|
||||
// telemetry-capable objects may be an asynchronous operation.)
|
||||
telemetryObjectPromise = promiseRelevantObjects(domainObject);
|
||||
unsubscribePromise = telemetryObjectPromise
|
||||
.then(cacheObjectReferences)
|
||||
.then(subscribeAll);
|
||||
function unsubscribeAll() {
|
||||
return unsubscribePromise.then(function (unsubscribes) {
|
||||
return $q.all(unsubscribes.map(function (unsubscribe) {
|
||||
return unsubscribe();
|
||||
}));
|
||||
});
|
||||
}
|
||||
|
||||
function initialize() {
|
||||
// Get a reference to relevant objects (those with telemetry
|
||||
// capabilities) and subscribe to their telemetry updates.
|
||||
// Keep a reference to their promised return values, as these
|
||||
// will be unsubscribe functions. (This must be a promise
|
||||
// because delegation is supported, and retrieving delegate
|
||||
// telemetry-capable objects may be an asynchronous operation.)
|
||||
telemetryObjectPromise = promiseRelevantObjects(domainObject);
|
||||
unsubscribePromise = telemetryObjectPromise
|
||||
.then(cacheObjectReferences)
|
||||
.then(subscribeAll);
|
||||
}
|
||||
|
||||
function idsMatch(ids) {
|
||||
return ids.length === telemetryObjects.length &&
|
||||
ids.every(function (id, index) {
|
||||
return telemetryObjects[index].getId() === id;
|
||||
});
|
||||
}
|
||||
|
||||
function modelChange(model) {
|
||||
if (!idsMatch((model || {}).composition || [])) {
|
||||
// Reinitialize if composition has changed
|
||||
unsubscribeAll().then(initialize);
|
||||
}
|
||||
}
|
||||
|
||||
function addMutationListener() {
|
||||
var mutation = domainObject &&
|
||||
domainObject.getCapability('mutation');
|
||||
if (mutation) {
|
||||
return mutation.listen(modelChange);
|
||||
}
|
||||
}
|
||||
|
||||
initialize();
|
||||
unlistenToMutation = addMutationListener();
|
||||
|
||||
return {
|
||||
/**
|
||||
@@ -172,11 +209,10 @@ define(
|
||||
* @memberof TelemetrySubscription
|
||||
*/
|
||||
unsubscribe: function () {
|
||||
return unsubscribePromise.then(function (unsubscribes) {
|
||||
return $q.all(unsubscribes.map(function (unsubscribe) {
|
||||
return unsubscribe();
|
||||
}));
|
||||
});
|
||||
if (unlistenToMutation) {
|
||||
unlistenToMutation();
|
||||
}
|
||||
return unsubscribeAll();
|
||||
},
|
||||
/**
|
||||
* Get the most recent domain value that has been observed
|
||||
@@ -264,4 +300,4 @@ define(
|
||||
return TelemetrySubscription;
|
||||
|
||||
}
|
||||
);
|
||||
);
|
||||
|
||||
@@ -32,7 +32,9 @@ define(
|
||||
mockDomainObject,
|
||||
mockCallback,
|
||||
mockTelemetry,
|
||||
mockMutation,
|
||||
mockUnsubscribe,
|
||||
mockUnlisten,
|
||||
mockSeries,
|
||||
testMetadata,
|
||||
subscription;
|
||||
@@ -59,7 +61,12 @@ define(
|
||||
"telemetry",
|
||||
["subscribe", "getMetadata"]
|
||||
);
|
||||
mockMutation = jasmine.createSpyObj(
|
||||
"mutation",
|
||||
["mutate", "listen"]
|
||||
);
|
||||
mockUnsubscribe = jasmine.createSpy("unsubscribe");
|
||||
mockUnlisten = jasmine.createSpy("unlisten");
|
||||
mockSeries = jasmine.createSpyObj(
|
||||
"series",
|
||||
[ "getPointCount", "getDomainValue", "getRangeValue" ]
|
||||
@@ -68,12 +75,19 @@ define(
|
||||
mockQ.when.andCallFake(mockPromise);
|
||||
|
||||
mockDomainObject.hasCapability.andReturn(true);
|
||||
mockDomainObject.getCapability.andReturn(mockTelemetry);
|
||||
mockDomainObject.getCapability.andCallFake(function (c) {
|
||||
return {
|
||||
telemetry: mockTelemetry,
|
||||
mutation: mockMutation
|
||||
}[c];
|
||||
});
|
||||
mockDomainObject.getId.andReturn('test-id');
|
||||
|
||||
mockTelemetry.subscribe.andReturn(mockUnsubscribe);
|
||||
mockTelemetry.getMetadata.andReturn(testMetadata);
|
||||
|
||||
mockMutation.listen.andReturn(mockUnlisten);
|
||||
|
||||
mockSeries.getPointCount.andReturn(42);
|
||||
mockSeries.getDomainValue.andReturn(123456);
|
||||
mockSeries.getRangeValue.andReturn(789);
|
||||
@@ -213,6 +227,22 @@ define(
|
||||
expect(mockCallback2)
|
||||
.toHaveBeenCalledWith([ mockDomainObject ]);
|
||||
});
|
||||
|
||||
it("reinitializes on mutation", function () {
|
||||
expect(mockTelemetry.subscribe.calls.length).toEqual(1);
|
||||
// Notify of a mutation which appears to change composition
|
||||
mockMutation.listen.mostRecentCall.args[0]({
|
||||
composition: ['Z']
|
||||
});
|
||||
// Use subscribe call as an indication of reinitialization
|
||||
expect(mockTelemetry.subscribe.calls.length).toEqual(2);
|
||||
});
|
||||
|
||||
it("stops listening for mutation on unsubscribe", function () {
|
||||
expect(mockUnlisten).not.toHaveBeenCalled();
|
||||
subscription.unsubscribe();
|
||||
expect(mockUnlisten).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
}
|
||||
);
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user