Skip to content

Commit

Permalink
fix(hooks): debounce useViewLoading
Browse files Browse the repository at this point in the history
this should help smooth out any items using this hook.
  • Loading branch information
steveoh committed Nov 20, 2024
1 parent 22a02d9 commit 41f4cc3
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions packages/utilities/src/hooks/useViewLoading.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,34 @@
import { useEffect, useState } from 'react';

export default function useViewLoading(view: __esri.MapView | null) {
let timeoutId: NodeJS.Timeout | null = null;

export default function useViewLoading(
view: __esri.MapView | null,
debounceDuration = 500,
) {
const [isLoading, setIsLoading] = useState(false);

useEffect(() => {
const init = () => {
view?.watch('updating', (updating: boolean) => setIsLoading(updating));
};

if (view) {
init();
if (!view) {
return;
}

view.when(() => {
view.watch('updating', (updating: boolean) => {
if (timeoutId) {
return;
}

if (updating) {
setIsLoading(true);
} else {
timeoutId = setTimeout(() => {
setIsLoading(false);
timeoutId = null;
}, debounceDuration);
}
});
});
}, [view]);

return isLoading;
Expand Down

0 comments on commit 41f4cc3

Please sign in to comment.