Skip to content
This repository has been archived by the owner on Dec 5, 2019. It is now read-only.

Commit

Permalink
once() and removeListener for RemoteEmitter
Browse files Browse the repository at this point in the history
  • Loading branch information
James Halliday committed Oct 23, 2010
1 parent 17e0c4e commit 8d30795
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
9 changes: 8 additions & 1 deletion lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,15 @@ RemoteEmitter.prototype.attach = function (conn) {
if (i >= 0) events[name].splice(i,1);
self.removeListener(name, f);
}
}
},
once : function (name, f) {
ev.on(name, function g () {
ev.off(name, g);
f.apply(ev, arguments);
});
},
};
ev.removeListener = ev.off;

conn.on('end', function () {
Object.keys(events).forEach(function (name) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name" : "dnode",
"version" : "0.3.5",
"version" : "0.3.6",
"description" : "Asynchronous, bidirectional remote method calls with transparently wrapped callbacks",
"modules" : {
"index" : "./lib/dnode",
Expand Down
8 changes: 4 additions & 4 deletions test/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ exports['remote emitters'] = function (assert) {
return moo.attach(conn);
}).listen(port);

var got = { moo : false, tied : [ false, false ] };
var got = { moo : 0, tied : [ false, false ] };

server.on('ready', function () {
DNode.connect(port, function (remote) {
Expand All @@ -33,8 +33,8 @@ exports['remote emitters'] = function (assert) {
});

remote.subscribe(function (ev) {
ev.on('moo', function () {
got.moo = true;
ev.once('moo', function () {
got.moo ++;
});
});

Expand All @@ -61,7 +61,7 @@ exports['remote emitters'] = function (assert) {
});

setTimeout(function () {
assert.ok(got.moo);
assert.equal(got.moo, 1);
assert.ok(got.tied[0]);
assert.ok(got.tied[1]);
server.end();
Expand Down

0 comments on commit 8d30795

Please sign in to comment.