Merge BackendEventHandler into Backend class (#632)

* Merge BackendEventHandler into Backend class

* Remove unused public functions
This commit is contained in:
toasted-nutbread 2020-06-28 17:22:44 -04:00 committed by GitHub
parent 5183fb575f
commit 7590055d4e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 77 deletions

View File

@ -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;
}
}

View File

@ -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;
}