aboutsummaryrefslogtreecommitdiff
path: root/ext/js/comm
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2021-02-14 15:19:31 -0500
committerGitHub <noreply@github.com>2021-02-14 15:19:31 -0500
commitefe8140f103179f50b610f182148b9427af99010 (patch)
tree94afdefddf9985d2ec11cdcd9a1e2638e2005b3d /ext/js/comm
parent94201ed44ac1835637e77a6a7f583aca70bbf2d3 (diff)
Api prepare refactor (#1391)
* Refactor API preparation * Run yomichan.prepare manually in the entry point function
Diffstat (limited to 'ext/js/comm')
-rw-r--r--ext/js/comm/api.js39
1 files changed, 16 insertions, 23 deletions
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();
})();