Skip to content

Commit

Permalink
Avoid dispatching an event in pdfjsSandbox when destruction has run
Browse files Browse the repository at this point in the history
  • Loading branch information
Snuffleupagus committed Jun 19, 2024
1 parent b7d194f commit 73b1005
Showing 1 changed file with 23 additions and 17 deletions.
40 changes: 23 additions & 17 deletions web/generic_scripting.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,33 +40,39 @@ async function docProperties(pdfDocument) {
}

class GenericScripting {
#readyCapability = Promise.withResolvers();

#sandbox = null;

constructor(sandboxBundleSrc) {
this._ready = new Promise((resolve, reject) => {
const sandbox =
typeof PDFJSDev === "undefined"
? import(sandboxBundleSrc) // eslint-disable-line no-unsanitized/method
: __non_webpack_import__(sandboxBundleSrc);
sandbox
.then(pdfjsSandbox => {
resolve(pdfjsSandbox.QuickJSSandbox());
})
.catch(reject);
});
const sandbox =
typeof PDFJSDev === "undefined"
? import(sandboxBundleSrc) // eslint-disable-line no-unsanitized/method
: __non_webpack_import__(sandboxBundleSrc);
sandbox
.then(async pdfjsSandbox => {
this.#sandbox = await pdfjsSandbox.QuickJSSandbox();
this.#readyCapability.resolve();
})
.catch(this.#readyCapability.reject);
}

async createSandbox(data) {
const sandbox = await this._ready;
sandbox.create(data);
await this.#readyCapability.promise;
this.#sandbox?.create(data);
}

async dispatchEventInSandbox(event) {
const sandbox = await this._ready;
setTimeout(() => sandbox.dispatchEvent(event), 0);
await this.#readyCapability.promise;
if (this.#sandbox) {
setTimeout(() => this.#sandbox?.dispatchEvent(event), 0);
}
}

async destroySandbox() {
const sandbox = await this._ready;
sandbox.nukeSandbox();
await this.#readyCapability.promise;
this.#sandbox?.nukeSandbox();
this.#sandbox = null;
}
}

Expand Down

0 comments on commit 73b1005

Please sign in to comment.