Skip to content

fix(core): support symbol and prototype-named events - #82

Open
rekcilyssup wants to merge 1 commit into
cordiverse:mainfrom
rekcilyssup:fix/symbol-event-keys
Open

fix(core): support symbol and prototype-named events#82
rekcilyssup wants to merge 1 commit into
cordiverse:mainfrom
rekcilyssup:fix/symbol-event-keys

Conversation

@rekcilyssup

Copy link
Copy Markdown

Closes #50

Problem

  • ctx.on() accepts symbols, but emit() / parallel() / serial() / bail()
    threw TypeError: name.startsWith is not a function on dispatch — _resolve()
    narrowed every event name to string and called startsWith() unconditionally.
  • String names colliding with Object.prototype properties (e.g. __proto__)
    failed during registration, because _hooks was a plain object whose inherited
    prototype shadowed own keys.
  • Disposing the last listener left the (now empty) event bucket behind.

Fix

In packages/core/src/events.ts:

  • _hooks is now a null-prototype table, so prototype-named keys are plain own
    properties.
  • _resolve() only applies the internal/ prefix check to string names; symbols
    pass through to the hook table unchanged.
  • register() / unregister() now track the event key, and unregister() deletes
    the bucket when the last listener is disposed.

Tests

Added 5 tests: symbol dispatch via emit and parallel, __proto__ registration
and dispatch, and bucket release after disposing the last listener.

Verified locally: yarn lint, yarn build, yarn test (167 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.

Symbol and prototype-named events fail during registration or dispatch

1 participant