Skip to content

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

Open
buiducnhat wants to merge 1 commit into
cordiverse:mainfrom
buiducnhat:fix/issue-50-symbol-proto-events
Open

fix(core): support symbol and prototype-named events#67
buiducnhat wants to merge 1 commit into
cordiverse:mainfrom
buiducnhat:fix/issue-50-symbol-proto-events

Conversation

@buiducnhat

Copy link
Copy Markdown

Summary

Fixes #50 — symbol and prototype-named events fail during registration or dispatch.

Problem

The event API accepts symbol names in ctx.on(), but dispatching the same symbol throws before its listener can run. A string matching an inherited Object.prototype property (e.g. __proto__) also fails during registration.

const ctx = new Context()
const symbol = Symbol('example')
ctx.on(symbol, () => {})
ctx.emit(symbol)
// TypeError: name.startsWith is not a function

ctx.on('__proto__', () => {})
// TypeError: hooks[method] is not a function

Root cause

  • EventsService._hooks was initialized as a plain object, so _hooks['__proto__'] resolved to the inherited prototype instead of a listener array.
  • EventsService._resolve() narrowed every event name to string and called startsWith() unconditionally, which fails for symbols.

Fix

  • Initialize _hooks with Object.create(null) so string names never collide with Object.prototype members.
  • Guard the internal-prefix check in _resolve() so it only runs on string names (typeof name === 'string' && name.startsWith('internal/')).
  • Widen the internal/dispatch event name type to string | symbol.

Tests

Added three test cases in packages/core/tests/events.spec.ts:

  • symbol events register and dispatch via emit (and dispose cleanly)
  • symbol events dispatch across emit/parallel/serial/bail
  • __proto__ registers, dispatches, and releases its bucket on dispose

All 74 core tests pass. The core package builds and type-checks cleanly.

Closes cordiverse#50. Symbol event names now dispatch across emit/parallel/serial/bail
instead of throwing 'name.startsWith is not a function', and string names that
collide with Object.prototype properties (e.g. __proto__) register and dispatch
correctly.

- Initialize EventsService._hooks with Object.create(null) so event buckets are
own properties instead of inherited prototype members.
- Guard the internal-prefix check in _resolve() against non-string (symbol) names.
- Widen the internal/dispatch event name type to string | symbol.
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