Skip to content

Commit

Permalink
feat: allowed milliseconds option for waitUntil
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan-Zayne committed Oct 29, 2024
1 parent 2fc230f commit 909c6db
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/cuddly-icons-sin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@zayne-labs/toolkit": patch
---

feat: allowed milliseconds option for waitUntil
24 changes: 18 additions & 6 deletions packages/toolkit/src/core/wait.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,36 @@ type Delay =
seconds: number;
};

export const waitUntil = (delay: Delay) => {
if (delay.seconds === 0 || delay.milliseconds === 0) return;
export const waitUntil = (delay: number | Delay) => {
if (typeof delay === "number" || delay.seconds === 0 || delay.milliseconds === 0) return;

const { promise, resolve } = PromiseWithResolvers();

const delayInMs = typeof delay.seconds === "number" ? delay.seconds * 1000 : delay.milliseconds;
const delayInMs =
typeof delay === "number"
? delay
: // eslint-disable-next-line unicorn/no-nested-ternary
typeof delay.seconds === "number"
? delay.seconds * 1000
: delay.milliseconds;

setTimeout(resolve, delayInMs);

return promise;
};

export const waitUntilSync = (delay: Delay) => {
if (delay.seconds === 0 || delay.milliseconds === 0) return;
export const waitUntilSync = (delay: number | Delay) => {
if (typeof delay === "number" || delay.seconds === 0 || delay.milliseconds === 0) return;

const startTime = performance.now();

const delayInMs = typeof delay.seconds === "number" ? delay.seconds * 1000 : delay.milliseconds;
const delayInMs =
typeof delay === "number"
? delay
: // eslint-disable-next-line unicorn/no-nested-ternary
typeof delay.seconds === "number"
? delay.seconds * 1000
: delay.milliseconds;

for (
let currentTime = startTime;
Expand Down

0 comments on commit 909c6db

Please sign in to comment.