streaming-iterables
Advanced tools
Comparing version 6.1.0 to 6.2.0
@@ -0,1 +1,3 @@ | ||
/// <reference lib="esnext.asynciterable" /> | ||
export declare type AnyIterable<T> = Iterable<T> | AsyncIterable<T>; | ||
@@ -138,2 +140,6 @@ | ||
export declare function throttle<T>(limit: number, interval: number): (iterable: AnyIterable<T>) => AsyncGenerator<T>; | ||
export declare function throttle<T>(limit: number, interval: number, iterable: AnyIterable<T>): AsyncGenerator<T>; | ||
export declare function time(config?: TimeConfig): CurriedTimeResult; | ||
@@ -140,0 +146,0 @@ |
@@ -677,2 +677,45 @@ (function (global, factory) { | ||
const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms)); | ||
function _throttle(limit, interval, iterable) { | ||
if (!Number.isFinite(limit)) { | ||
throw new TypeError('Expected `limit` to be a finite number'); | ||
} | ||
if (limit <= 0) { | ||
throw new TypeError('Expected `limit` to be greater than 0'); | ||
} | ||
if (!Number.isFinite(interval)) { | ||
throw new TypeError('Expected `interval` to be a finite number'); | ||
} | ||
return (async function* __throttle() { | ||
let sent = 0; | ||
let time; | ||
for await (const val of iterable) { | ||
if (sent < limit) { | ||
if (typeof time === 'undefined') { | ||
time = Date.now(); | ||
} | ||
sent++; | ||
yield val; | ||
continue; | ||
} | ||
// Only wait if the interval hasn't already passed while we were | ||
// yielding the previous values. | ||
const elapsedMs = Date.now() - time; | ||
const waitFor = interval - elapsedMs; | ||
if (waitFor > 0) { | ||
await sleep(waitFor); | ||
} | ||
time = Date.now(); | ||
sent = 1; | ||
yield val; | ||
} | ||
})(); | ||
} | ||
function throttle(limit, interval, iterable) { | ||
if (iterable === undefined) { | ||
return (curriedIterable) => _throttle(limit, interval, curriedIterable); | ||
} | ||
return _throttle(limit, interval, iterable); | ||
} | ||
function addTime(a, b) { | ||
@@ -909,2 +952,3 @@ let seconds = a[0] + b[0]; | ||
exports.tap = tap; | ||
exports.throttle = throttle; | ||
exports.time = time; | ||
@@ -911,0 +955,0 @@ exports.transform = transform; |
{ | ||
"name": "streaming-iterables", | ||
"version": "6.1.0", | ||
"version": "6.2.0", | ||
"description": "A collection of utilities for async iterables. Designed to replace your streams.", | ||
@@ -5,0 +5,0 @@ "main": "./dist/index.js", |
@@ -64,2 +64,3 @@ # streaming-iterables πββοΈ | ||
- [`tap()`](#tap) | ||
- [`throttle()`](#throttle) | ||
- [`time()`](#time) | ||
@@ -467,2 +468,22 @@ - [`transform()`](#transform) | ||
### throttle | ||
```ts | ||
function throttle<T>(limit: number, interval: number, iterable: AnyIterable<T>): AsyncGenerator<T> | ||
``` | ||
Throttles `iterable` at a rate of `limit` per `interval` without discarding data. Useful for throttling rate limited APIs. | ||
`limit` can be greater than 0 but less than `Infinity`. | ||
`interval` can be greater than or equal to 0 but less than `Infinity`. | ||
```ts | ||
import { throttle } from 'streaming-iterables' | ||
import { getPokemon, trainMonster } from 'iterable-pokedex' | ||
// load monsters at a maximum rate of 1 per second | ||
for await (const monster of throttle(1, 1000, getPokemon())) { | ||
await trainMonster(monster) | ||
} | ||
``` | ||
### time | ||
@@ -469,0 +490,0 @@ ```ts |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
92983
1959
582