fix(core): support symbol and prototype-named events - #67
Open
buiducnhat wants to merge 1 commit into
Open
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 inheritedObject.prototypeproperty (e.g.__proto__) also fails during registration.Root cause
EventsService._hookswas initialized as a plain object, so_hooks['__proto__']resolved to the inherited prototype instead of a listener array.EventsService._resolve()narrowed every event name tostringand calledstartsWith()unconditionally, which fails for symbols.Fix
_hookswithObject.create(null)so string names never collide withObject.prototypemembers._resolve()so it only runs on string names (typeof name === 'string' && name.startsWith('internal/')).internal/dispatchevent name type tostring | symbol.Tests
Added three test cases in
packages/core/tests/events.spec.ts:emit(and dispose cleanly)emit/parallel/serial/bail__proto__registers, dispatches, and releases its bucket on disposeAll 74 core tests pass. The core package builds and type-checks cleanly.