fix API calls when Backend isn't ready yet
This commit is contained in:
parent
2abf46b6fa
commit
e6e5f23cf8
@ -48,6 +48,7 @@ class Backend {
|
|||||||
this.messageToken = yomichan.generateId(16);
|
this.messageToken = yomichan.generateId(16);
|
||||||
|
|
||||||
this._messageHandlers = new Map([
|
this._messageHandlers = new Map([
|
||||||
|
['isBackendReady', this._onApiIsBackendReady.bind(this)],
|
||||||
['optionsSchemaGet', this._onApiOptionsSchemaGet.bind(this)],
|
['optionsSchemaGet', this._onApiOptionsSchemaGet.bind(this)],
|
||||||
['optionsGet', this._onApiOptionsGet.bind(this)],
|
['optionsGet', this._onApiOptionsGet.bind(this)],
|
||||||
['optionsGetFull', this._onApiOptionsGetFull.bind(this)],
|
['optionsGetFull', this._onApiOptionsGetFull.bind(this)],
|
||||||
@ -267,6 +268,10 @@ class Backend {
|
|||||||
|
|
||||||
// Message handlers
|
// Message handlers
|
||||||
|
|
||||||
|
async _onApiIsBackendReady() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
async _onApiOptionsSchemaGet() {
|
async _onApiOptionsSchemaGet() {
|
||||||
return this.getOptionsSchema();
|
return this.getOptionsSchema();
|
||||||
}
|
}
|
||||||
|
@ -122,6 +122,30 @@ function apiGetDefaultAnkiFieldTemplates() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function _apiInvoke(action, params={}) {
|
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};
|
const data = {action, params};
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
try {
|
try {
|
||||||
@ -148,3 +172,7 @@ function _apiInvoke(action, params={}) {
|
|||||||
function _apiCheckLastError() {
|
function _apiCheckLastError() {
|
||||||
// NOP
|
// NOP
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let _isBackendReady = false;
|
||||||
|
let _isBackendReadyResolve = null;
|
||||||
|
let _isBackendReadyPromise = null;
|
||||||
|
Loading…
Reference in New Issue
Block a user