UNPKG

873 BJavaScriptView Raw
1import { AsyncIterableX } from './asynciterablex';
2import { sleep } from './_sleep';
3import { throwIfAborted } from '../aborterror';
4class IntervalAsyncIterable extends AsyncIterableX {
5 constructor(dueTime) {
6 super();
7 this._dueTime = dueTime;
8 }
9 async *[Symbol.asyncIterator](signal) {
10 throwIfAborted(signal);
11 let i = 0;
12 while (1) {
13 await sleep(this._dueTime, signal);
14 yield i++;
15 }
16 }
17}
18/**
19 * Produces a new item in an async-iterable at the given interval cycle time.
20 *
21 * @export
22 * @param {number} dueTime The due time in milliseconds to spawn a new item.
23 * @returns {AsyncIterableX<number>} An async-iterable producing values at the specified interval.
24 */
25export function interval(dueTime) {
26 return new IntervalAsyncIterable(dueTime);
27}
28
29//# sourceMappingURL=interval.mjs.map