Skip to content

Commit

Permalink
return cloned objects from rebus
Browse files Browse the repository at this point in the history
some may modify objects they get from rebus, so need to give them deep
clones, or they will mess with rebus internal logic.
  • Loading branch information
yosefd committed Jul 28, 2013
1 parent 9208cf5 commit 995d650
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 6 deletions.
7 changes: 4 additions & 3 deletions lib/rebus.js
Expand Up @@ -2,6 +2,7 @@
var fs = require('fs');
var async = require('async');
var deepEqual = require('deep-equal');
var clone = require('clone');
var syncasyncFacade = require('ypatterns').syncasyncFacade;

// Suffix for all published objects.
Expand Down Expand Up @@ -91,7 +92,7 @@ module.exports = function (folder, options, callback) {

function createInstance() {
var instance = { publish: publish, subscribe: subscribe, close: close };
instance.__defineGetter__("value", function () { return shared; });
instance.__defineGetter__("value", function () { return clone(shared); });
return instance;
}

Expand Down Expand Up @@ -381,7 +382,7 @@ module.exports = function (folder, options, callback) {
// Call notification in the next tick, so that return value from subsribtion
// will be available.
process.nextTick(function () {
notification(currentobj);
notification(clone(currentobj));
});
}
}
Expand Down Expand Up @@ -423,7 +424,7 @@ module.exports = function (folder, options, callback) {
for (var id in meta[nfs]) {
fns.push(function (i) {
return function () {
meta[nfs][i](obj);
meta[nfs][i](clone(obj));
}
} (id));
}
Expand Down
7 changes: 4 additions & 3 deletions package.json
Expand Up @@ -3,12 +3,12 @@
"description": "rebus - reactive Pub/Sub bus for sharing data between nodejs applications running on the same host",
"main": "./main",
"bin": {},
"author": "anode <anode@microsoft.com>",
"version": "0.4.5",
"author": "yosefd <yosefd@microsoft.com>",
"version": "0.4.6",
"license": "MIT",
"contributors": [
"Yosef Dinerstein <yosefd@microsoft.com>",
"Elad Ben-Israel <eladb@microsoft.com>",
"Elad Ben-Israel <elad.benisrael@gmail.com>",
"Saar Yahalom <saary@microsoft.com>"
],
"keywords": [
Expand All @@ -27,6 +27,7 @@
"dependencies": {
"async": "0.1.x",
"deep-equal": "0.0.x",
"clone": "0.1.x",
"ypatterns": "0.2.x"
},
"devDependencies": {
Expand Down
44 changes: 44 additions & 0 deletions test/test.js
Expand Up @@ -263,6 +263,50 @@ module.exports = testCase({
});
},

modifyRebusObject: function (test) {
var self = this;
var rebus1 = rebus(self.folder, function (err) {
test.ok(!err, 'failed to start empty instance');
test.ok(rebus1, 'got the 1st rebus instance');
var count1 = 0;
var count2 = 0;
rebus1.subscribe('a.c', function (obj) {
console.log('rebus1 got', obj);
count1++;
if (obj.b === 'b') {
obj.b = 'x';
rebus1.publish('a.c', obj);
}
});

var rebus2 = rebus(self.folder, function (err) {
test.ok(!err, 'failed to start empty instance');
test.ok(rebus2, 'got the 2nd rebus instance');
rebus2.subscribe('a.c', function (obj) {
console.log('rebus2 got', obj);
count2++;
if (obj.b === 'x') {
var obj = rebus2.value.a.c;
obj['d'] = 'z';
rebus2.publish('a.c', obj);
}
});
});

rebus1.publish('a.c', { b: 'b' });

setTimeout(function () {
test.deepEqual(rebus1.value.a.c, { b: 'x', d: 'z' });
test.deepEqual(rebus2.value.a.c, { b: 'x', d: 'z' });
test.equal(count1, 4); // one empty, one 'b', one 'x' and one with d.
test.equal(count2, 4); // one empty, one 'b', one 'x' and one with d.
rebus1.close();
rebus2.close();
test.done();
}, 200);
});
},

sync1: function (test) {
var self = this;
var rebus1 = rebus(self.folder, { singletons: false });
Expand Down

0 comments on commit 995d650

Please sign in to comment.