From 600054b5ca9e8b6df13b3545e789b9945913e790 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Tue, 13 May 2014 18:25:12 +0300 Subject: [PATCH] Keep a client-side key index and add a lookup function. --- static/cosmopolite.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/static/cosmopolite.js b/static/cosmopolite.js index 349a5f5..a86f20d 100644 --- a/static/cosmopolite.js +++ b/static/cosmopolite.js @@ -70,6 +70,7 @@ Cosmopolite.prototype.subscribe = function(subject, messages, keys) { } this.subscriptions_[subject] = { 'messages': [], + 'keys': {}, }; this.sendRPC_('subscribe', { 'subject': subject, @@ -115,11 +116,23 @@ Cosmopolite.prototype.sendMessage = function(subject, message, key) { * Fetch all received messages for a subject * * @param {!string} subject Subject name + * @const */ Cosmopolite.prototype.getMessages = function(subject) { return this.subscriptions_[subject].messages; }; +/** + * 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) { + return this.subscriptions_[subject].keys[key]; +}; + /** * Callback when a script loads. */ @@ -371,6 +384,9 @@ Cosmopolite.prototype.onServerEvent_ = function(e) { } e['message'] = JSON.parse(e['message']); subscription.messages.push(e); + if (e['key']) { + subscription.keys[e['key']] = e; + } if ('onMessage' in this.callbacks_) { this.callbacks_['onMessage'](e); }