mirror of
https://github.com/nasa/openmct.git
synced 2024-12-18 20:57:53 +00:00
Added bridge between old and new event models
This commit is contained in:
parent
10e90519c0
commit
bd3c6665fb
@ -1,11 +1,11 @@
|
||||
define([
|
||||
'./object-utils',
|
||||
'./ObjectAPI',
|
||||
'./objectEventBus'
|
||||
'./objectEventEmitter'
|
||||
], function (
|
||||
utils,
|
||||
ObjectAPI,
|
||||
objectEventBus
|
||||
objectEventEmitter
|
||||
) {
|
||||
function ObjectServiceProvider(objectService, instantiate, topic) {
|
||||
this.objectService = objectService;
|
||||
@ -13,30 +13,42 @@ define([
|
||||
|
||||
this.topicService = topic;
|
||||
this.generalTopic = topic('mutation');
|
||||
this.bridgeEventBuses(this.topicService, this.generalTopic);
|
||||
this.bridgeEventBuses();
|
||||
}
|
||||
|
||||
/**
|
||||
* Bridges old and new style mutation events to provide compatibility between the two APIs
|
||||
* @private
|
||||
*/
|
||||
ObjectServiceProvider.prototype.bridgeEventBuses = function (topicService, generalTopic) {
|
||||
ObjectServiceProvider.prototype.bridgeEventBuses = function () {
|
||||
var generalTopicListener;
|
||||
|
||||
function handleMutation(newStyleObject) {
|
||||
var oldStyleObject = utils.toOldFormat(newStyleObject);
|
||||
var specificTopic = topicService("mutation:" + oldStyleObject.getId());
|
||||
var handleMutation = function (newStyleObject) {
|
||||
var keyString = utils.makeKeyString(newStyleObject.key);
|
||||
var oldStyleObject = this.instantiate(utils.toOldFormat(newStyleObject), keyString);
|
||||
var specificTopic = this.topicService("mutation:" + keyString);
|
||||
|
||||
generalTopic.notify(oldStyleObject);
|
||||
// Don't trigger self
|
||||
if (generalTopicListener){
|
||||
generalTopicListener();
|
||||
}
|
||||
this.generalTopic.notify(oldStyleObject);
|
||||
specificTopic.notify(oldStyleObject.getModel());
|
||||
}
|
||||
|
||||
function handleLegacyMutation(legacyObject){
|
||||
generalTopicListener = this.generalTopic.listen(handleLegacyMutation);
|
||||
}.bind(this);
|
||||
|
||||
var handleLegacyMutation = function (legacyObject){
|
||||
var newStyleObject = utils.toNewFormat(legacyObject.getModel(), legacyObject.getId());
|
||||
objectEventBus.emit(newStyleObject.key.identifier + ":*", newStyleObject);
|
||||
}
|
||||
|
||||
objectEventBus.on('mutation', handleMutation)
|
||||
generalTopic.listen(handleLegacyMutation);
|
||||
//Don't trigger self
|
||||
objectEventEmitter.off('mutation', handleMutation);
|
||||
objectEventEmitter.emit(newStyleObject.key.identifier + ":*", newStyleObject);
|
||||
objectEventEmitter.on('mutation', handleMutation);
|
||||
}.bind(this);
|
||||
|
||||
objectEventEmitter.on('mutation', handleMutation);
|
||||
generalTopicListener = this.generalTopic.listen(handleLegacyMutation);
|
||||
};
|
||||
|
||||
ObjectServiceProvider.prototype.save = function (object) {
|
||||
@ -66,7 +78,7 @@ define([
|
||||
|
||||
// Injects new object API as a decorator so that it hijacks all requests.
|
||||
// Object providers implemented on new API should just work, old API should just work, many things may break.
|
||||
function LegacyObjectAPIInterceptor(ROOTS, instantiate, objectService) {
|
||||
function LegacyObjectAPIInterceptor(ROOTS, instantiate, topic, objectService) {
|
||||
this.getObjects = function (keys) {
|
||||
var results = {},
|
||||
promises = keys.map(function (keyString) {
|
||||
@ -85,7 +97,7 @@ define([
|
||||
};
|
||||
|
||||
ObjectAPI._supersecretSetFallbackProvider(
|
||||
new ObjectServiceProvider(objectService, instantiate)
|
||||
new ObjectServiceProvider(objectService, instantiate, topic)
|
||||
);
|
||||
|
||||
ROOTS.forEach(function (r) {
|
||||
|
@ -1,9 +1,13 @@
|
||||
define([
|
||||
'lodash'
|
||||
'lodash',
|
||||
'./objectEventEmitter'
|
||||
], function (
|
||||
_
|
||||
_,
|
||||
objectEventEmitter
|
||||
) {
|
||||
|
||||
var ANY_OBJECT_EVENT = "mutation";
|
||||
|
||||
/**
|
||||
* The MutableObject wraps a DomainObject and provides getters and
|
||||
* setters for
|
||||
@ -11,8 +15,7 @@ define([
|
||||
* @param object
|
||||
* @constructor
|
||||
*/
|
||||
function MutableObject(eventEmitter, object) {
|
||||
this.eventEmitter = eventEmitter;
|
||||
function MutableObject(object) {
|
||||
this.object = object;
|
||||
this.unlisteners = [];
|
||||
}
|
||||
@ -29,18 +32,22 @@ define([
|
||||
|
||||
MutableObject.prototype.on = function(path, callback) {
|
||||
var fullPath = qualifiedEventName(this.object, path);
|
||||
this.eventEmitter.on(fullPath, callback);
|
||||
this.unlisteners.push(this.eventEmitter.off.bind(this.eventEmitter, fullPath, callback));
|
||||
objectEventEmitter.on(fullPath, callback);
|
||||
this.unlisteners.push(objectEventEmitter.off.bind(objectEventEmitter, fullPath, callback));
|
||||
};
|
||||
|
||||
MutableObject.prototype.set = function (path, value) {
|
||||
|
||||
_.set(this.object, path, value);
|
||||
_.set(this.object, 'modified', Date.now());
|
||||
|
||||
//Emit event specific to property
|
||||
this.eventEmitter.emit(qualifiedEventName(this.object, path), value);
|
||||
objectEventEmitter.emit(qualifiedEventName(this.object, path), value);
|
||||
//Emit wildcare event
|
||||
this.eventEmitter.emit(qualifiedEventName(this.object, '*'), this.object);
|
||||
objectEventEmitter.emit(qualifiedEventName(this.object, '*'), this.object);
|
||||
|
||||
//Emit a general "any object" event
|
||||
objectEventEmitter.emit(ANY_OBJECT_EVENT, this.object);
|
||||
};
|
||||
|
||||
return MutableObject;
|
||||
|
@ -1,11 +1,9 @@
|
||||
define([
|
||||
'lodash',
|
||||
'EventEmitter',
|
||||
'./object-utils',
|
||||
'./MutableObject'
|
||||
], function (
|
||||
_,
|
||||
EventEmitter,
|
||||
utils,
|
||||
MutableObject
|
||||
) {
|
||||
@ -18,8 +16,7 @@ define([
|
||||
var Objects = {},
|
||||
ROOT_REGISTRY = [],
|
||||
PROVIDER_REGISTRY = {},
|
||||
FALLBACK_PROVIDER,
|
||||
eventEmitter = new EventEmitter();
|
||||
FALLBACK_PROVIDER;
|
||||
|
||||
Objects._supersecretSetFallbackProvider = function (p) {
|
||||
FALLBACK_PROVIDER = p;
|
||||
@ -83,17 +80,7 @@ define([
|
||||
};
|
||||
|
||||
Objects.getMutable = function (object) {
|
||||
var mutable = new MutableObject(eventEmitter, object);
|
||||
var id = object.key.identifier;
|
||||
var specificTopic = topic("mutation:" + id);
|
||||
|
||||
function legacyEvent (modifiedObject) {
|
||||
specificTopic.notify(utils.toOldFormat(modifiedObject));
|
||||
};
|
||||
|
||||
// Add legacy event support
|
||||
mutable.on("*", legacyEvent);
|
||||
return mutable;
|
||||
return new MutableObject(object);
|
||||
};
|
||||
|
||||
return Objects;
|
||||
|
@ -40,7 +40,8 @@ define([
|
||||
implementation: LegacyObjectAPIInterceptor,
|
||||
depends: [
|
||||
"roots[]",
|
||||
"instantiate"
|
||||
"instantiate",
|
||||
"topic"
|
||||
]
|
||||
}
|
||||
]
|
||||
|
Loading…
Reference in New Issue
Block a user