Adding compatibility between old and new style mutation events

This commit is contained in:
Andrew Henry 2016-08-21 23:01:48 -07:00
parent 9c88b7ce1d
commit 8c439d8109
3 changed files with 53 additions and 4 deletions

View File

@ -1,15 +1,44 @@
define([
'./object-utils',
'./ObjectAPI'
'./ObjectAPI',
'./objectEventBus'
], function (
utils,
ObjectAPI
ObjectAPI,
objectEventBus
) {
function ObjectServiceProvider(objectService, instantiate) {
function ObjectServiceProvider(objectService, instantiate, topic) {
this.objectService = objectService;
this.instantiate = instantiate;
this.topicService = topic;
this.generalTopic = topic('mutation');
this.bridgeEventBuses(this.topicService, this.generalTopic);
}
/**
* Bridges old and new style mutation events to provide compatibility between the two APIs
* @private
*/
ObjectServiceProvider.prototype.bridgeEventBuses = function (topicService, generalTopic) {
function handleMutation(newStyleObject) {
var oldStyleObject = utils.toOldFormat(newStyleObject);
var specificTopic = topicService("mutation:" + oldStyleObject.getId());
generalTopic.notify(oldStyleObject);
specificTopic.notify(oldStyleObject.getModel());
}
function handleLegacyMutation(legacyObject){
var newStyleObject = utils.toNewFormat(legacyObject.getModel(), legacyObject.getId());
objectEventBus.emit(newStyleObject.key.identifier + ":*", newStyleObject);
}
objectEventBus.on('mutation', handleMutation)
generalTopic.listen(handleLegacyMutation);
};
ObjectServiceProvider.prototype.save = function (object) {
var key = object.key,
keyString = utils.makeKeyString(key),

View File

@ -83,7 +83,17 @@ define([
};
Objects.getMutable = function (object) {
return new MutableObject(eventEmitter, 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 Objects;

View File

@ -0,0 +1,10 @@
define([
"EventEmitter"
], function (
EventEmitter
) {
/**
* Provides a singleton event bus for sharing between objects.
*/
return new EventEmitter();
});