From bd3c6665fbbde8a5f03f4269c80591f7af7b13af Mon Sep 17 00:00:00 2001 From: Andrew Henry Date: Mon, 22 Aug 2016 14:25:39 +0100 Subject: [PATCH] Added bridge between old and new event models --- src/api/objects/LegacyObjectAPIInterceptor.js | 44 ++++++++++++------- src/api/objects/MutableObject.js | 23 ++++++---- src/api/objects/ObjectAPI.js | 17 +------ src/api/objects/bundle.js | 3 +- ...bjectEventBus.js => objectEventEmitter.js} | 0 5 files changed, 47 insertions(+), 40 deletions(-) rename src/api/objects/{objectEventBus.js => objectEventEmitter.js} (100%) diff --git a/src/api/objects/LegacyObjectAPIInterceptor.js b/src/api/objects/LegacyObjectAPIInterceptor.js index f31cd3af81..98badc129c 100644 --- a/src/api/objects/LegacyObjectAPIInterceptor.js +++ b/src/api/objects/LegacyObjectAPIInterceptor.js @@ -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) { diff --git a/src/api/objects/MutableObject.js b/src/api/objects/MutableObject.js index 009737dc4f..00c637e6e9 100644 --- a/src/api/objects/MutableObject.js +++ b/src/api/objects/MutableObject.js @@ -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; diff --git a/src/api/objects/ObjectAPI.js b/src/api/objects/ObjectAPI.js index e0e3c0872c..f55d849d14 100644 --- a/src/api/objects/ObjectAPI.js +++ b/src/api/objects/ObjectAPI.js @@ -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; diff --git a/src/api/objects/bundle.js b/src/api/objects/bundle.js index d9cdb8a0e8..4ff0e11d90 100644 --- a/src/api/objects/bundle.js +++ b/src/api/objects/bundle.js @@ -40,7 +40,8 @@ define([ implementation: LegacyObjectAPIInterceptor, depends: [ "roots[]", - "instantiate" + "instantiate", + "topic" ] } ] diff --git a/src/api/objects/objectEventBus.js b/src/api/objects/objectEventEmitter.js similarity index 100% rename from src/api/objects/objectEventBus.js rename to src/api/objects/objectEventEmitter.js