Move optionsUpdate message handler into Yomichan class
This commit is contained in:
parent
e14bd75a4f
commit
2a95f1420f
@ -204,14 +204,6 @@ async function onOptionsUpdate({source}) {
|
||||
await formWrite(options);
|
||||
}
|
||||
|
||||
function onMessage({action, params}) {
|
||||
switch (action) {
|
||||
case 'optionsUpdate':
|
||||
onOptionsUpdate(params);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function showExtensionInformation() {
|
||||
const node = document.getElementById('extension-info');
|
||||
@ -235,7 +227,7 @@ async function onReady() {
|
||||
|
||||
storageInfoInitialize();
|
||||
|
||||
chrome.runtime.onMessage.addListener(onMessage);
|
||||
yomichan.on('optionsUpdate', onOptionsUpdate);
|
||||
}
|
||||
|
||||
$(document).ready(() => onReady());
|
||||
|
@ -54,6 +54,7 @@ class Frontend extends TextScanner {
|
||||
try {
|
||||
await this.updateOptions();
|
||||
|
||||
yomichan.on('optionsUpdate', () => this.updateOptions());
|
||||
chrome.runtime.onMessage.addListener(this.onRuntimeMessage.bind(this));
|
||||
this.isPreparedPromiseResolve();
|
||||
} catch (e) {
|
||||
@ -200,6 +201,5 @@ Frontend._windowMessageHandlers = new Map([
|
||||
]);
|
||||
|
||||
Frontend._runtimeMessageHandlers = new Map([
|
||||
['optionsUpdate', (self) => { self.updateOptions(); }],
|
||||
['popupSetVisibleOverride', (self, {visible}) => { self.popup.setVisibleOverride(visible); }]
|
||||
]);
|
||||
|
@ -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();
|
||||
|
@ -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)]
|
||||
]);
|
||||
|
Loading…
Reference in New Issue
Block a user