diff options
Diffstat (limited to 'ext/bg/js')
-rw-r--r-- | ext/bg/js/backend.js | 122 | ||||
-rw-r--r-- | ext/bg/js/background-main.js | 3 |
2 files changed, 48 insertions, 77 deletions
diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index 03bf243d..547b32d0 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -160,8 +160,28 @@ class Backend { return this._prepareCompletePromise; } + _prepareInternalSync() { + if (isObject(chrome.commands) && isObject(chrome.commands.onCommand)) { + const onCommand = this._onWebExtensionEventWrapper(this._onCommand.bind(this)); + chrome.commands.onCommand.addListener(onCommand); + } + + if (isObject(chrome.tabs) && isObject(chrome.tabs.onZoomChange)) { + const onZoomChange = this._onWebExtensionEventWrapper(this._onZoomChange.bind(this)); + chrome.tabs.onZoomChange.addListener(onZoomChange); + } + + const onConnect = this._onWebExtensionEventWrapper(this._onConnect.bind(this)); + chrome.runtime.onConnect.addListener(onConnect); + + const onMessage = this._onMessageWrapper.bind(this); + chrome.runtime.onMessage.addListener(onMessage); + } + async _prepareInternal() { try { + this._prepareInternalSync(); + this._defaultBrowserActionTitle = await this._getBrowserIconTitle(); this._badgePrepareDelayTimer = setTimeout(() => { this._badgePrepareDelayTimer = null; @@ -209,30 +229,10 @@ class Backend { } } - prepareComplete() { - return this._prepareCompletePromise; - } - isPrepared() { return this._isPrepared; } - handleCommand(...args) { - return this._onCommand(...args); - } - - handleZoomChange(...args) { - return this._onZoomChange(...args); - } - - handleConnect(...args) { - return this._onConnect(...args); - } - - handleMessage(...args) { - return this._onMessage(...args); - } - getFullOptions(useSchema=false) { const options = this._options; return useSchema ? JsonSchema.createProxy(options, this._optionsSchema) : options; @@ -256,6 +256,34 @@ class Backend { this._updateBadge(); } + // WebExtension event handlers (with prepared checks) + + _onWebExtensionEventWrapper(handler) { + return (...args) => { + if (this._isPrepared) { + handler(...args); + return; + } + + this._prepareCompletePromise.then( + () => { handler(...args); }, + () => {} // NOP + ); + }; + } + + _onMessageWrapper(message, sender, sendResponse) { + if (this._isPrepared) { + return this._onMessage(message, sender, sendResponse); + } + + this._prepareCompletePromise.then( + () => { this._onMessage(message, sender, sendResponse); }, + () => { sendResponse(); } + ); + return true; + } + // WebExtension event handlers _onCommand(command) { @@ -1341,57 +1369,3 @@ class Backend { } } } - -class BackendEventHandler { - constructor(backend) { - this._backend = backend; - } - - prepare() { - if (isObject(chrome.commands) && isObject(chrome.commands.onCommand)) { - const onCommand = this._createGenericEventHandler((...args) => this._backend.handleCommand(...args)); - chrome.commands.onCommand.addListener(onCommand); - } - - if (isObject(chrome.tabs) && isObject(chrome.tabs.onZoomChange)) { - const onZoomChange = this._createGenericEventHandler((...args) => this._backend.handleZoomChange(...args)); - chrome.tabs.onZoomChange.addListener(onZoomChange); - } - - const onConnect = this._createGenericEventHandler((...args) => this._backend.handleConnect(...args)); - chrome.runtime.onConnect.addListener(onConnect); - - const onMessage = this._onMessage.bind(this); - chrome.runtime.onMessage.addListener(onMessage); - } - - // Event handlers - - _createGenericEventHandler(handler) { - return this._onGenericEvent.bind(this, handler); - } - - _onGenericEvent(handler, ...args) { - if (this._backend.isPrepared()) { - handler(...args); - return; - } - - this._backend.prepareComplete().then( - () => { handler(...args); }, - () => {} // NOP - ); - } - - _onMessage(message, sender, sendResponse) { - if (this._backend.isPrepared()) { - return this._backend.handleMessage(message, sender, sendResponse); - } - - this._backend.prepareComplete().then( - () => { this._backend.handleMessage(message, sender, sendResponse); }, - () => { sendResponse(); } // NOP - ); - return true; - } -} diff --git a/ext/bg/js/background-main.js b/ext/bg/js/background-main.js index 9dd447c4..1b6bd138 100644 --- a/ext/bg/js/background-main.js +++ b/ext/bg/js/background-main.js @@ -17,13 +17,10 @@ /* global * Backend - * BackendEventHandler */ (() => { const backend = new Backend(); - const backendEventHandler = new BackendEventHandler(backend); - backendEventHandler.prepare(); if (typeof window === 'object' && window !== null) { window.yomichanBackend = backend; } |