Chore: Create useDebounce hook (#4470)

* create useDebounce hook

* fix types

* apply useDebounce hook

* migrate to use-debounce lib

* remove useless useDebounce
This commit is contained in:
Gleidson Daniel Silva 2022-09-16 12:19:07 -03:00 committed by Diego Mello
parent 661b3f9a22
commit 0e907402b8
5 changed files with 31 additions and 26 deletions

View File

@ -1,3 +1,5 @@
import { useDebouncedCallback } from 'use-debounce';
export function debounce(func: Function, wait?: number, immediate?: boolean) { export function debounce(func: Function, wait?: number, immediate?: boolean) {
let timeout: ReturnType<typeof setTimeout> | null; let timeout: ReturnType<typeof setTimeout> | null;
function _debounce(...args: any[]) { function _debounce(...args: any[]) {
@ -21,3 +23,7 @@ export function debounce(func: Function, wait?: number, immediate?: boolean) {
_debounce.stop = () => clearTimeout(timeout!); _debounce.stop = () => clearTimeout(timeout!);
return _debounce; return _debounce;
} }
export function useDebounce(func: (...args: any) => any, wait?: number): (...args: any[]) => void {
return useDebouncedCallback(func, wait || 1000);
}

View File

@ -1,4 +1,4 @@
import React, { useEffect, useState, useCallback } from 'react'; import React, { useEffect, useState } from 'react';
import { FlatList } from 'react-native'; import { FlatList } from 'react-native';
import { RouteProp } from '@react-navigation/native'; import { RouteProp } from '@react-navigation/native';
import { StackNavigationOptions, StackNavigationProp } from '@react-navigation/stack'; import { StackNavigationOptions, StackNavigationProp } from '@react-navigation/stack';
@ -24,7 +24,7 @@ import DropdownItemHeader from './Dropdown/DropdownItemHeader';
import styles from './styles'; import styles from './styles';
import { ICannedResponse } from '../../definitions/ICannedResponse'; import { ICannedResponse } from '../../definitions/ICannedResponse';
import { ChatsStackParamList } from '../../stacks/types'; import { ChatsStackParamList } from '../../stacks/types';
import { getRoomTitle, getUidDirectMessage, debounce } from '../../lib/methods/helpers'; import { getRoomTitle, getUidDirectMessage, useDebounce } from '../../lib/methods/helpers';
import { Services } from '../../lib/services'; import { Services } from '../../lib/services';
import { ILivechatDepartment } from '../../definitions/ILivechatDepartment'; import { ILivechatDepartment } from '../../definitions/ILivechatDepartment';
import { useAppSelector } from '../../lib/hooks'; import { useAppSelector } from '../../lib/hooks';
@ -88,7 +88,7 @@ const CannedResponsesListView = ({ navigation, route }: ICannedResponsesListView
} }
}; };
const getDepartments = debounce(async () => { const getDepartments = useDebounce(async () => {
try { try {
const res = await Services.getDepartments(); const res = await Services.getDepartments();
if (res.success) { if (res.success) {
@ -187,12 +187,9 @@ const CannedResponsesListView = ({ navigation, route }: ICannedResponsesListView
} }
}, [departments, cannedResponses]); }, [departments, cannedResponses]);
const searchCallback = useCallback( const searchCallback = useDebounce(async (text = '', department = '', depId = '') => {
debounce(async (text = '', department = '', depId = '') => { await getListCannedResponse({ text, department, depId, debounced: true });
await getListCannedResponse({ text, department, depId, debounced: true }); }, 1000);
}, 1000),
[]
); // use debounce with useCallback https://stackoverflow.com/a/58594890
useEffect(() => { useEffect(() => {
getRoomFromDb(); getRoomFromDb();

View File

@ -10,7 +10,7 @@ import ActivityIndicator from '../../containers/ActivityIndicator';
import I18n from '../../i18n'; import I18n from '../../i18n';
import StatusBar from '../../containers/StatusBar'; import StatusBar from '../../containers/StatusBar';
import log from '../../lib/methods/helpers/log'; import log from '../../lib/methods/helpers/log';
import { debounce, isIOS } from '../../lib/methods/helpers'; import { isIOS, useDebounce } from '../../lib/methods/helpers';
import SafeAreaView from '../../containers/SafeAreaView'; import SafeAreaView from '../../containers/SafeAreaView';
import * as HeaderButton from '../../containers/HeaderButton'; import * as HeaderButton from '../../containers/HeaderButton';
import * as List from '../../containers/List'; import * as List from '../../containers/List';
@ -81,10 +81,10 @@ const DiscussionsView = ({ navigation, route }: IDiscussionsViewProps): React.Re
} }
}; };
const onSearchChangeText = debounce(async (text: string) => { const onSearchChangeText = useDebounce(async (text: string) => {
setIsSearching(true); setIsSearching(true);
await load(text); await load(text);
}, 300); }, 500);
const onCancelSearchPress = () => { const onCancelSearchPress = () => {
setIsSearching(false); setIsSearching(false);
@ -145,20 +145,16 @@ const DiscussionsView = ({ navigation, route }: IDiscussionsViewProps): React.Re
navigation.setOptions(options); navigation.setOptions(options);
}, [navigation, isSearching]); }, [navigation, isSearching]);
const onDiscussionPress = debounce( const onDiscussionPress = (item: TThreadModel) => {
(item: TThreadModel) => { if (item.drid && item.t) {
if (item.drid && item.t) { navigation.push('RoomView', {
navigation.push('RoomView', { rid: item.drid,
rid: item.drid, prid: item.rid,
prid: item.rid, name: item.msg,
name: item.msg, t
t });
}); }
} };
},
1000,
true
);
const renderItem = ({ item }: { item: IMessageFromServer }) => ( const renderItem = ({ item }: { item: IMessageFromServer }) => (
<Item <Item

View File

@ -137,6 +137,7 @@
"ua-parser-js": "^1.0.2", "ua-parser-js": "^1.0.2",
"uri-js": "^4.4.1", "uri-js": "^4.4.1",
"url-parse": "1.5.10", "url-parse": "1.5.10",
"use-debounce": "^8.0.4",
"use-deep-compare-effect": "1.6.1", "use-deep-compare-effect": "1.6.1",
"xregexp": "5.0.2", "xregexp": "5.0.2",
"yup": "^0.32.11" "yup": "^0.32.11"

View File

@ -20213,6 +20213,11 @@ use-composed-ref@^1.3.0:
resolved "https://registry.yarnpkg.com/use-composed-ref/-/use-composed-ref-1.3.0.tgz#3d8104db34b7b264030a9d916c5e94fbe280dbda" resolved "https://registry.yarnpkg.com/use-composed-ref/-/use-composed-ref-1.3.0.tgz#3d8104db34b7b264030a9d916c5e94fbe280dbda"
integrity sha512-GLMG0Jc/jiKov/3Ulid1wbv3r54K9HlMW29IWcDFPEqFkSO2nS0MuefWgMJpeHQ9YJeXDL3ZUF+P3jdXlZX/cQ== integrity sha512-GLMG0Jc/jiKov/3Ulid1wbv3r54K9HlMW29IWcDFPEqFkSO2nS0MuefWgMJpeHQ9YJeXDL3ZUF+P3jdXlZX/cQ==
use-debounce@^8.0.4:
version "8.0.4"
resolved "https://registry.yarnpkg.com/use-debounce/-/use-debounce-8.0.4.tgz#27e93b2f010bd0b8ad06e9fc7de891d9ee5d6b8e"
integrity sha512-fGqsYQzl8kLHF2QpQSgIwgOgJmnh6j5L6SIzQiHdLfwp3q1egUL3btq5Bg2SJysH6A0ILLgT2IqXZKoNJr0nFw==
use-deep-compare-effect@1.6.1: use-deep-compare-effect@1.6.1:
version "1.6.1" version "1.6.1"
resolved "https://registry.yarnpkg.com/use-deep-compare-effect/-/use-deep-compare-effect-1.6.1.tgz#061a0ac5400aa0461e33dddfaa2a98bca873182a" resolved "https://registry.yarnpkg.com/use-deep-compare-effect/-/use-deep-compare-effect-1.6.1.tgz#061a0ac5400aa0461e33dddfaa2a98bca873182a"