diff options
author | siikamiika <siikamiika@users.noreply.github.com> | 2020-02-14 11:00:36 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-02-14 11:00:36 +0200 |
commit | c09a3ded1d694887e4e9ff48cc3c387abdbb2065 (patch) | |
tree | af4a70e78ecc76a9e93ffcd23323f48c9da89d35 /ext | |
parent | e645296b1b5e993355844f2cb2b026b87d05759e (diff) | |
parent | 810a7e7d92d15412974810702d954de60453dd31 (diff) |
Merge pull request #357 from siikamiika/simplify-display-prepare
Simplify display prepare
Diffstat (limited to 'ext')
-rw-r--r-- | ext/bg/js/search.js | 6 | ||||
-rw-r--r-- | ext/fg/js/float.js | 32 | ||||
-rw-r--r-- | ext/fg/js/frontend.js | 3 | ||||
-rw-r--r-- | ext/fg/js/popup-proxy-host.js | 8 | ||||
-rw-r--r-- | ext/fg/js/popup-proxy.js | 5 | ||||
-rw-r--r-- | ext/fg/js/popup.js | 25 | ||||
-rw-r--r-- | ext/mixed/js/display-generator.js | 26 | ||||
-rw-r--r-- | ext/mixed/js/display.js | 28 |
8 files changed, 57 insertions, 76 deletions
diff --git a/ext/bg/js/search.js b/ext/bg/js/search.js index 93bcfa53..76a62b97 100644 --- a/ext/bg/js/search.js +++ b/ext/bg/js/search.js @@ -49,9 +49,9 @@ class DisplaySearch extends Display { async prepare() { try { - await this.initialize(); - - 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/fg/js/float.js b/ext/fg/js/float.js index d31b8336..418c69b6 100644 --- a/ext/fg/js/float.js +++ b/ext/fg/js/float.js @@ -34,6 +34,22 @@ 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); + + apiForward('popupSetDisplayInitialized'); + } + onError(error) { if (this._orphaned) { this.setContent('orphaned'); @@ -93,20 +109,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([ @@ -123,7 +125,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/fg/js/frontend.js b/ext/fg/js/frontend.js index 3611d44e..a620fb03 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -244,5 +244,6 @@ Frontend._windowMessageHandlers = new Map([ ]); Frontend._runtimeMessageHandlers = new Map([ - ['popupSetVisibleOverride', (self, {visible}) => { self.popup.setVisibleOverride(visible); }] + ['popupSetVisibleOverride', (self, {visible}) => { self.popup.setVisibleOverride(visible); }], + ['popupSetDisplayInitialized', (self) => { self.popup.setDisplayInitialized(); }] ]); diff --git a/ext/fg/js/popup-proxy-host.js b/ext/fg/js/popup-proxy-host.js index 98729796..f6a3d451 100644 --- a/ext/fg/js/popup-proxy-host.js +++ b/ext/fg/js/popup-proxy-host.js @@ -43,7 +43,8 @@ class PopupProxyHost { ['showContent', ({id, elementRect, writingMode, type, details}) => this._onApiShowContent(id, elementRect, writingMode, type, details)], ['setCustomCss', ({id, css}) => this._onApiSetCustomCss(id, css)], ['clearAutoPlayTimer', ({id}) => this._onApiClearAutoPlayTimer(id)], - ['setContentScale', ({id, scale}) => this._onApiSetContentScale(id, scale)] + ['setContentScale', ({id, scale}) => this._onApiSetContentScale(id, scale)], + ['setDisplayInitialized', ({id}) => this._onApiSetDisplayInitialized(id)] ])); } @@ -104,6 +105,11 @@ class PopupProxyHost { return popup.setContentScale(scale); } + async _onApiSetDisplayInitialized(id) { + const popup = this._getPopup(id); + return popup.setDisplayInitialized(); + } + // Private functions _createPopupInternal(parentId, depth) { diff --git a/ext/fg/js/popup-proxy.js b/ext/fg/js/popup-proxy.js index db6dffb1..6f3c0f1c 100644 --- a/ext/fg/js/popup-proxy.js +++ b/ext/fg/js/popup-proxy.js @@ -103,6 +103,11 @@ class PopupProxy { this._invokeHostApi('setContentScale', {id, scale}); } + async setDisplayInitialized() { + const id = await this._getPopupId(); + this._invokeHostApi('setDisplayInitialized', {id}); + } + // Private _getPopupId() { diff --git a/ext/fg/js/popup.js b/ext/fg/js/popup.js index 0b142dda..d077b1f8 100644 --- a/ext/fg/js/popup.js +++ b/ext/fg/js/popup.js @@ -28,8 +28,6 @@ class Popup { this._child = null; this._childrenSupported = true; this._injectPromise = null; - this._isInjected = false; - this._isInjectedAndLoaded = false; this._visible = false; this._visibleOverride = null; this._options = null; @@ -118,16 +116,16 @@ class Popup { } clearAutoPlayTimer() { - if (this._isInjectedAndLoaded) { - this._invokeApi('clearAutoPlayTimer'); - } + this._invokeApi('clearAutoPlayTimer'); } setContentScale(scale) { this._contentScale = scale; - if (this._isInjectedAndLoaded) { - this._invokeApi('setContentScale', {scale}); - } + this._invokeApi('setContentScale', {scale}); + } + + setDisplayInitialized() { + throw new Error('Override me'); } // Popup-only public functions @@ -147,7 +145,7 @@ class Popup { } isVisibleSync() { - return this._isInjected && (this._visibleOverride !== null ? this._visibleOverride : this._visible); + return (this._visibleOverride !== null ? this._visibleOverride : this._visible); } updateTheme() { @@ -226,7 +224,6 @@ class Popup { return new Promise((resolve) => { const parentFrameId = (typeof this._frameId === 'number' ? this._frameId : null); this._container.addEventListener('load', () => { - this._isInjectedAndLoaded = true; this._invokeApi('initialize', { options: this._options, popupInfo: { @@ -238,12 +235,11 @@ class Popup { childrenSupported: this._childrenSupported, scale: this._contentScale }); - resolve(); + this.setDisplayInitialized = resolve; }); this._observeFullscreen(); this._onFullscreenChanged(); this.setCustomOuterCss(this._options.general.customPopupOuterCss, false); - this._isInjected = true; }); } @@ -328,10 +324,9 @@ class Popup { } _invokeApi(action, params={}) { - if (!this._isInjectedAndLoaded) { - throw new Error('Frame not loaded'); + if (this._container.contentWindow) { + this._container.contentWindow.postMessage({action, params}, '*'); } - this._container.contentWindow.postMessage({action, params}, '*'); } _observeFullscreen() { diff --git a/ext/mixed/js/display-generator.js b/ext/mixed/js/display-generator.js index 3617e546..46f3d17e 100644 --- a/ext/mixed/js/display-generator.js +++ b/ext/mixed/js/display-generator.js @@ -20,9 +20,6 @@ class DisplayGenerator { constructor() { - this._isInitialized = false; - this._initializationPromise = null; - this._termEntryTemplate = null; this._termExpressionTemplate = null; this._termDefinitionItemTemplate = null; @@ -41,18 +38,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) { @@ -304,13 +293,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 b18e275d..178567ab 100644 --- a/ext/mixed/js/display.js +++ b/ext/mixed/js/display.js @@ -48,6 +48,13 @@ class Display { this.setInteractive(true); } + async prepare(options=null) { + const displayGeneratorPromise = this.displayGenerator.prepare(); + const updateOptionsPromise = this.updateOptions(options); + await Promise.all([displayGeneratorPromise, updateOptionsPromise]); + yomichan.on('optionsUpdate', () => this.updateOptions(null)); + } + onError(_error) { throw new Error('Override me'); } @@ -243,15 +250,6 @@ class Display { throw new Error('Override me'); } - isInitialized() { - return this.options !== null; - } - - async initialize(options=null) { - 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); @@ -362,7 +360,6 @@ class Display { async setContentTerms(definitions, context, token) { if (!context) { throw new Error('Context expected'); } - if (!this.isInitialized()) { return; } this.setEventListenersActive(false); @@ -370,10 +367,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) { @@ -426,7 +420,6 @@ class Display { async setContentKanji(definitions, context, token) { if (!context) { throw new Error('Context expected'); } - if (!this.isInitialized()) { return; } this.setEventListenersActive(false); @@ -434,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) { |