Skip to content

Add onPageAdded constructor option - #1871

Merged
blikblum merged 1 commit into
foliojs:masterfrom
tlq5l:fix/first-page-event-option
Nov 22, 2026
Merged

blikblum merged 1 commit into
foliojs:masterfrom
tlq5l:fix/first-page-event-option

Conversation

@tlq5l

@tlq5l tlq5l commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

What kind of change does this PR introduce?

A feature, closes #1529. Replaces closed #1870 by adding an onPageAdded constructor option. Issue #1529 has no maintainer confirmation that this proposal fixes the reported case.

An on constructor option lets callers attach several listeners before the first page and keeps original listener references for removal. It adds a broader event-registration contract than existing focused options.

An onPageAdded option handles the first-page case directly and fits existing single-purpose options. It runs synchronously with the document argument on every added page, including autoFirstPage, in Node and browser builds. Because the constructor registers a wrapper, passing the original callback to off() will not remove it.

This PR picks onPageAdded for a small, consistent cross-runtime API while leaving existing callers unchanged. Switching to the on option before merge is easy.

Four of the new tests fail on master. The new tests pass on this branch.

Checklist:

  • Unit Tests
  • Documentation
  • Update CHANGELOG.md
  • Ready to be merged

The pageAdded event is emitted inside addPage(), and the constructor adds the first page before a caller can attach a listener. A page added that way could not be written to with on('pageAdded'). The new onPageAdded option takes a function, registers it before the automatic first page is added, and calls it with the document for every page. A first page added by hand with autoFirstPage: false is covered too.

The emission point of pageAdded does not change. Listeners attached with on() or once() see the same events in the same order as before, and no event methods are overridden.

Fixes foliojs#1529

Agent: claude
@blikblum

Copy link
Copy Markdown
Member

Much better. Thanks

@blikblum
blikblum merged commit 716b67e into foliojs:master Nov 22, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

pageAdded event not called for first page with autoFirstPage: true

2 participants