diff --git a/web/struct_tree_layer_builder.js b/web/struct_tree_layer_builder.js index 19de6f72025d8..4e117eacec99e 100644 --- a/web/struct_tree_layer_builder.js +++ b/web/struct_tree_layer_builder.js @@ -76,7 +76,9 @@ const HEADING_PATTERN = /^H(\d+)$/; class StructTreeLayerBuilder { #promise; - #treeDom = undefined; + #treeDom = null; + + #treePromise; #elementAttributes = new Map(); @@ -85,13 +87,23 @@ class StructTreeLayerBuilder { } async render() { - if (this.#treeDom !== undefined) { - return this.#treeDom; + if (this.#treePromise) { + return this.#treePromise; + } + const { promise, resolve, reject } = Promise.withResolvers(); + this.#treePromise = promise; + + try { + this.#treeDom = this.#walk(await this.#promise); + } catch (ex) { + reject(ex); } - const treeDom = (this.#treeDom = this.#walk(await this.#promise)); this.#promise = null; - treeDom?.classList.add("structTree"); - return treeDom; + + this.#treeDom?.classList.add("structTree"); + resolve(this.#treeDom); + + return promise; } async getAriaAttributes(annotationId) {