diff options
author | siikamiika <siikamiika@users.noreply.github.com> | 2020-03-02 04:51:45 +0200 |
---|---|---|
committer | siikamiika <siikamiika@users.noreply.github.com> | 2020-03-02 04:51:45 +0200 |
commit | 967e99b7f69d24fc76999675cef44b919602dd31 (patch) | |
tree | 4128ebda33c7ff04a597f03a2ea46c183ae22f18 /ext/mixed/js | |
parent | e6e5f23cf8481db31b94c18244f404dd3374ad90 (diff) |
ensure Backend prepare in other places
Diffstat (limited to 'ext/mixed/js')
-rw-r--r-- | ext/mixed/js/api.js | 28 | ||||
-rw-r--r-- | ext/mixed/js/core.js | 13 | ||||
-rw-r--r-- | ext/mixed/js/display.js | 1 |
3 files changed, 14 insertions, 28 deletions
diff --git a/ext/mixed/js/api.js b/ext/mixed/js/api.js index fa61a1e2..26f4389d 100644 --- a/ext/mixed/js/api.js +++ b/ext/mixed/js/api.js @@ -122,30 +122,6 @@ function apiGetDefaultAnkiFieldTemplates() { } function _apiInvoke(action, params={}) { - if (!_isBackendReady) { - if (_isBackendReadyPromise === null) { - _isBackendReadyPromise = new Promise((resolve) => (_isBackendReadyResolve = resolve)); - const checkBackendReady = async () => { - try { - if (await _apiInvokeRaw('isBackendReady')) { - _isBackendReady = true; - _isBackendReadyResolve(); - } - } catch (e) { - // NOP - } - setTimeout(checkBackendReady, 100); // poll Backend until it responds - }; - checkBackendReady(); - } - return _isBackendReadyPromise.then( - () => _apiInvokeRaw(action, params) - ); - } - return _apiInvokeRaw(action, params); -} - -function _apiInvokeRaw(action, params={}) { const data = {action, params}; return new Promise((resolve, reject) => { try { @@ -172,7 +148,3 @@ function _apiInvokeRaw(action, params={}) { function _apiCheckLastError() { // NOP } - -let _isBackendReady = false; -let _isBackendReadyResolve = null; -let _isBackendReadyPromise = null; diff --git a/ext/mixed/js/core.js b/ext/mixed/js/core.js index 83813796..a3cb2459 100644 --- a/ext/mixed/js/core.js +++ b/ext/mixed/js/core.js @@ -269,17 +269,26 @@ const yomichan = (() => { constructor() { super(); + this._isBackendPreparedResolve = null; + this._isBackendPreparedPromise = new Promise((resolve) => (this._isBackendPreparedResolve = resolve)); + this._messageHandlers = new Map([ + ['backendPrepared', this._onBackendPrepared.bind(this)], ['getUrl', this._onMessageGetUrl.bind(this)], ['optionsUpdated', this._onMessageOptionsUpdated.bind(this)], ['zoomChanged', this._onMessageZoomChanged.bind(this)] ]); chrome.runtime.onMessage.addListener(this._onMessage.bind(this)); + chrome.runtime.sendMessage({action: 'yomichanOnline'}); } // Public + prepare() { + return this._isBackendPreparedPromise; + } + generateId(length) { const array = new Uint8Array(length); window.crypto.getRandomValues(array); @@ -305,6 +314,10 @@ const yomichan = (() => { return false; } + _onBackendPrepared() { + this._isBackendPreparedResolve(); + } + _onMessageGetUrl() { return {url: window.location.href}; } diff --git a/ext/mixed/js/display.js b/ext/mixed/js/display.js index e3e5e7df..6a762a65 100644 --- a/ext/mixed/js/display.js +++ b/ext/mixed/js/display.js @@ -153,6 +153,7 @@ class Display { } async prepare(options=null) { + await yomichan.prepare(); const displayGeneratorPromise = this.displayGenerator.prepare(); const updateOptionsPromise = this.updateOptions(options); await Promise.all([displayGeneratorPromise, updateOptionsPromise]); |