Skip to content

Commit

Permalink
Prevent request stacking
Browse files Browse the repository at this point in the history
  • Loading branch information
justjanne committed Aug 16, 2022
1 parent aaf7bf7 commit 07b30c1
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions src/hooks/useUserOnboardingContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,26 @@ export function useUserOnboardingContext(): UserOnboardingContext | null {
);

useEffect(() => {
cli.on(ClientEvent.AccountData, handler);
const handle = setInterval(handler, USER_ONBOARDING_CONTEXT_INTERVAL);
handler();
let handle;

This comment has been minimized.

Copy link
@t3chguy

t3chguy Aug 16, 2022

Member

sorry for all the nits
could this get a type 👼 ?

let enabled = true;
const repeater = async () => {
if (handle) {
clearTimeout(handle);
handle = null;
}
await handler();
if (enabled) {
handle = setTimeout(repeater, USER_ONBOARDING_CONTEXT_INTERVAL);
}
};
repeater().catch(err => logger.warn("could not update user onboarding context", err));
cli.on(ClientEvent.AccountData, repeater);
return () => {
cli.off(ClientEvent.AccountData, handler);
enabled = false;
cli.off(ClientEvent.AccountData, repeater);
if (handle) {
clearInterval(handle);
clearTimeout(handle);
handle = null;
}
};
}, [cli, handler]);
Expand Down

0 comments on commit 07b30c1

Please sign in to comment.