Skip to content

Commit f980ff6

Browse files
committed
Changed debounce to throttle for resize operations
1 parent bfad8da commit f980ff6

File tree

3 files changed

+19
-15
lines changed

3 files changed

+19
-15
lines changed

react-spaces/src/Globals/Debounce.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export function throttle<F extends ((...args: any) => any)>(callback: F, limit : number) {
2+
var wait = false; // Initially, we're not waiting
3+
return function (...args: any) { // We return a throttled function
4+
if (!wait) { // If we're not waiting
5+
callback(...args); // Execute users function
6+
wait = true; // Prevent future invocations
7+
setTimeout(function () { // After a period of time
8+
wait = false; // And allow future invocations
9+
}, limit);
10+
}
11+
}
12+
}

react-spaces/src/Resizable.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as React from 'react';
22
import './Styles.css';
3-
import { debounce } from './Globals/Debounce';
3+
import { throttle } from './Globals/Throttle';
44
import { ResizeType } from './Globals/Types';
55
import * as PropTypes from 'prop-types';
66

@@ -16,6 +16,8 @@ interface IProps {
1616
onResizeEnd?: () => void
1717
}
1818

19+
const RESIZE_THROTTLE = 10;
20+
1921
export const Resizable : React.FC<IProps> = (props) => {
2022
const resize = (originalX: number, originalY: number, x: number, y: number) => {
2123
const adjustmentX =
@@ -42,8 +44,8 @@ export const Resizable : React.FC<IProps> = (props) => {
4244
let moved = false;
4345

4446
const touchResize = (e: TouchEvent) => resizing && resize(originalTouchX, originalTouchY, e.touches[0].pageX, e.touches[0].pageY);
45-
const debouncedTouchResize = debounce<typeof touchResize>(touchResize, 10);
46-
const withPreventDefault = (e: TouchEvent) => { moved = true; e.preventDefault(); e.stopImmediatePropagation(); debouncedTouchResize(e); };
47+
const throttledTouchResize = throttle<typeof touchResize>(touchResize, RESIZE_THROTTLE);
48+
const withPreventDefault = (e: TouchEvent) => { moved = true; e.preventDefault(); e.stopImmediatePropagation(); throttledTouchResize(e); };
4749
const removeListener = () => {
4850
resizing = false;
4951
window.removeEventListener('touchmove', withPreventDefault);
@@ -64,8 +66,8 @@ export const Resizable : React.FC<IProps> = (props) => {
6466
let moved = false;
6567

6668
const mouseResize = (e: MouseEvent) => resizing && resize(originalMouseX, originalMouseY, e.pageX, e.pageY);
67-
const debouncedMouseResize = debounce<typeof mouseResize>(mouseResize, 10);
68-
const withPreventDefault = (e: MouseEvent) => { moved = true; e.preventDefault(); e.stopImmediatePropagation(); debouncedMouseResize(e); };
69+
const throttledMouseResize = throttle<typeof mouseResize>(mouseResize, RESIZE_THROTTLE);
70+
const withPreventDefault = (e: MouseEvent) => { moved = true; e.preventDefault(); e.stopImmediatePropagation(); throttledMouseResize(e); };
6971
const removeListener = () => {
7072
resizing = false;
7173
window.removeEventListener('mousemove', withPreventDefault);

0 commit comments

Comments
 (0)