implement usethrottle

This commit is contained in:
Michael Fatemi 2021-06-27 19:14:41 -04:00
parent 0828fdc014
commit 15f1e50225
3 changed files with 56 additions and 3 deletions

View File

@ -1,8 +1,9 @@
import { useState } from 'react'; import { useCallback, useState } from 'react';
import UIButton from './UIButton'; import UIButton from './UIButton';
import UIPlacesAutocomplete from './UIPlacesAutocomplete'; import UIPlacesAutocomplete from './UIPlacesAutocomplete';
import UISecondaryBox from './UISecondaryBox'; import UISecondaryBox from './UISecondaryBox';
import UISecondaryHeader from './UISecondaryHeader'; import UISecondaryHeader from './UISecondaryHeader';
import useThrottle from './useThrottle';
const green = '#60f760'; const green = '#60f760';
const lightgrey = '#e0e0e0'; const lightgrey = '#e0e0e0';
@ -201,6 +202,7 @@ const dummyPeopleData: IPerson[] = [
]; ];
function People({ event }: { event: IEvent }) { function People({ event }: { event: IEvent }) {
const PADDING = '1rem'; const PADDING = '1rem';
// eslint-disable-next-line
const [people, setPeople] = useState(dummyPeopleData); const [people, setPeople] = useState(dummyPeopleData);
return ( return (
<div style={{ display: 'flex', flexDirection: 'column' }}> <div style={{ display: 'flex', flexDirection: 'column' }}>
@ -242,7 +244,13 @@ export default function Event({ event }: { event: IEvent }) {
const { name, group, formattedAddress, startTime, endTime } = event; const { name, group, formattedAddress, startTime, endTime } = event;
const [haveRide, setHaveRide] = useState(false); const [haveRide, setHaveRide] = useState(false);
const [locationPlaceId, setLocationPlaceId] = useState<string>(null!); const [locationPlaceId, setLocationPlaceId] = useState<string>(null!);
const [interested, setInterested] = useState(false); const [interested, toggleInterested] = useState(false);
const toggleInterestedThrottled = useThrottle(
useCallback(() => {
toggleInterested((i) => !i);
}, []),
500
);
return ( return (
<UISecondaryBox> <UISecondaryBox>
@ -250,7 +258,7 @@ export default function Event({ event }: { event: IEvent }) {
<GroupName name={group} /> <GroupName name={group} />
<Details {...{ startTime, endTime, formattedAddress }} /> <Details {...{ startTime, endTime, formattedAddress }} />
<UIButton <UIButton
onClick={() => setInterested((i) => !i)} onClick={toggleInterestedThrottled}
style={{ style={{
backgroundColor: interested ? green : lightgrey, backgroundColor: interested ? green : lightgrey,
color: interested ? 'white' : 'black', color: interested ? 'white' : 'black',

View File

@ -0,0 +1,38 @@
import { useMemo } from 'react';
function throttle<F extends (...args: any) => any, T>(
this: T,
callback: F,
limit: number,
thisArg?: T
): F {
let waiting = false;
let pendingArgs: null | any[] = null;
const scope = thisArg ?? this;
// @ts-ignore
const fn = function (this: T, ...args: any[]) {
if (!waiting) {
callback.apply(this, args);
waiting = true;
setTimeout(() => {
if (pendingArgs !== null) {
callback.apply(this, pendingArgs);
pendingArgs = null;
}
waiting = false;
}, limit);
} else {
pendingArgs = args;
}
};
fn.bind(scope);
// @ts-ignore
return fn;
}
export default function useThrottle<F extends (...args: any) => any>(
fn: F,
limit: number
) {
return useMemo(() => throttle(fn, limit), [fn, limit]);
}

View File

@ -0,0 +1,7 @@
import { useCallback, useState } from 'react';
export default function useToggle(initial: boolean) {
const [value, setValue] = useState(initial);
const toggle = useCallback(() => setValue((v) => !v), []);
return [value, toggle];
}