From efe8140f103179f50b610f182148b9427af99010 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sun, 14 Feb 2021 15:19:31 -0500 Subject: Api prepare refactor (#1391) * Refactor API preparation * Run yomichan.prepare manually in the entry point function --- ext/js/comm/api.js | 39 ++++++++++++++++----------------------- 1 file changed, 16 insertions(+), 23 deletions(-) (limited to 'ext/js/comm/api.js') diff --git a/ext/js/comm/api.js b/ext/js/comm/api.js index d37b091a..26397d1f 100644 --- a/ext/js/comm/api.js +++ b/ext/js/comm/api.js @@ -22,8 +22,8 @@ const api = (() => { class API { constructor() { - this._forwardLogsToBackendEnabled = false; - this._crossFrame = new CrossFrameAPI(); + this._prepared = false; + this._crossFrame = null; } get crossFrame() { @@ -31,20 +31,11 @@ const api = (() => { } prepare() { + if (this._prepared) { return; } + this._crossFrame = new CrossFrameAPI(); this._crossFrame.prepare(); - } - - forwardLogsToBackend() { - if (this._forwardLogsToBackendEnabled) { return; } - this._forwardLogsToBackendEnabled = true; - - yomichan.on('log', async ({error, level, context}) => { - try { - await this.log(serializeError(error), level, context); - } catch (e) { - // NOP - } - }); + yomichan.on('log', this._onLog.bind(this)); + this._prepared = true; } // Invoke functions @@ -161,10 +152,6 @@ const api = (() => { return this._invoke('getMedia', {targets}); } - log(error, level, context) { - return this._invoke('log', {error, level, context}); - } - logIndicatorClear() { return this._invoke('logIndicatorClear'); } @@ -331,10 +318,16 @@ const api = (() => { _checkLastError() { // NOP } + + async _onLog({error, level, context}) { + try { + error = serializeError(error); + await this._invoke('log', {error, level, context}); + } catch (e) { + // NOP + } + } } - // eslint-disable-next-line no-shadow - const api = new API(); - api.prepare(); - return api; + return new API(); })(); -- cgit v1.2.3