Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 07b30c1

Browse files
committed
Prevent request stacking
1 parent aaf7bf7 commit 07b30c1

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

src/hooks/useUserOnboardingContext.ts

+18-5
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,26 @@ export function useUserOnboardingContext(): UserOnboardingContext | null {
8585
);
8686

8787
useEffect(() => {
88-
cli.on(ClientEvent.AccountData, handler);
89-
const handle = setInterval(handler, USER_ONBOARDING_CONTEXT_INTERVAL);
90-
handler();
88+
let handle;
89+
let enabled = true;
90+
const repeater = async () => {
91+
if (handle) {
92+
clearTimeout(handle);
93+
handle = null;
94+
}
95+
await handler();
96+
if (enabled) {
97+
handle = setTimeout(repeater, USER_ONBOARDING_CONTEXT_INTERVAL);
98+
}
99+
};
100+
repeater().catch(err => logger.warn("could not update user onboarding context", err));
101+
cli.on(ClientEvent.AccountData, repeater);
91102
return () => {
92-
cli.off(ClientEvent.AccountData, handler);
103+
enabled = false;
104+
cli.off(ClientEvent.AccountData, repeater);
93105
if (handle) {
94-
clearInterval(handle);
106+
clearTimeout(handle);
107+
handle = null;
95108
}
96109
};
97110
}, [cli, handler]);

0 commit comments

Comments
 (0)