Skip to content

fix(dragListener): prevent spurious DRAG_UPDATE after drag ends#290

Open
draedful wants to merge 1 commit intomainfrom
claude/strange-euclid-7d1ef2
Open

fix(dragListener): prevent spurious DRAG_UPDATE after drag ends#290
draedful wants to merge 1 commit intomainfrom
claude/strange-euclid-7d1ef2

Conversation

@draedful
Copy link
Copy Markdown
Collaborator

@draedful draedful commented Apr 23, 2026

Summary

  • Set finished = true before mouseupBinded/cleanupDragState in all three end-of-drag handlers (mouseup, mouseleave, cancel) in dragListener.ts
  • Replace !startCoords?.length with !startCoords || !prevCoords in GraphComponent.onDrag

Root Cause

handleCameraChange guards on started && !finished && lastMouseEvent before emitting DRAG_UPDATE. finished was assigned after cleanup calls, leaving a window where it was still false. The original crash was startDragCoords.length (TypeError on undefined) — patched in #194 with optional chaining, but the underlying issue remained.

Fix

Move finished = true to the top of each end-of-drag handler so handleCameraChange sees the flag and skips the DRAG_UPDATE emit entirely.

Test plan

  • Drag a block/group and release — no console errors
  • All existing tests pass (npm test)

Summary by Sourcery

Ensure drag interactions are correctly finalized to avoid spurious drag updates after a drag ends.

Bug Fixes:

  • Set the drag completion flag before invoking drag end handlers to prevent DRAG_UPDATE emissions after the drag has finished.
  • Harden drag update handling in the graph component by guarding against missing start or previous coordinates to avoid runtime errors.

`handleCameraChange` was emitting DRAG_UPDATE during drag-end cleanup
because `finished` was set *after* `mouseupBinded` and `cleanupDragState`.
`cleanupDragState` (and, previously, custom `onDrop` callbacks) calls
`disableAutoPanning`, which synchronously fires `camera-change`, which
`handleCameraChange` intercepts — still seeing `finished=false` — and
re-emits DRAG_UPDATE on the still-live emitter.

The original crash was `startDragCoords.length` (TypeError on undefined)
when the spurious DRAG_UPDATE arrived after `startDragCoords` had been
cleared in `onEnd`. That was patched with `?.`, but the root cause remained.

Fix: set `finished = true` first in all three end-of-drag handlers
(mouseup, mouseleave, cancel) so `handleCameraChange` sees the flag
and skips the emit immediately.

Also replace `\!startCoords?.length` with `\!startCoords || \!prevCoords`
in GraphComponent.onDrag — explicit null-guard on both coordinate
variables instead of using `.length` as a truthiness proxy.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@draedful draedful requested a review from Antamansid as a code owner April 23, 2026 16:33
@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented Apr 23, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Ensures drag end is marked as finished before any cleanup so DRAG_UPDATE emissions are suppressed after drag termination, and tightens the drag guard condition in GraphComponent to avoid accessing unset coordinates.

File-Level Changes

Change Details Files
Mark drag as finished at the very start of all drag-ending handlers to avoid post-cleanup DRAG_UPDATE emissions.
  • Move finished = true assignment to the top of the mouseleave handler before calling mouseupBinded and cleanupDragState.
  • Move finished = true assignment to the top of the mouseup handler before calling mouseupBinded and cleanupDragState.
  • Move finished = true assignment to the top of the cancel emitter handler before calling cleanupDragState.
src/utils/functions/dragListener.ts
Strengthen drag update guards to require both start and previous coordinates to be initialized before processing updates.
  • Replace the !startCoords?.length guard with an explicit `!startCoords

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

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

Hey - I've left some high level feedback:

  • The three end-of-drag handlers now share the same finished = true; / started / cleanupDragState / cleanup sequence; consider extracting this into a small helper to ensure the ordering stays consistent if it needs to change again.
  • In GraphComponent.onDrag the if (!startCoords || !prevCoords) return; guard now silently drops updates; if this path is truly unexpected, consider logging or asserting so future regressions are easier to detect rather than failing quietly.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The three end-of-drag handlers now share the same `finished = true;` / `started` / `cleanupDragState` / `cleanup` sequence; consider extracting this into a small helper to ensure the ordering stays consistent if it needs to change again.
- In `GraphComponent.onDrag` the `if (!startCoords || !prevCoords) return;` guard now silently drops updates; if this path is truly unexpected, consider logging or asserting so future regressions are easier to detect rather than failing quietly.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@gravity-ui-bot
Copy link
Copy Markdown
Contributor

Preview is ready.

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.

2 participants