From 2a95f1420f08b034ae8e12ecffed86aa6f33e53a Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Fri, 20 Dec 2019 13:36:54 -0500 Subject: Move optionsUpdate message handler into Yomichan class --- ext/mixed/js/core.js | 11 +++++++++-- ext/mixed/js/display.js | 15 +-------------- 2 files changed, 10 insertions(+), 16 deletions(-) (limited to 'ext/mixed') diff --git a/ext/mixed/js/core.js b/ext/mixed/js/core.js index edb1f913..a3c8c0b0 100644 --- a/ext/mixed/js/core.js +++ b/ext/mixed/js/core.js @@ -232,10 +232,13 @@ class EventDispatcher { */ const yomichan = (() => { - class Yomichan { + class Yomichan extends EventDispatcher { constructor() { + super(); + this._messageHandlers = new Map([ - ['getUrl', this._onMessageGetUrl.bind(this)] + ['getUrl', this._onMessageGetUrl.bind(this)], + ['optionsUpdate', this._onMessageOptionsUpdate.bind(this)] ]); chrome.runtime.onMessage.addListener(this._onMessage.bind(this)); @@ -253,6 +256,10 @@ const yomichan = (() => { _onMessageGetUrl() { return {url: window.location.href}; } + + _onMessageOptionsUpdate({source}) { + this.trigger('optionsUpdate', {source}); + } } return new Yomichan(); diff --git a/ext/mixed/js/display.js b/ext/mixed/js/display.js index f3b5dd2a..089941a9 100644 --- a/ext/mixed/js/display.js +++ b/ext/mixed/js/display.js @@ -225,15 +225,6 @@ class Display { } } - onRuntimeMessage({action, params}, sender, callback) { - const handler = Display._runtimeMessageHandlers.get(action); - if (typeof handler !== 'function') { return false; } - - const result = handler(this, params, sender); - callback(result); - return false; - } - getOptionsContext() { throw new Error('Override me'); } @@ -244,7 +235,7 @@ class Display { async initialize(options=null) { await this.updateOptions(options); - chrome.runtime.onMessage.addListener(this.onRuntimeMessage.bind(this)); + yomichan.on('optionsUpdate', () => this.updateOptions(null)); } async updateOptions(options) { @@ -878,7 +869,3 @@ Display._onKeyDownHandlers = new Map([ return false; }] ]); - -Display._runtimeMessageHandlers = new Map([ - ['optionsUpdate', (self) => self.updateOptions(null)] -]); -- cgit v1.2.3