Skip to content

Commit

Permalink
Debounce
Browse files Browse the repository at this point in the history
  • Loading branch information
leegee committed Oct 15, 2024
1 parent 2e5b6c3 commit 50e32f8
Showing 1 changed file with 31 additions and 6 deletions.
37 changes: 31 additions & 6 deletions src/components/GridInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,20 @@ interface GridInputProps {
gridIndex: number;
}

const debounce = (func: Function, delay: number) => {
let timeoutId: NodeJS.Timeout | null = null;

return (...args: any) => {
if (timeoutId) {
clearTimeout(timeoutId);
}

timeoutId = setTimeout(() => {
func(...args);
}, delay);
};
};

const GridInput: React.FC<GridInputProps> = ({ gridIndex }) => {
const { grids, setGrid, updateNoteVelocity, setOrUpdateNoteInGrid } = useMusicStore();
const grid = grids[gridIndex];
Expand Down Expand Up @@ -44,6 +58,11 @@ const GridInput: React.FC<GridInputProps> = ({ gridIndex }) => {
}
}, [isCtrlPressed, gridIndex, setOrUpdateNoteInGrid, updateNoteVelocity, grids]);

const debouncedToggleNoteRef = useRef(debounce(toggleNote, 25));
// Update the ref whenever toggleNote changes
useEffect(() => {
debouncedToggleNoteRef.current = debounce(toggleNote, 25);
}, [toggleNote]);

const handleMouseDown = (pitch: number, e: React.MouseEvent<HTMLDivElement>) => {
const beatIndex = Number(e.currentTarget.dataset.beat);
Expand All @@ -58,19 +77,25 @@ const GridInput: React.FC<GridInputProps> = ({ gridIndex }) => {

// Set dragging note to the existing note or create a new one
setDraggingNote(note || { pitch, velocity: 75 });
}
else {
} else {
toggleNote(pitch, beatIndex); // Call toggleNote with the updated signature
}
};

const handleMouseMove = useCallback((e: MouseEvent) => {
if (draggingNote && isCtrlPressed && gridRef.current) {
if (draggingNote && isCtrlPressed) {
if (!gridRef.current?.clientWidth) return;

// Use the dragging note's pitch and calculate the velocity based on the mouse Y position
const velocity = Math.max(0, Math.min(127, 127 - e.clientY / 4));
const beatIndex = Number((e.currentTarget as HTMLElement)?.dataset.beat);
toggleNote(draggingNote.pitch, beatIndex, velocity);

// Instead of trying to find the beatIndex here, we can keep track of it when starting the drag
const beatIndex = Math.floor(e.clientX / (gridRef.current?.clientWidth / grid.numColumns)); // Adjust based on your layout
// toggleNote(draggingNote.pitch, beatIndex, velocity); // Use stored pitch from dragging note
debouncedToggleNoteRef.current(draggingNote.pitch, beatIndex, velocity); // Use debounced function
}
}, [draggingNote, isCtrlPressed, toggleNote]);
}, [draggingNote, grid.numColumns, isCtrlPressed]);


const handleMouseUp = () => {
setIsCtrlPressed(false);
Expand Down

0 comments on commit 50e32f8

Please sign in to comment.