Skip to content

Commit

Permalink
Merge pull request #18689 from Snuffleupagus/StructTreeLayerBuilder-r…
Browse files Browse the repository at this point in the history
…ender-caching

Improve the `StructTreeLayerBuilder.render` method
  • Loading branch information
calixteman authored Sep 4, 2024
2 parents 1cdaaad + 4b86286 commit 4fb045b
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions web/struct_tree_layer_builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ const HEADING_PATTERN = /^H(\d+)$/;
class StructTreeLayerBuilder {
#promise;

#treeDom = undefined;
#treeDom = null;

#treePromise;

#elementAttributes = new Map();

Expand All @@ -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) {
Expand Down

0 comments on commit 4fb045b

Please sign in to comment.