[API] Remove singleton event emitter

This commit is contained in:
Victor Woeltjen
2016-12-23 16:51:15 -08:00
parent 381f3d9b69
commit a5a17b9502
3 changed files with 36 additions and 23 deletions

View File

@ -21,11 +21,9 @@
*****************************************************************************/ *****************************************************************************/
define([ define([
'./object-utils', './object-utils'
'./objectEventEmitter'
], function ( ], function (
utils, utils
objectEventEmitter
) { ) {
function ObjectServiceProvider(objectService, instantiate, topic) { function ObjectServiceProvider(objectService, instantiate, topic) {
this.objectService = objectService; this.objectService = objectService;
@ -61,12 +59,12 @@ define([
var newStyleObject = utils.toNewFormat(legacyObject.getModel(), legacyObject.getId()); var newStyleObject = utils.toNewFormat(legacyObject.getModel(), legacyObject.getId());
//Don't trigger self //Don't trigger self
objectEventEmitter.off('mutation', handleMutation); this.eventEmitter.off('mutation', handleMutation);
objectEventEmitter.emit(newStyleObject.identifier.key + ":*", newStyleObject); this.eventEmitter.emit(newStyleObject.identifier.key + ":*", newStyleObject);
objectEventEmitter.on('mutation', handleMutation); this.eventEmitter.on('mutation', handleMutation);
}.bind(this); }.bind(this);
objectEventEmitter.on('mutation', handleMutation); this.eventEmitter.on('mutation', handleMutation);
removeGeneralTopicListener = this.generalTopic.listen(handleLegacyMutation); removeGeneralTopicListener = this.generalTopic.listen(handleLegacyMutation);
}; };
@ -96,6 +94,8 @@ define([
// Injects new object API as a decorator so that it hijacks all requests. // 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. // Object providers implemented on new API should just work, old API should just work, many things may break.
function LegacyObjectAPIInterceptor(openmct, ROOTS, instantiate, topic, objectService) { function LegacyObjectAPIInterceptor(openmct, ROOTS, instantiate, topic, objectService) {
var eventEmitter = openmct.objects.eventEmitter;
this.getObjects = function (keys) { this.getObjects = function (keys) {
var results = {}, var results = {},
promises = keys.map(function (keyString) { promises = keys.map(function (keyString) {
@ -114,7 +114,12 @@ define([
}; };
openmct.objects.supersecretSetFallbackProvider( openmct.objects.supersecretSetFallbackProvider(
new ObjectServiceProvider(objectService, instantiate, topic) new ObjectServiceProvider(
eventEmitter,
objectService,
instantiate,
topic
)
); );
ROOTS.forEach(function (r) { ROOTS.forEach(function (r) {

View File

@ -21,11 +21,9 @@
*****************************************************************************/ *****************************************************************************/
define([ define([
'lodash', 'lodash'
'./objectEventEmitter'
], function ( ], function (
_, _
objectEventEmitter
) { ) {
var ANY_OBJECT_EVENT = "mutation"; var ANY_OBJECT_EVENT = "mutation";
@ -36,7 +34,8 @@ define([
* @param object * @param object
* @interface MutableObject * @interface MutableObject
*/ */
function MutableObject(object) { function MutableObject(eventEmitter, object) {
this.eventEmitter = eventEmitter;
this.object = object; this.object = object;
this.unlisteners = []; this.unlisteners = [];
} }
@ -61,8 +60,11 @@ define([
*/ */
MutableObject.prototype.on = function (path, callback) { MutableObject.prototype.on = function (path, callback) {
var fullPath = qualifiedEventName(this.object, path); var fullPath = qualifiedEventName(this.object, path);
objectEventEmitter.on(fullPath, callback); var eventOff =
this.unlisteners.push(objectEventEmitter.off.bind(objectEventEmitter, fullPath, callback)); this.eventEmitter.off.bind(this.eventEmitter, fullPath, callback);
this.eventEmitter.on(fullPath, callback);
this.unlisteners.push(eventOff);
}; };
/** /**
@ -78,12 +80,12 @@ define([
_.set(this.object, 'modified', Date.now()); _.set(this.object, 'modified', Date.now());
//Emit event specific to property //Emit event specific to property
objectEventEmitter.emit(qualifiedEventName(this.object, path), value); this.eventEmitter.emit(qualifiedEventName(this.object, path), value);
//Emit wildcare event //Emit wildcare event
objectEventEmitter.emit(qualifiedEventName(this.object, '*'), this.object); this.eventEmitter.emit(qualifiedEventName(this.object, '*'), this.object);
//Emit a general "any object" event //Emit a general "any object" event
objectEventEmitter.emit(ANY_OBJECT_EVENT, this.object); this.eventEmitter.emit(ANY_OBJECT_EVENT, this.object);
}; };
return MutableObject; return MutableObject;

View File

@ -25,13 +25,15 @@ define([
'./object-utils', './object-utils',
'./MutableObject', './MutableObject',
'./RootRegistry', './RootRegistry',
'./RootObjectProvider' './RootObjectProvider',
'./EventEmitter'
], function ( ], function (
_, _,
utils, utils,
MutableObject, MutableObject,
RootRegistry, RootRegistry,
RootObjectProvider RootObjectProvider,
EventEmitter
) { ) {
@ -42,6 +44,7 @@ define([
*/ */
function ObjectAPI() { function ObjectAPI() {
this.eventEmitter = new EventEmitter();
this.providers = {}; this.providers = {};
this.rootRegistry = new RootRegistry(); this.rootRegistry = new RootRegistry();
this.rootProvider = new RootObjectProvider(this.rootRegistry); this.rootProvider = new RootObjectProvider(this.rootRegistry);
@ -175,7 +178,9 @@ define([
* @memberof module:openmct.ObjectAPI# * @memberof module:openmct.ObjectAPI#
*/ */
ObjectAPI.prototype.mutate = function (domainObject, path, value) { ObjectAPI.prototype.mutate = function (domainObject, path, value) {
return new MutableObject(domainObject).set(path, value); var mutableObject =
new MutableObject(this.eventEmitter, domainObject);
return mutableObject.set(path, value);
}; };
/** /**
@ -188,7 +193,8 @@ define([
* @memberof module:openmct.ObjectAPI# * @memberof module:openmct.ObjectAPI#
*/ */
ObjectAPI.prototype.observe = function (domainObject, path, callback) { ObjectAPI.prototype.observe = function (domainObject, path, callback) {
var mutableObject = new MutableObject(domainObject); var mutableObject =
new MutableObject(this.eventEmitter, domainObject);
mutableObject.on(path, callback); mutableObject.on(path, callback);
return mutableObject.stopListening.bind(mutableObject); return mutableObject.stopListening.bind(mutableObject);
}; };