Remove the concept of keys; they can just be encoded into subject names.

This commit is contained in:
Ian Gulliver
2014-05-25 23:50:53 -07:00
parent 8e7af2f5cf
commit 898121dabd
5 changed files with 24 additions and 146 deletions

View File

@@ -132,9 +132,8 @@ Cosmopolite.prototype.shutdown = function() {
* @param {!*} subject Subject name or object
* @param {number=} messages Number of recent messages to request; 0 for none, -1 for all
* @param {number=} last_id ID of last message received; fetch all messages since
* @param {Array.<string>=} keys Key names to ensure we receive at least 1 message defining
*/
Cosmopolite.prototype.subscribe = function(subject, messages, last_id, keys) {
Cosmopolite.prototype.subscribe = function(subject, messages, last_id) {
return new Promise(function(resolve, reject) {
var canonicalSubject = this.canonicalSubject_(subject);
var subjectString = JSON.stringify(canonicalSubject);
@@ -142,7 +141,6 @@ Cosmopolite.prototype.subscribe = function(subject, messages, last_id, keys) {
this.subscriptions_[subjectString] = {
'messages': [],
'pins': [],
'keys': {},
'state': this.SubscriptionState.PENDING,
};
}
@@ -156,9 +154,6 @@ Cosmopolite.prototype.subscribe = function(subject, messages, last_id, keys) {
if (last_id != null) {
args['last_id'] = last_id;
}
if (keys != null) {
args['keys'] = keys;
}
this.sendRPC_('subscribe', args, function(response) {
// unsubscribe may have been called since we sent the RPC. That's racy
@@ -201,18 +196,14 @@ Cosmopolite.prototype.unsubscribe = function(subject) {
*
* @param {!string} subject Subject name
* @param {!*} message Message string or object
* @param {string=} key Key name to associate this message with
*/
Cosmopolite.prototype.sendMessage = function(subject, message, key) {
Cosmopolite.prototype.sendMessage = function(subject, message) {
return new Promise(function(resolve, reject) {
var args = {
'subject': this.canonicalSubject_(subject),
'message': JSON.stringify(message),
'sender_message_id': this.uuid_(),
};
if (key) {
args['key'] = key;
}
// No message left behind.
var messageQueue = JSON.parse(localStorage[this.messageQueueKey_]);
@@ -249,19 +240,6 @@ Cosmopolite.prototype.getPins = function(subject) {
return this.subscriptions_[subjectString].pins;
};
/**
* Fetch the most recent message that defined a key
*
* @param {!string} subject Subject name
* @param {!string} key Key name
* @const
*/
Cosmopolite.prototype.getKeyMessage = function(subject, key) {
var canonicalSubject = this.canonicalSubject_(subject);
var subjectString = JSON.stringify(canonicalSubject);
return this.subscriptions_[subjectString].keys[key];
};
/**
* Return a Promise for our profile ID.
*/
@@ -835,9 +813,6 @@ Cosmopolite.prototype.onMessage_ = function(e) {
}
subscription.messages.splice(insertAfter + 1, 0, e);
if (e['key']) {
subscription.keys[e['key']] = e;
}
if ('onMessage' in this.callbacks_) {
this.callbacks_['onMessage'](e);
}

View File

@@ -24,9 +24,6 @@ a {
<div>
Message: <input type="text" id="message">
</div>
<div>
Key: <input type="text" id="key">
</div>
<div>
<input type="button" id="send" value="Send">
<input type="button" id="pin" value="Pin">
@@ -34,10 +31,6 @@ a {
<hr>
<div id="keys"></div>
<hr>
<div id="pins"></div>
<hr>
@@ -49,9 +42,6 @@ var subject = document.getElementById('subject');
var subscriptions = document.getElementById('subscriptions');
var message = document.getElementById('message');
var key = document.getElementById('key');
var keys = document.getElementById('keys');
var pins = document.getElementById('pins');
@@ -96,26 +86,6 @@ window.addEventListener('load', function() {
return;
}
addMessage(message);
if (message['key']) {
var i;
for (i = 0; i < keys.childNodes.length; i++) {
var key = keys.childNodes[i];
if (key.message['key'] == message['key']) {
// Overwrite
key.replaceChild(
document.createTextNode(message['message']),
key.childNodes[1]);
break;
}
}
if (i == keys.childNodes.length) {
var keyDiv = document.createElement('div');
keyDiv.appendChild(document.createTextNode(message['key'] + ' = '));
keyDiv.appendChild(document.createTextNode(message['message']));
keyDiv.message = message;
keys.appendChild(keyDiv);
}
}
},
onPin: function(pin) {
if (subscriptions.value != pin['subject']['name']) {
@@ -154,7 +124,7 @@ window.addEventListener('load', function() {
});
document.getElementById('send').addEventListener('click', function() {
debug.sendMessage(subscriptions.value, message.value, key.value);
debug.sendMessage(subscriptions.value, message.value);
});
document.getElementById('pin').addEventListener('click', function() {

View File

@@ -106,39 +106,6 @@ asyncTest('Message round trip', function() {
cosmo.subscribe(subject, -1);
});
asyncTest('Overwrite key', function() {
expect(8);
var subject = randstring();
var message1 = randstring();
var message2 = randstring();
var key = randstring();
var messages = 0;
var callbacks = {
'onMessage': function(e) {
messages++;
equal(e['subject']['name'], subject, 'subject matches');
equal(e['key'], key, 'key matches');
if (messages == 1) {
equal(e['message'], message1, 'message #1 matches');
equal(cosmo.getKeyMessage(subject, key)['message'], message1, 'message #1 matches by key')
cosmo.sendMessage(subject, message2, key);
return;
}
equal(e['message'], message2, 'message #2 matches');
equal(cosmo.getKeyMessage(subject, key)['message'], message2, 'message #2 matches by key')
cosmo.shutdown();
start();
},
};
var cosmo = new Cosmopolite(callbacks, null, randstring());
cosmo.subscribe(subject, -1);
cosmo.sendMessage(subject, message1, key);
});
asyncTest('Complex object', function() {
expect(2);
@@ -200,17 +167,15 @@ asyncTest('subscribe/unsubscribe Promise', function() {
});
asyncTest('Duplicate message suppression', function() {
expect(3);
expect(2);
var subject = randstring();
var key = randstring();
var message1 = randstring();
var message2 = randstring();
var callbacks = {
'onMessage': function (msg) {
equal(msg['subject']['name'], subject, 'subject matches');
equal(msg['key'], key, 'key matches');
equal(msg['message'], message1, 'message matches');
cosmo.shutdown();
start();
@@ -224,9 +189,9 @@ asyncTest('Duplicate message suppression', function() {
// chosen by fair dice roll.
// guaranteed to be random.
};
cosmo.sendMessage(subject, message1, key).then(function() {
cosmo.sendMessage(subject, message2, key).then(function() {
cosmo.subscribe(subject, 0, null, [key]);
cosmo.sendMessage(subject, message1).then(function() {
cosmo.sendMessage(subject, message2).then(function() {
cosmo.subscribe(subject, -1);
});
});
});
@@ -319,30 +284,25 @@ asyncTest('resubscribe', function() {
});
asyncTest('Message ordering', function() {
expect(5);
expect(3);
var subject = randstring();
var messages = [ 'A', 'B', 'C', 'D', 'E', 'F' ];
var keys = [ null, 'X', 'X', null, null, null ];
var messages = [ 'A', 'B', 'C', 'D' ];
var cosmo = new Cosmopolite({}, null, randstring());
var sendNextMessage = function() {
if (messages.length) {
cosmo.sendMessage(subject, messages.shift(), keys.shift()).then(sendNextMessage);
cosmo.sendMessage(subject, messages.shift()).then(sendNextMessage);
} else {
cosmo.subscribe(subject, 1).then(function() {
cosmo.subscribe(subject, 0, null, ['X']).then(function() {
cosmo.subscribe(subject, 2).then(function() {
var fetched = cosmo.getMessages(subject);
equal(fetched.length, 3, 'three messages');
equal(fetched[0]['message'], 'C', 'message 0: C matches');
equal(fetched[1]['message'], 'E', 'message 1: E matches');
equal(fetched[2]['message'], 'F', 'message 2: F matches');
equal(cosmo.getKeyMessage(subject, 'X')['message'], 'C', 'key X matches');
cosmo.shutdown();
start();
});
cosmo.subscribe(subject, 2).then(function() {
var fetched = cosmo.getMessages(subject);
equal(fetched.length, 2, 'two messages');
equal(fetched[0]['message'], 'C', 'message 0: C matches');
equal(fetched[1]['message'], 'D', 'message 1: D matches');
cosmo.shutdown();
start();
});
});
}