Repository navigation
Conversation
Register a handler that is invoked at most once, as requested in developit#136. The handler removes itself before its first invocation, so it fires exactly once even if it throws, and it can also be removed beforehand with off(type, handler): a once() wrapper is matched through the original handler, and like Node's removeListener the first matching listener is removed. Includes runtime tests, type tests and a README API entry. Fixes developit#136.
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: ca0179d6a3
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
Guard once() wrappers with a fired flag so a synchronous nested re-emit cannot invoke the handler twice through emit()'s snapshot; share the wrapper-to-handler map at module scope so off() works through any emitter sharing the handler map; replace Array.prototype.findIndex in off() with a plain loop to keep IE11 working within the README's advertised IE9+ support. Adds regression tests for all three.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 755c29e42a
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
Hi! 👋
Motivation: #136 has asked for an official
.once()since 2019, and you noted there that you weren't entirely against adding it, depending on the size impact. This PR adds it.Change:
once(type, handler)method (plus'*'wildcard support) with the same typed overloads ason(). The handler removes itself before its first invocation, so it fires exactly once even if it throws.off(type, handler)can remove aonce()handler beforehand: the wrapper is matched through the original handler, and like Node'sremoveListenerthe first matching listener is removed. This covers theon()+once()combination you raised in Support for.once()API #136 — whichever was registered first is the oneoff()removes.once().Size impact (honest note): the bundle grows from ~190 B to ~312 B gzipped (ESM/CJS, per microbundle), so this does push mitt past the 200 B mark. If that's a deal-breaker I'm happy to discuss a smaller variant — I kept the implementation as small as I could while keeping
off(type, handler)working foronce()handlers.Verification:
npx tsc --noEmit,npx eslint src test, andnpx tsc test/test-types-compilation.ts --noEmit --strictall pass.once()tests). Note: the repo's stocknpm testfails on modern Node before reaching mocha because the legacyesmloader crashes (pre-existing — same on Node 22, see Allow interface event maps in TypeScript #223); I ran mocha directly with--no-package --no-configto skip that loader, which is the only change in how the tests were run.emitter.once('ready', fn)+ two emits) calls the handler exactly once.Fixes #136. Thanks for considering it!