Make subscribe/unsubscribe/sendMessage return Promises that fire on RPC return.

This commit is contained in:
Ian Gulliver
2014-05-17 16:52:28 +03:00
parent 96b17ad6ff
commit 994afde51f
2 changed files with 81 additions and 29 deletions

View File

@@ -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));
};
/**

View File

@@ -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');