-
-
Notifications
You must be signed in to change notification settings - Fork 674
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: use fixed event listeners to address race conditions #1566
Open
chrisvxd
wants to merge
2
commits into
clauderic:experimental
Choose a base branch
from
chrisvxd:fixed-pointer-event-listeners
base: experimental
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@dnd-kit/dom': patch | ||
--- | ||
|
||
Use fixed event listeners in PointerSensor to address race conditions preventing `pointermove` and `pointerup` events from firing when document changes during a drag. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,6 +56,8 @@ export class PointerSensor extends Sensor< | |
|
||
protected initialCoordinates: Coordinates | undefined; | ||
|
||
protected source: Draggable | undefined = undefined; | ||
|
||
#clearTimeout: CleanupFunction | undefined; | ||
|
||
constructor( | ||
|
@@ -67,6 +69,14 @@ export class PointerSensor extends Sensor< | |
this.handleCancel = this.handleCancel.bind(this); | ||
this.handlePointerUp = this.handlePointerUp.bind(this); | ||
this.handleKeyDown = this.handleKeyDown.bind(this); | ||
|
||
effect(() => { | ||
const unbindGlobal = this.bindGlobal(options ?? {}); | ||
|
||
return () => { | ||
unbindGlobal(); | ||
}; | ||
}); | ||
} | ||
|
||
public bind(source: Draggable, options = this.options) { | ||
|
@@ -92,6 +102,48 @@ export class PointerSensor extends Sensor< | |
return unbind; | ||
} | ||
|
||
protected bindGlobal(options: PointerSensorOptions) { | ||
const documents = new Set<Document>(); | ||
|
||
for (const draggable of this.manager.registry.draggables.value) { | ||
if (draggable.element) { | ||
documents.add(getDocument(draggable.element)); | ||
} | ||
} | ||
|
||
for (const droppable of this.manager.registry.droppables.value) { | ||
if (droppable.element) { | ||
documents.add(getDocument(droppable.element)); | ||
} | ||
} | ||
|
||
const unbindFns = Array.from(documents).map((doc) => | ||
this.listeners.bind(doc, [ | ||
{ | ||
type: 'pointermove', | ||
listener: (event: PointerEvent) => | ||
this.handlePointerMove(event, doc, options), | ||
}, | ||
{ | ||
type: 'pointerup', | ||
listener: this.handlePointerUp, | ||
options: { | ||
capture: true, | ||
}, | ||
}, | ||
{ | ||
// Cancel activation if there is a competing Drag and Drop interaction | ||
type: 'dragstart', | ||
listener: this.handleDragStart, | ||
}, | ||
]) | ||
); | ||
|
||
return () => { | ||
unbindFns.forEach((unbind) => unbind()); | ||
}; | ||
} | ||
|
||
protected handlePointerDown( | ||
event: PointerEvent, | ||
source: Draggable, | ||
|
@@ -106,11 +158,6 @@ export class PointerSensor extends Sensor< | |
) { | ||
return; | ||
} | ||
const {target} = event; | ||
const isNativeDraggable = | ||
isHTMLElement(target) && | ||
target.draggable && | ||
target.getAttribute('draggable') === 'true'; | ||
|
||
const offset = getFrameTransform(source.element); | ||
|
||
|
@@ -119,6 +166,8 @@ export class PointerSensor extends Sensor< | |
y: event.clientY * offset.scaleY + offset.y, | ||
}; | ||
|
||
this.source = source; | ||
|
||
const {activationConstraints} = options; | ||
const constraints = | ||
typeof activationConstraints === 'function' | ||
|
@@ -145,48 +194,38 @@ export class PointerSensor extends Sensor< | |
} | ||
} | ||
|
||
const ownerDocument = getDocument(event.target); | ||
|
||
const unbindListeners = this.listeners.bind(ownerDocument, [ | ||
{ | ||
type: 'pointermove', | ||
listener: (event: PointerEvent) => | ||
this.handlePointerMove(event, source, options), | ||
}, | ||
{ | ||
type: 'pointerup', | ||
listener: this.handlePointerUp, | ||
options: { | ||
capture: true, | ||
}, | ||
}, | ||
{ | ||
// Cancel activation if there is a competing Drag and Drop interaction | ||
type: 'dragstart', | ||
listener: isNativeDraggable ? this.handleCancel : preventDefault, | ||
}, | ||
]); | ||
|
||
const cleanup = () => { | ||
setTimeout(unbindListeners); | ||
this.#clearTimeout?.(); | ||
this.initialCoordinates = undefined; | ||
this.source = undefined; | ||
}; | ||
|
||
this.cleanup.add(cleanup); | ||
} | ||
|
||
protected handlePointerMove( | ||
event: PointerEvent, | ||
source: Draggable, | ||
doc: Document, | ||
options: PointerSensorOptions | ||
) { | ||
if (!this.source) { | ||
return; | ||
} | ||
|
||
const ownerDocument = | ||
this.source.element && getDocument(this.source.element); | ||
|
||
// Event may have duplicated between documents if user is bubbling events | ||
if (doc !== ownerDocument) { | ||
return; | ||
} | ||
Comment on lines
+218
to
+221
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note, new safe-guard to account for an edge-case where the user bubbles the I can address this on my end, but a safe-guard seemed sensible. |
||
|
||
const coordinates = { | ||
x: event.clientX, | ||
y: event.clientY, | ||
}; | ||
|
||
const offset = getFrameTransform(source.element); | ||
const offset = getFrameTransform(this.source.element); | ||
|
||
coordinates.x = coordinates.x * offset.scaleX + offset.x; | ||
coordinates.y = coordinates.y * offset.scaleY + offset.y; | ||
|
@@ -210,7 +249,7 @@ export class PointerSensor extends Sensor< | |
const {activationConstraints} = options; | ||
const constraints = | ||
typeof activationConstraints === 'function' | ||
? activationConstraints(event, source) | ||
? activationConstraints(event, this.source) | ||
: activationConstraints; | ||
const {distance, delay} = constraints ?? {}; | ||
|
||
|
@@ -222,7 +261,7 @@ export class PointerSensor extends Sensor< | |
return this.handleCancel(); | ||
} | ||
if (exceedsDistance(delta, distance.value)) { | ||
return this.handleStart(source, event); | ||
return this.handleStart(this.source, event); | ||
} | ||
} | ||
|
||
|
@@ -304,6 +343,25 @@ export class PointerSensor extends Sensor< | |
this.cleanup.add(unbind); | ||
} | ||
|
||
protected handleDragStart(event: DragEvent) { | ||
const {target} = event; | ||
|
||
if (!isElement(target)) { | ||
return; | ||
} | ||
|
||
const isNativeDraggable = | ||
isHTMLElement(target) && | ||
target.draggable && | ||
target.getAttribute('draggable') === 'true'; | ||
|
||
if (isNativeDraggable) { | ||
this.handleCancel(); | ||
} else { | ||
preventDefault(event); | ||
} | ||
} | ||
|
||
protected handleCancel() { | ||
const {dragOperation} = this.manager; | ||
|
||
|
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.
Noting that this may be better elsewhere for perf reasons