From 93eaee9765c7478ee5ad7485d664b86acf3d4510 Mon Sep 17 00:00:00 2001 From: siikamiika Date: Thu, 13 Feb 2020 01:43:01 +0200 Subject: simplify DisplayGenerator initialization --- ext/mixed/js/display-generator.js | 26 ++++---------------------- ext/mixed/js/display.js | 11 +++-------- 2 files changed, 7 insertions(+), 30 deletions(-) (limited to 'ext/mixed/js') diff --git a/ext/mixed/js/display-generator.js b/ext/mixed/js/display-generator.js index d5ab9dbc..ee3ac4cf 100644 --- a/ext/mixed/js/display-generator.js +++ b/ext/mixed/js/display-generator.js @@ -19,9 +19,6 @@ class DisplayGenerator { constructor() { - this._isInitialized = false; - this._initializationPromise = null; - this._termEntryTemplate = null; this._termExpressionTemplate = null; this._termDefinitionItemTemplate = null; @@ -40,18 +37,10 @@ class DisplayGenerator { this._tagFrequencyTemplate = null; } - isInitialized() { - return this._isInitialized; - } - - initialize() { - if (this._isInitialized) { - return Promise.resolve(); - } - if (this._initializationPromise === null) { - this._initializationPromise = this._initializeInternal(); - } - return this._initializationPromise; + async prepare() { + const html = await apiGetDisplayTemplatesHtml(); + const doc = new DOMParser().parseFromString(html, 'text/html'); + this._setTemplates(doc); } createTermEntry(details) { @@ -303,13 +292,6 @@ class DisplayGenerator { return node; } - async _initializeInternal() { - const html = await apiGetDisplayTemplatesHtml(); - const doc = new DOMParser().parseFromString(html, 'text/html'); - this._setTemplates(doc); - this._isInitialized = true; - } - _setTemplates(doc) { this._termEntryTemplate = doc.querySelector('#term-entry-template'); this._termExpressionTemplate = doc.querySelector('#term-expression-template'); diff --git a/ext/mixed/js/display.js b/ext/mixed/js/display.js index a6cfe848..cee63d9b 100644 --- a/ext/mixed/js/display.js +++ b/ext/mixed/js/display.js @@ -243,6 +243,7 @@ class Display { } async initialize(options=null) { + await this.displayGenerator.prepare(); await this.updateOptions(options); yomichan.on('optionsUpdate', () => this.updateOptions(null)); } @@ -365,10 +366,7 @@ class Display { window.focus(); } - if (!this.displayGenerator.isInitialized()) { - await this.displayGenerator.initialize(); - if (this.setContentToken !== token) { return; } - } + if (this.setContentToken !== token) { return; } this.definitions = definitions; if (context.disableHistory) { @@ -429,10 +427,7 @@ class Display { window.focus(); } - if (!this.displayGenerator.isInitialized()) { - await this.displayGenerator.initialize(); - if (this.setContentToken !== token) { return; } - } + if (this.setContentToken !== token) { return; } this.definitions = definitions; if (context.disableHistory) { -- cgit v1.2.3 From df37acd17f9459c17185552f11dc4cc424e01958 Mon Sep 17 00:00:00 2001 From: siikamiika Date: Thu, 13 Feb 2020 01:59:26 +0200 Subject: rename display initialize methods to prepare --- ext/bg/js/search.js | 2 +- ext/fg/js/float.js | 30 +++++++++++++++--------------- ext/mixed/js/display.js | 24 ++++++++++++------------ 3 files changed, 28 insertions(+), 28 deletions(-) (limited to 'ext/mixed/js') diff --git a/ext/bg/js/search.js b/ext/bg/js/search.js index 6641255f..86ed675a 100644 --- a/ext/bg/js/search.js +++ b/ext/bg/js/search.js @@ -47,7 +47,7 @@ class DisplaySearch extends Display { async prepare() { try { - await this.initialize(); + await super.prepare(); await this.queryParser.prepare(); diff --git a/ext/fg/js/float.js b/ext/fg/js/float.js index 8d61d8f6..3766d5a4 100644 --- a/ext/fg/js/float.js +++ b/ext/fg/js/float.js @@ -33,6 +33,20 @@ class DisplayFloat extends Display { window.addEventListener('message', (e) => this.onMessage(e), false); } + async prepare(options, popupInfo, url, childrenSupported, scale) { + await super.prepare(options); + + const {id, depth, parentFrameId} = popupInfo; + this.optionsContext.depth = depth; + this.optionsContext.url = url; + + if (childrenSupported) { + popupNestedInitialize(id, depth, parentFrameId, url); + } + + this.setContentScale(scale); + } + onError(error) { if (this._orphaned) { this.setContent('orphaned'); @@ -92,20 +106,6 @@ class DisplayFloat extends Display { setContentScale(scale) { document.body.style.fontSize = `${scale}em`; } - - async initialize(options, popupInfo, url, childrenSupported, scale) { - await super.initialize(options); - - const {id, depth, parentFrameId} = popupInfo; - this.optionsContext.depth = depth; - this.optionsContext.url = url; - - if (childrenSupported) { - popupNestedInitialize(id, depth, parentFrameId, url); - } - - this.setContentScale(scale); - } } DisplayFloat._onKeyDownHandlers = new Map([ @@ -122,7 +122,7 @@ DisplayFloat._messageHandlers = new Map([ ['setContent', (self, {type, details}) => self.setContent(type, details)], ['clearAutoPlayTimer', (self) => self.clearAutoPlayTimer()], ['setCustomCss', (self, {css}) => self.setCustomCss(css)], - ['initialize', (self, {options, popupInfo, url, childrenSupported, scale}) => self.initialize(options, popupInfo, url, childrenSupported, scale)], + ['initialize', (self, {options, popupInfo, url, childrenSupported, scale}) => self.prepare(options, popupInfo, url, childrenSupported, scale)], ['setContentScale', (self, {scale}) => self.setContentScale(scale)] ]); diff --git a/ext/mixed/js/display.js b/ext/mixed/js/display.js index cee63d9b..8dea625d 100644 --- a/ext/mixed/js/display.js +++ b/ext/mixed/js/display.js @@ -43,6 +43,16 @@ class Display { this.setInteractive(true); } + async prepare(options=null) { + await this.displayGenerator.prepare(); + await this.updateOptions(options); + yomichan.on('optionsUpdate', () => this.updateOptions(null)); + } + + isPrepared() { + return this.options !== null; + } + onError(_error) { throw new Error('Override me'); } @@ -238,16 +248,6 @@ class Display { throw new Error('Override me'); } - isInitialized() { - return this.options !== null; - } - - async initialize(options=null) { - await this.displayGenerator.prepare(); - await this.updateOptions(options); - yomichan.on('optionsUpdate', () => this.updateOptions(null)); - } - async updateOptions(options) { this.options = options ? options : await apiOptionsGet(this.getOptionsContext()); this.updateDocumentOptions(this.options); @@ -358,7 +358,7 @@ class Display { async setContentTerms(definitions, context, token) { if (!context) { throw new Error('Context expected'); } - if (!this.isInitialized()) { return; } + if (!this.isPrepared()) { return; } this.setEventListenersActive(false); @@ -419,7 +419,7 @@ class Display { async setContentKanji(definitions, context, token) { if (!context) { throw new Error('Context expected'); } - if (!this.isInitialized()) { return; } + if (!this.isPrepared()) { return; } this.setEventListenersActive(false); -- cgit v1.2.3 From 8abab28c4db4bae6e1bce003fc228d26e0458c78 Mon Sep 17 00:00:00 2001 From: siikamiika Date: Thu, 13 Feb 2020 14:36:32 +0200 Subject: remove isPrepared check --- ext/mixed/js/display.js | 6 ------ 1 file changed, 6 deletions(-) (limited to 'ext/mixed/js') diff --git a/ext/mixed/js/display.js b/ext/mixed/js/display.js index 8dea625d..b524fe34 100644 --- a/ext/mixed/js/display.js +++ b/ext/mixed/js/display.js @@ -49,10 +49,6 @@ class Display { yomichan.on('optionsUpdate', () => this.updateOptions(null)); } - isPrepared() { - return this.options !== null; - } - onError(_error) { throw new Error('Override me'); } @@ -358,7 +354,6 @@ class Display { async setContentTerms(definitions, context, token) { if (!context) { throw new Error('Context expected'); } - if (!this.isPrepared()) { return; } this.setEventListenersActive(false); @@ -419,7 +414,6 @@ class Display { async setContentKanji(definitions, context, token) { if (!context) { throw new Error('Context expected'); } - if (!this.isPrepared()) { return; } this.setEventListenersActive(false); -- cgit v1.2.3 From d7e1ef01d8af4a315a31364eb5138e24a132ea1e Mon Sep 17 00:00:00 2001 From: siikamiika Date: Thu, 13 Feb 2020 16:26:45 +0200 Subject: use Promise.all to await dependencies --- ext/bg/js/search.js | 6 +++--- ext/mixed/js/display.js | 5 +++-- 2 files changed, 6 insertions(+), 5 deletions(-) (limited to 'ext/mixed/js') diff --git a/ext/bg/js/search.js b/ext/bg/js/search.js index 86ed675a..bb27205c 100644 --- a/ext/bg/js/search.js +++ b/ext/bg/js/search.js @@ -47,9 +47,9 @@ class DisplaySearch extends Display { async prepare() { try { - await super.prepare(); - - await this.queryParser.prepare(); + const superPromise = super.prepare(); + const queryParserPromise = this.queryParser.prepare(); + await Promise.all([superPromise, queryParserPromise]); const {queryParams: {query='', mode=''}} = parseUrl(window.location.href); diff --git a/ext/mixed/js/display.js b/ext/mixed/js/display.js index b524fe34..d49c205e 100644 --- a/ext/mixed/js/display.js +++ b/ext/mixed/js/display.js @@ -44,8 +44,9 @@ class Display { } async prepare(options=null) { - await this.displayGenerator.prepare(); - await this.updateOptions(options); + const displayGeneratorPromise = this.displayGenerator.prepare(); + const updateOptionsPromise = this.updateOptions(options); + await Promise.all([displayGeneratorPromise, updateOptionsPromise]); yomichan.on('optionsUpdate', () => this.updateOptions(null)); } -- cgit v1.2.3