Skip to content

Run event handlers the way the DOM runs listeners - #566

Merged
CodeByZach merged 1 commit into
candidate-2.0from
event-handlers
Nov 8, 2026
Merged

CodeByZach merged 1 commit into
candidate-2.0from
event-handlers

Conversation

@CodeByZach

Copy link
Copy Markdown
Owner

Makes Pace.on/once/off/trigger run handlers the way the DOM runs event listeners (spec):

  • An exception in a handler is reported as an uncaught error (with reportError where available, so window.onerror keeps the details when pace.js comes from a CDN), and pace and the other handlers carry on. Before, a throwing done or progress handler left the bar stuck for the rest of the page's life.
  • An event runs the handlers registered when it fires, minus any removed meanwhile. Before, a handler that removed itself or an earlier handler made the next one get skipped, and Pace.off(event) inside a handler threw.
  • A once handler is removed before it runs, so it runs once even if it throws or fires its own event again.

The README's events section says what happens when a handler throws, and what off does without a handler.

Tests: tests/handlers.spec.mjs covers each case. Eight of its nine tests fail on the previous code; the ninth (a handler removed while the event fires is not called) already passed and guards the new code.

Before, an exception in a handler stopped the rest of the event: when it
happened in a `done` or `progress` handler, the bar never hid and pace never
restarted again. A handler that removed itself or an earlier handler with
Pace.off made the next handler get skipped, Pace.off(event) inside a handler
threw, and handlers added while an event fired ran right away.

Now, as in DOM event dispatch, an event runs the handlers registered when
it fires, minus any removed meanwhile. A `once` handler is removed before it
runs, and an exception is reported as an uncaught error without stopping
pace or the other handlers. It is reported with reportError where the
browser has it, so window.onerror keeps the details even when pace.js comes
from a CDN; Safari before 15.4 throws it from a microtask instead.

Handlers are stored in a map without a prototype, so event names such as
"constructor" work too.
@CodeByZach
CodeByZach merged commit 55a81ad into candidate-2.0 Nov 8, 2026
2 checks passed
@CodeByZach
CodeByZach deleted the event-handlers branch November 8, 2026 01:03
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.

1 participant