From c90f933acb2e04cc63de2ccc436928ff0462e2dd Mon Sep 17 00:00:00 2001 From: Dallon Feldner Date: Tue, 30 Oct 2012 08:25:39 -0700 Subject: [PATCH] Added dpd.once, off, onConnect, socket functions Test app should no longer have expected failures --- HISTORY.md | 6 +++++- clib/.jshintrc | 1 + clib/dpd.js | 22 ++++++++++++++++++++ lib/resources/client-lib.js | 6 ++++++ test-app/public/index.html | 5 +++-- test-app/public/test/collection.test.js | 10 ++++----- test-app/public/test/user-collection.test.js | 6 +++--- 7 files changed, 45 insertions(+), 11 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index 44cfa32..dfb3372 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -3,6 +3,10 @@ ## 0.6.8 - Fixed CORS incorrectly requiring a referer header + - Added `dpd.once(name, fn)` function to execute a realtime handler exactly once + - Added `dpd.off(name, [fn])` function to remove a realtime handler + - Added `dpd.onConnect(fn)` function to listen for the built-in `connect` event. (Sugar for `dpd.once('connect', fn)`) + - Added `dpd.socket` property to provide direct access to socket.io. ## 0.6.7 @@ -13,7 +17,7 @@ - Fixed bugs preventing events from being `emit()`ed to users in certain connection states - Fixed bug where boolean query values (?bool=true) were not treated as booleans - Fixed unnecessary error when parsing JSON body - - Added more intelegent body parsing + - Added more intelligent body parsing - Added `changed()` method in collection events - Added `previous` object in collection events - Fixed `dpd showkey` prompt for missing keys.json diff --git a/clib/.jshintrc b/clib/.jshintrc index 01aefb5..3bf7333 100644 --- a/clib/.jshintrc +++ b/clib/.jshintrc @@ -5,6 +5,7 @@ "lastsemic": true, "laxbreak": true, "strict": false, + "eqnull": true, "undef": true, "browser": true, diff --git a/clib/dpd.js b/clib/dpd.js index e38e91b..666a8bf 100644 --- a/clib/dpd.js +++ b/clib/dpd.js @@ -272,5 +272,27 @@ socket.on.apply(socket, arguments); }; + window.dpd.once = function(name, fn) { + var _fn = function() { + socket.removeListener(name, _fn); + fn.apply(this, arguments); + }; + socket.on(name, _fn); + }; + + window.dpd.off = function(name, fn) { + if (fn == null) { + socket.removeAllListeners(name); + } else { + socket.removeListener(name, fn); + } + }; + + window.dpd.onConnect = function(fn) { + window.dpd.once('connect', fn); + }; + + window.dpd.socket = socket; + })(); \ No newline at end of file diff --git a/lib/resources/client-lib.js b/lib/resources/client-lib.js index 80a1b4c..44dce7f 100644 --- a/lib/resources/client-lib.js +++ b/lib/resources/client-lib.js @@ -85,6 +85,12 @@ ClientLib.prototype.generateResource = function(r, res) { res.write('dpd.' + jsName + '.on = function(ev, fn) {\n'); res.write(' return dpd.on("' + r.path.replace('/', '') + '" + ":" + ev, fn);\n'); res.write('}\n'); + res.write('dpd.' + jsName + '.once = function(ev, fn) {\n'); + res.write(' return dpd.once("' + r.path.replace('/', '') + '" + ":" + ev, fn);\n'); + res.write('}\n'); + res.write('dpd.' + jsName + '.off = function(ev, fn) {\n'); + res.write(' return dpd.off("' + r.path.replace('/', '') + '" + ":" + ev, fn);\n'); + res.write('}\n'); } if(r.external) { diff --git a/test-app/public/index.html b/test-app/public/index.html index bf18e6f..7b1a1aa 100644 --- a/test-app/public/index.html +++ b/test-app/public/index.html @@ -19,8 +19,9 @@
\ No newline at end of file diff --git a/test-app/public/test/collection.test.js b/test-app/public/test/collection.test.js index 3e83046..4faabd3 100644 --- a/test-app/public/test/collection.test.js +++ b/test-app/public/test/collection.test.js @@ -8,7 +8,7 @@ describe('Collection', function() { describe('dpd.on("createTodo", fn)', function() { it('should respond to a realtime event', function(done) { this.timeout(1500); - dpd.on('createTodo', function(todo) { + dpd.once('createTodo', function(todo) { expect(todo).to.exist; expect(todo.title).to.equal('$REALTIME'); done(); @@ -20,7 +20,7 @@ describe('Collection', function() { describe('dpd.on("createTodo2", fn)', function() { it('should respond to a realtime event without a parameter', function(done) { - dpd.on('createTodo2', function(todo) { + dpd.once('createTodo2', function(todo) { expect(todo).to.not.exist; done(); }); @@ -31,7 +31,7 @@ describe('Collection', function() { describe('dpd.todos.on("changed", fn)', function() { it('should respond to the built-in changed event on post', function(done) { - dpd.todos.on('changed', function() { + dpd.todos.once('changed', function() { done(); }); @@ -40,7 +40,7 @@ describe('Collection', function() { it('should respond to the built-in changed event on put', function(done) { dpd.todos.post({title: 'changed - create'}, function(item) { - dpd.todos.on('changed', function() { + dpd.todos.once('changed', function() { done(); }); @@ -50,7 +50,7 @@ describe('Collection', function() { it('should respond to the built-in changed event on del', function(done) { dpd.todos.post({title: 'changed - create'}, function(item) { - dpd.todos.on('changed', function() { + dpd.todos.once('changed', function() { done(); }); diff --git a/test-app/public/test/user-collection.test.js b/test-app/public/test/user-collection.test.js index bc54040..02ce175 100644 --- a/test-app/public/test/user-collection.test.js +++ b/test-app/public/test/user-collection.test.js @@ -107,7 +107,7 @@ describe('User Collection', function() { }); describe('dpd.users.on("changed", fn)', function() { it('should respond to the built-in changed event on post', function(done) { - dpd.users.on('changed', function() { + dpd.users.once('changed', function() { done(); }); @@ -116,7 +116,7 @@ describe('User Collection', function() { it('should respond to the built-in changed event on put', function(done) { dpd.users.post({username: 'foo2@bar.com', password: '123456'}, function(item) { - dpd.users.on('changed', function() { + dpd.users.once('changed', function() { done(); }); @@ -126,7 +126,7 @@ describe('User Collection', function() { it('should respond to the built-in changed event on del', function(done) { dpd.users.post({username: 'foo2@bar.com', password: '123456'}, function(item) { - dpd.users.on('changed', function() { + dpd.users.once('changed', function() { done(); });