Skip to content

Commit

Permalink
Don't timeout if the completion variable is already set (#287)
Browse files Browse the repository at this point in the history
* Don't timeout if the completion variable is already set

* Update formatting.

* Add unit test.

Co-authored-by: Seth Westphal <[email protected]>
  • Loading branch information
Congelli501 and westy92 authored Mar 25, 2021
1 parent b236ee0 commit db38386
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/CompletionTriggers/Variable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ export class Variable extends CompletionTrigger {
awaitPromise: true,
expression: `
new Promise((resolve, reject) => {
// check if already set
if (window['${varName}'] === true) {
resolve();
return;
}
Object.defineProperty(window, '${varName}', {
set: (val) => { if (val === true) resolve(); }
});
Expand Down
22 changes: 22 additions & 0 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,28 @@ describe('HtmlPdf', () => {
}
});

it('should not time out if the variable is already set', async () => {
const alreadySetHtml = `
<html>
<body>
<div id="test">Failed!</div>
<script>
document.getElementById('test').innerHTML = 'Variable!';
alreadySet = true;
</script>
</body>
</html>
`;
const options: HtmlPdf.CreateOptions = {
port,
completionTrigger: new HtmlPdf.CompletionTrigger.Variable('alreadySet', 300),
};
const result = await HtmlPdf.create(alreadySetHtml, options);
expect(result).to.be.an.instanceOf(HtmlPdf.CreateResult);
const pdf = await getParsedPdf(result.toBuffer());
expect(pdf.getRawTextContent()).startsWith('Variable!');
});

it('should generate correctly after being triggered', async () => {
const options: HtmlPdf.CreateOptions = {
port,
Expand Down

0 comments on commit db38386

Please sign in to comment.