-
-
Notifications
You must be signed in to change notification settings - Fork 179
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: update event.target & event.currentTarget via defineProperty. #545
Closed
Closed
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b4e5e23
fix: update currenTarget via defineProperty. Fixes #544 & #343.
benbender daf779e
fix: set event via defineProperties
benbender 98fcc43
Merge branch 'master' into fix/set-event-target
benbender 23a212b
Merge branch 'master' into fix/set-event-target
capricorn86 a4f5a94
Merge branch 'master' into fix/set-event-target
benbender File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @benbender! 🙂
Thank you for contributing!
I am a bit worried about the performance impact of calling
defineProperty()
on each event and element where it bubbles. UsingdefineProperty()
may change the shape of the object, which could take away optimizations in the V8 engine.Maybe we can do a solution where we use getters in the
Event
class, which returns_currentTarget
and_target
that can be modified from here?We can also do some performance testing and see how it affects it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey 👋 ,
I gave your idea about the getters a quick shot and it did not work. The problem is that
jest-testing-library
useslib.dom.d.ts
wheretarget
/currentTarget
are readonly. So as soon as we are writing directly to them and types are checked at runtime (vitest
), this will fail :/Tbh, I didn't even thought about performance in this context. I would be happy to do some microbenchmarks if there is some infrastructure I could use. Do you have anything at hand which could be repurposed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is weird that
jest-testing-library
runs the compiler on thehappy-dom
internal code 🤔Maybe it is possible to do something like this?
Event.ts
EventTarget.ts
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sadly this won't help as its a typing issue. So whenever you try to write to
.target
/.currentTarget
, it will fail. This problem is independent of the implementation inhappy-dom
. The problem originates from the (correct) types inlib.dom.d.ts
, as they mark those props as readonly. The only fix (at least as far as I can see) is to not write directly to those readonly-props in the interfaces. That's also why thedefineProperty()
-approach works...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@benbender I am not sure if you missed the "_" (underline) before the properties. My idea was that we perhaps can write to other properties instead and then return them from getters that are never changed? Then we will not risk any performance penalty of using
Object.defineProperty()
.I would also like to understand why
vitest
orjest-testing-library
are checking the types at runtime in the internal code ofhappy-dom
. Do you have some example code I can run? 😅There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure. Repro: https://github.com/benbender/happy-dom-repro-544
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Related: vitest-dev/vitest#1494