Skip to content

Commit 3da69f0

Browse files
ohahclaude
andcommitted
refactor: simplify detach() guard and add regression test for touch scroll
- Replace empty if block with optional chaining guard - Add test verifying touchState survives detach/attach cycle Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent e16d1dc commit 3da69f0

2 files changed

Lines changed: 21 additions & 4 deletions

File tree

packages/react-wasm-table/src/adapter/__tests__/event-manager.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,26 @@ describe("EventManager", () => {
866866
expect(onCellClick).not.toHaveBeenCalled();
867867
});
868868

869+
it("touchState survives detach/attach cycle during active scroll", () => {
870+
const onScroll = mock(() => {});
871+
em.setLayouts([], []);
872+
em.attach(canvas, { onScroll });
873+
874+
// Start touch and begin scrolling
875+
canvas.dispatchEvent(createTouchEvent("touchstart", [{ clientX: 100, clientY: 100 }]));
876+
canvas.dispatchEvent(createTouchEvent("touchmove", [{ clientX: 100, clientY: 80 }]));
877+
expect(onScroll).toHaveBeenCalled();
878+
onScroll.mockClear();
879+
880+
// Simulate React effect re-run: detach then re-attach
881+
em.detach();
882+
em.attach(canvas, { onScroll });
883+
884+
// Continue scrolling — touchmove should still work
885+
canvas.dispatchEvent(createTouchEvent("touchmove", [{ clientX: 100, clientY: 60 }]));
886+
expect(onScroll).toHaveBeenCalled();
887+
});
888+
869889
it("multi-touch is ignored", () => {
870890
const onScroll = mock(() => {});
871891
const onCellClick = mock(() => {});

packages/react-wasm-table/src/adapter/event-manager.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -874,10 +874,7 @@ export class EventManager {
874874
detach(): void {
875875
// Preserve touchState across detach/attach cycles so that an in-progress
876876
// touch gesture (e.g. scroll) survives React effect re-runs.
877-
// Only clear touchState if there is no active gesture.
878-
if (!this.touchState) {
879-
// no active touch — safe to clear everything
880-
} else if (this.touchState.longPressTimer) {
877+
if (this.touchState?.longPressTimer) {
881878
clearTimeout(this.touchState.longPressTimer);
882879
this.touchState.longPressTimer = null;
883880
}

0 commit comments

Comments
 (0)