fix: only resolve once per selector query

This commit is contained in:
cha0s 2019-04-24 02:44:35 -05:00
parent 4564aec67f
commit 950c598c5a

View File

@ -37,7 +37,7 @@ export class Stage extends decorate(Container) {
this._transformRatio = 1; this._transformRatio = 1;
// UI DOM node. // UI DOM node.
this.ui = this.createUiLayer(); this.ui = this.createUiLayer();
this._queuedFindSelectors = []; this._queuedFindSelectors = {};
// Event handlers. // Event handlers.
this.onWindowResize = this.onWindowResize.bind(this); this.onWindowResize = this.onWindowResize.bind(this);
window.addEventListener('resize', this.onWindowResize); window.addEventListener('resize', this.onWindowResize);
@ -115,18 +115,24 @@ export class Stage extends decorate(Container) {
if (node) { if (node) {
return Promise.resolve(node); return Promise.resolve(node);
} }
const queued = this._queuedFindSelectors[selector];
if (queued) {
return queued.promise;
}
let resolve; let resolve;
const promise = new Promise((resolve_) => resolve = resolve_); const promise = new Promise((resolve_) => resolve = resolve_);
this._queuedFindSelectors.push({resolve, selector}); this._queuedFindSelectors[selector] = {resolve, promise};
return promise return promise
} }
flushUiElements() { flushUiElements() {
for (let i = 0; i < this._queuedFindSelectors.length; i++) { const selectors = Object.keys(this._queuedFindSelectors);
const {resolve, selector} = this._queuedFindSelectors[i]; for (let i = 0; i < selectors.length; i++) {
const selector = selectors[i];
const {resolve} = this._queuedFindSelectors[selector];
resolve(this.ui.querySelector(selector)); resolve(this.ui.querySelector(selector));
} }
this._queuedFindSelectors = []; this._queuedFindSelectors = {};
} }
focus() { focus() {