Skip to content

Commit

Permalink
Merge pull request #10 from anodejs/yosefd1
Browse files Browse the repository at this point in the history
return cloned objects from rebus
  • Loading branch information
yosefd committed Jul 29, 2013
2 parents 9208cf5 + da9fc1c commit 60ed7df
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 6 deletions.
7 changes: 4 additions & 3 deletions lib/rebus.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
70 changes: 70 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,76 @@ 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 r1gotb = false;
var r1gotx = false;
var r1gotz = false;
var r2gotb = false;
var r2gotx = false;
var r2gotz = false;
// Number of notification may vary depending on file system behavior
// One possible scenario:
// 1. Notification for setting b arrives
// 2. x is published
// 3. Notification for x arrives
// 4. Changes in monitored file trigger notification for setting b again
// 5. Changed in file notify on setting x
// So instead of getting just 2 notifications, 4 may arrive.
// However, eventual consistency is garanteered.
rebus1.subscribe('a.c', function (obj) {
console.log('rebus1 got', obj);
if (obj.b === 'b') {
r1gotb = true;
obj.b = 'x';
rebus1.publish('a.c', obj);
}
if (obj.b === 'x') {
r1gotx = true;
}
if (obj.d === 'z') {
r1gotz = true;
}
});

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);
if (obj.b === 'b') {
r2gotb = true;
}
if (obj.b === 'x') {
r2gotx = true;
var obj = rebus2.value.a.c;
obj['d'] = 'z';
rebus2.publish('a.c', obj);
}
if (obj.d === 'z') {
r2gotz = true;
}
});
});

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.ok(r1gotb && r2gotb);
test.ok(r1gotx && r2gotx);
test.ok(r1gotz && r2gotz);
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 60ed7df

Please sign in to comment.