Skip to content

Commit

Permalink
refactor(hooks): swap to ref
Browse files Browse the repository at this point in the history
  • Loading branch information
steveoh committed Nov 20, 2024
1 parent 4ad3dd2 commit d463ee5
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions packages/utilities/src/hooks/useViewLoading.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { useEffect, useState } from 'react';

let timeoutId: NodeJS.Timeout | null = null;
import { useEffect, useRef, useState } from 'react';

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

useEffect(() => {
if (!view) {
Expand All @@ -15,20 +14,20 @@ export default function useViewLoading(

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

if (!updating) {
if (timeoutId) {
clearTimeout(timeoutId);
timeoutId = null;
if (timeoutId.current) {
clearTimeout(timeoutId.current);
timeoutId.current = null;
}
setIsLoading(false);
} else {
timeoutId = setTimeout(() => {
timeoutId.current = setTimeout(() => {
setIsLoading(true);
timeoutId = null;
timeoutId.current = null;
}, debounceDuration);
}
});
Expand Down

0 comments on commit d463ee5

Please sign in to comment.