Skip to content

Commit

Permalink
refactor(frontend): rename parameters of waitReady (dfinity#1845)
Browse files Browse the repository at this point in the history
# Motivation

I was having a look at the code and just felt like renaming the
parameters of `waitReady` could improve readability.

Also noticed that we could eliminate duplicate code by reusing
`waitForMilliseconds` function.

---------

Signed-off-by: David Dal Busco <[email protected]>
  • Loading branch information
peterpeterparker authored Jul 24, 2024
1 parent 78b83a8 commit 17dcc26
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/frontend/src/lib/services/actions.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const waitWalletReady = async (isDisabled: () => boolean): Promise<'ready
busy.start({ msg: hold_loading_wallet });

// 20 tries with a delay of 500ms each = max 10 seconds
const result = await waitReady({ count: 20, isDisabled });
const result = await waitReady({ retries: 20, isDisabled });

busy.stop();

Expand Down
16 changes: 9 additions & 7 deletions src/frontend/src/lib/utils/timeout.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,27 @@ export const waitForMilliseconds = (milliseconds: number): Promise<void> =>
});

export const waitReady = async ({
count,
isDisabled
retries,
isDisabled,
intervalInMs = 500
}: {
count: number;
retries: number;
isDisabled: () => boolean;
intervalInMs?: number;
}): Promise<'ready' | 'timeout'> => {
const disabled = isDisabled();

if (!disabled) {
return 'ready';
}

const nextCount = count - 1;
const remainingRetries = retries - 1;

if (nextCount === 0) {
if (remainingRetries === 0) {
return 'timeout';
}

await new Promise((resolve) => setTimeout(resolve, 500));
await waitForMilliseconds(intervalInMs);

return waitReady({ count: nextCount, isDisabled });
return waitReady({ retries: remainingRetries, isDisabled });
};

0 comments on commit 17dcc26

Please sign in to comment.