From 994afde51f9836011f1781508f360ec75c4efe7e Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Sat, 17 May 2014 16:52:28 +0300 Subject: [PATCH] Make subscribe/unsubscribe/sendMessage return Promises that fire on RPC return. --- static/cosmopolite.js | 69 +++++++++++++++++++++++++------------------ static/test.js | 41 +++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 29 deletions(-) diff --git a/static/cosmopolite.js b/static/cosmopolite.js index 099fea3..b37350e 100644 --- a/static/cosmopolite.js +++ b/static/cosmopolite.js @@ -84,22 +84,26 @@ Cosmopolite.prototype.subscribe = function(subject, messages, keys) { if (!this.ready_) { throw "cosmopolite: not ready"; } - keys = keys || []; - if (subject in this.subscriptions_) { - console.log( - this.loggingPrefix_(), - 'not sending duplication subscription request for subject:', subject); - return; - } - this.subscriptions_[subject] = { - 'messages': [], - 'keys': {}, - }; - this.sendRPC_('subscribe', { - 'subject': subject, - 'messages': messages, - 'keys': keys, - }); + + return new Promise(function(resolve, reject) { + keys = keys || []; + if (subject in this.subscriptions_) { + console.log( + this.loggingPrefix_(), + 'not sending duplication subscription request for subject:', subject); + resolve(); + } + this.subscriptions_[subject] = { + 'messages': [], + 'keys': {}, + }; + var args = { + 'subject': subject, + 'messages': messages, + 'keys': keys, + }; + this.sendRPC_('subscribe', args, resolve); + }.bind(this)); }; /** @@ -114,10 +118,14 @@ Cosmopolite.prototype.unsubscribe = function(subject) { if (!this.ready_) { throw "cosmopolite: not ready"; } - delete this.subscriptions_[subject]; - this.sendRPC_('unsubscribe', { - 'subject': subject, - }); + + return new Promise(function(resolve, reject) { + delete this.subscriptions_[subject]; + var args = { + 'subject': subject, + } + this.sendRPC_('unsubscribe', args, resolve); + }.bind(this)); }; /** @@ -131,15 +139,18 @@ Cosmopolite.prototype.sendMessage = function(subject, message, key) { if (!this.ready_) { throw "cosmopolite: not ready"; } - var args = { - 'subject': subject, - 'message': JSON.stringify(message), - 'sender_message_id': this.uuid_(), - }; - if (key) { - args['key'] = key; - } - this.sendRPC_('sendMessage', args); + + return new Promise(function(resolve, reject) { + var args = { + 'subject': subject, + 'message': JSON.stringify(message), + 'sender_message_id': this.uuid_(), + }; + if (key) { + args['key'] = key; + } + this.sendRPC_('sendMessage', args, resolve); + }.bind(this)); }; /** diff --git a/static/test.js b/static/test.js index 2abead2..ce78ea7 100644 --- a/static/test.js +++ b/static/test.js @@ -174,6 +174,47 @@ asyncTest('Complex object', function() { var cosmo = new Cosmopolite(callbacks, null, randstring()); }); +asyncTest('sendMessage Promise', function() { + expect(1); + + var subject = randstring(); + var message = randstring(); + + var callbacks = { + 'onReady': function() { + cosmo.sendMessage(subject, message).then(function() { + ok(true, 'sendMessage Promise fulfilled'); + cosmo.shutdown(); + start(); + }); + }, + }; + + var cosmo = new Cosmopolite(callbacks, null, randstring()); +}); + +asyncTest('subscribe/unsubscribe Promise', function() { + expect(2); + + var subject = randstring(); + var message = randstring(); + + var callbacks = { + 'onReady': function() { + cosmo.subscribe(subject).then(function() { + ok(true, 'subscribe Promise fulfilled'); + cosmo.unsubscribe(subject).then(function() { + ok(true, 'unsubscribe Promise fulfilled'); + cosmo.shutdown(); + start(); + }); + }); + }, + }; + + var cosmo = new Cosmopolite(callbacks, null, randstring()); +}); + module('dev_appserver only');