Skip to content
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 event bubbling can break in test conditions (#4161) #4232

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions src/diff/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@
}
}

// BigInt is used for the counter to avoid overflow.
// It is supported by all major browsers except IE and Opera mini
// where the plain number is used as fallback.
// eslint-disable-next-line no-undef
const zero = typeof window != 'undefined' && window.BigInt ? BigInt(0) : 0;

Check warning on line 20 in src/diff/props.js

View workflow job for this annotation

GitHub Actions / Build & Test

A function with a name starting with an uppercase letter should only be used as a constructor

Check warning on line 20 in src/diff/props.js

View workflow job for this annotation

GitHub Actions / Build & Test / Build & Test

A function with a name starting with an uppercase letter should only be used as a constructor
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const zero = typeof window != 'undefined' && window.BigInt ? BigInt(0) : 0;
const zero = Number.MIN_SAFE_INTEGER;

let globalCounter = zero;
let cachedCounter = zero;
const p = Promise.resolve();
const getCounter = () =>
cachedCounter ||
(p.then(() => (cachedCounter = zero)), (cachedCounter = ++globalCounter));

/**
* Set a property value on a DOM node
* @param {PreactElement} dom The DOM node to modify
Expand Down Expand Up @@ -68,7 +80,15 @@

if (value) {
if (!oldValue) {
value._attached = Date.now();
// Note that the counter is incremented when an event listener is newly attached
// for the first time during a render cycle. All event listeners that are newly
// attached during that render cycle will share the same counter value.
// The value will be compared to the _dispatched added to the event itself
// and the event will propagate when _attached is less or equal to _dispatched.
// Potentially the event handler might be shared between multiple nodes, but
// as long as the order of _attached and _dispatched values is correct
// the event bubbling will work.
value._attached = getCounter();
const handler = useCapture ? eventProxyCapture : eventProxy;
dom.addEventListener(name, handler, useCapture);
} else {
Expand Down Expand Up @@ -138,11 +158,11 @@
*/
if (!e._dispatched) {
// When an event has no _dispatched we know this is the first event-target in the chain
// so we set the initial dispatched time.
e._dispatched = Date.now();
// When the _dispatched is smaller than the time when the targetted event handler was attached
// so we set the initial dispatched counter value.
e._dispatched = globalCounter;
// If the _dispatched is smaller than the counter when the targetted event handler was attached
// we know we have bubbled up to an element that was added during patching the dom.
} else if (e._dispatched <= eventHandler._attached) {
} else if (e._dispatched < eventHandler._attached) {
return;
}
return eventHandler(options.event ? options.event(e) : e);
Expand Down
2 changes: 1 addition & 1 deletion src/internal.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ declare global {
}

export interface PreactEvent extends Event {
_dispatched?: number;
_dispatched?: number | bigint;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
_dispatched?: number | bigint;
_dispatched?: number;

}

// We use the `current` property to differentiate between the two kinds of Refs so
Expand Down
Loading