@thi.ng/rstream
Version:
Reactive streams & subscription primitives for constructing dataflow graphs / pipelines
25 lines (24 loc) • 556 B
JavaScript
import { __optsWithID } from "./idgen.js";
import { stream } from "./stream.js";
const fromInterval = (delay, opts) => {
opts = __optsWithID("interval", {
num: Infinity,
...opts
});
return stream((stream2) => {
let i = 0;
let count = opts.num;
stream2.next(i++);
let id = setInterval(() => {
stream2.next(i++);
if (--count <= 0) {
clearInterval(id);
stream2.closeIn !== "never" && stream2.done();
}
}, delay);
return () => clearInterval(id);
}, opts);
};
export {
fromInterval
};