From 5762e59c419cb07807412e7430768ab31021f187 Mon Sep 17 00:00:00 2001 From: lilia Date: Fri, 19 Dec 2014 13:59:18 -0800 Subject: [PATCH] DRY up registration event callbacks This was just a special case of the extension.on/trigger interface. --- js/background.js | 2 +- js/chromium.js | 17 +++++++---------- js/views/inbox_view.js | 2 +- 3 files changed, 9 insertions(+), 12 deletions(-) diff --git a/js/background.js b/js/background.js index 8ac625d36..b650b27b1 100644 --- a/js/background.js +++ b/js/background.js @@ -38,6 +38,6 @@ } }; - textsecure.registration.addListener(init); + extension.on('registration_done', init); init(); })(); diff --git a/js/chromium.js b/js/chromium.js index 42ac9738c..2ac9011ec 100644 --- a/js/chromium.js +++ b/js/chromium.js @@ -38,7 +38,12 @@ chrome.runtime.sendMessage(null, { name: name, data: object }); }; - window.extension.onMessage = function (name, callback) { + window.extension.on = function (name, callback) { + // this causes every listener to fire on every message. + // if we eventually end up with lots of listeners (lol) + // might be worth making a map of 'name' -> [callbacks, ...] + // so we can fire a single listener that calls only the necessary + // calllbacks for that message name chrome.runtime.onMessage.addListener(function(e) { if (e.name === name) { callback(e.data); @@ -50,20 +55,12 @@ window.textsecure.registration = { done: function () { localStorage.setItem("chromiumRegistrationDone", ""); - chrome.runtime.sendMessage('registration_done'); + extension.trigger('registration_done'); window.location = '/index.html'; }, isDone: function () { return localStorage.getItem("chromiumRegistrationDone") !== null; }, - - addListener: function (callback) { - chrome.runtime.onMessage.addListener(function(message) { - if (message === 'registration_done') { - callback(); - } - }); - } }; }()); diff --git a/js/views/inbox_view.js b/js/views/inbox_view.js index 407d7d409..967a6c94a 100644 --- a/js/views/inbox_view.js +++ b/js/views/inbox_view.js @@ -42,7 +42,7 @@ } }.bind(this)); - extension.onMessage('message', function(message) { + extension.on('message', function(message) { this.conversations.fetch({id: message.conversationId}).then(function() { this.conversations.get(message.conversationId).fetchMessages(); }.bind(this));