Skip to content

Commit

Permalink
clearTimeoutEx
Browse files Browse the repository at this point in the history
  • Loading branch information
arietrouw committed Jan 18, 2024
1 parent ade198e commit 14319d0
Show file tree
Hide file tree
Showing 3 changed files with 330 additions and 297 deletions.
31 changes: 31 additions & 0 deletions .yarn/versions/a7200791.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
releases:
"@xylabs/api": patch
"@xylabs/array": patch
"@xylabs/arraybuffer": patch
"@xylabs/assert": patch
"@xylabs/axios": patch
"@xylabs/bignumber": patch
"@xylabs/buffer": patch
"@xylabs/crypto": patch
"@xylabs/decimal-precision": patch
"@xylabs/delay": patch
"@xylabs/error": patch
"@xylabs/eth-address": patch
"@xylabs/exists": patch
"@xylabs/forget": patch
"@xylabs/function-name": patch
"@xylabs/hex": patch
"@xylabs/jest-helpers": patch
"@xylabs/libauth": patch
"@xylabs/lodash": patch
"@xylabs/log": patch
"@xylabs/logger": patch
"@xylabs/platform": patch
"@xylabs/profile": patch
"@xylabs/promise": patch
"@xylabs/sdk-js": patch
"@xylabs/set": patch
"@xylabs/static-implements": patch
"@xylabs/timer": patch
"@xylabs/typeof": patch
"@xylabs/url": patch
11 changes: 9 additions & 2 deletions packages/timer/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
interface TimeoutInfo {
delay: number
func: () => void
id: string
}

let timeouts: TimeoutInfo[] = []
Expand All @@ -14,12 +15,18 @@ const timerFunc = () => {
timeout.func()
}
const notFiring = timeouts.filter((timeout) => timeout.delay > 100)
timeouts = notFiring.map((timeout) => ({ delay: timeout.delay - 100, func: timeout.func }))
timeouts = notFiring.map((timeout) => ({ delay: timeout.delay - 100, func: timeout.func, id: timeout.id }))
}

export const setTimeoutEx = (func: () => void, delay: number) => {
if (timeouts.length === 0) {
setTimeout(timerFunc, 100)
}
timeouts.push({ delay, func })
const id = `${Date.now()}|${Math.random() * 9_999_999_999}`
timeouts.push({ delay, func, id })
return id
}

export const clearTimeoutEx = (id: string) => {
timeouts = timeouts.filter((timeout) => timeout.id !== id)
}
Loading

0 comments on commit 14319d0

Please sign in to comment.