Skip to content

Commit

Permalink
fix: transformThrottle was actually buggy
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillgroshkov committed Sep 10, 2024
1 parent 5d684e1 commit c4e8f4c
Show file tree
Hide file tree
Showing 2 changed files with 232 additions and 223 deletions.
20 changes: 14 additions & 6 deletions src/stream/transform/transformThrottle.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Transform } from 'node:stream'
import {
_ms,
_since,
DeferredPromise,
localTime,
NumberOfMilliseconds,
Expand Down Expand Up @@ -75,7 +76,7 @@ export function transformThrottle<T>(opt: TransformThrottleOptions): TransformTy
paused = pDefer()
if (debug) {
console.log(
`${localTime.now().toPretty()} transformThrottle activated: ${count} items passed in ${_ms(interval * 1000)}, will pause for ${_ms(interval * 1000 - (Date.now() - start))}`,
`${localTime.now().toPretty()} transformThrottle activated: ${count} items passed in ${_since(start)}, will pause for ${_ms(interval * 1000 - (Date.now() - start))}`,
)
}
}
Expand All @@ -89,16 +90,23 @@ export function transformThrottle<T>(opt: TransformThrottleOptions): TransformTy
})

function onInterval(transform: Transform): void {
if (!paused) return
if (paused) {
if (debug) {
console.log(`${localTime.now().toPretty()} transformThrottle resumed`)
}

if (debug) {
console.log(`${localTime.now().toPretty()} transformThrottle resumed`)
paused.resolve()
paused = undefined
} else {
if (debug) {
console.log(
`${localTime.now().toPretty()} transformThrottle passed ${count} (of max ${throughput}) items in ${_since(start)}`,
)
}
}

count = 0
start = Date.now()
timeout = setTimeout(() => onInterval(transform), interval * 1000)
paused.resolve()
paused = undefined
}
}
Loading

0 comments on commit c4e8f4c

Please sign in to comment.