Skip to content

Commit

Permalink
🩹 Await render hook
Browse files Browse the repository at this point in the history
Render hooks can be asynchronous. This commit awaits its completion.
  • Loading branch information
ralfstx committed Jan 18, 2025
1 parent 0699f5b commit eec2a03
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/read-document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export type DocumentDefinition = {
modificationDate?: Date;
relationship?: FileRelationShip;
}[];
onRenderDocument?: (pdfDoc: PDFDocument) => void;
onRenderDocument?: (pdfDoc: PDFDocument) => void | Promise<void>;
};

export type Metadata = {
Expand Down
3 changes: 2 additions & 1 deletion src/render/render-document.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ describe('renderDocument', () => {
it('calls custom render hook', async () => {
const def = {
content: [],
onRenderDocument: (pdfDoc: PDFDocument) => {
onRenderDocument: async (pdfDoc: PDFDocument) => {
pdfDoc.setTitle('Test Title');
await new Promise((resolve) => setTimeout(resolve, 10));
},
};

Expand Down
3 changes: 2 additions & 1 deletion src/render/render-document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ export async function renderDocument(def: DocumentDefinition, pages: Page[]): Pr
});
}

def.onRenderDocument?.(pdfDoc);
await def.onRenderDocument?.(pdfDoc);

const idInfo = {
creator: 'pdfmkr',
time: new Date().toISOString(),
Expand Down

0 comments on commit eec2a03

Please sign in to comment.