@basic-streams/from-iterable
Advanced tools
Comparing version 0.0.5 to 0.0.6
{ | ||
"name": "@basic-streams/from-iterable", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"description": "fromIterable operator for basic-streams", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -13,18 +13,7 @@ # [@basic-streams](https://github.com/rpominov/basic-streams)/from-iterable | ||
Given an `iterable`, returns a stream that produces items from that iterable. If | ||
an `interval` is provided the items will be spread in time. Interval is a number | ||
of milliseconds by which items should be spread. The first item also will be | ||
delayed by that interval. If the interval is `0` the items will be produced as | ||
soon as possible but still asynchronously. | ||
Transforms an `iterable` into a stream. | ||
Also, you can provide a custom `scheduler`, a function that creates a stream | ||
that produces an event after a given ammount of milliseconds. By default | ||
[`later`][later] is used as a scheduler. | ||
```js | ||
import fromIterable from "@basic-streams/from-iterable" | ||
import later from "@basic-streams/later" | ||
// | ||
// simplest case | ||
fromIterable([1, 2, 3])(x => { | ||
@@ -37,6 +26,12 @@ console.log(x) | ||
// > 3 | ||
``` | ||
// | ||
// with an interval | ||
fromIterable([1, 2, 3], 10)(x => { | ||
If an `interval` is provided the items will be spread in time by that ammount of | ||
milliseconds, with the first one delayed. If the interval is `0` the items will | ||
be produced as soon as possible but still asynchronously. | ||
```js | ||
import fromIterable from "@basic-streams/from-iterable" | ||
fromIterable([1, 2, 3], 5000)(x => { | ||
console.log(x) | ||
@@ -49,6 +44,11 @@ }) | ||
// _________1_________2_________3 | ||
// ____1____2____3 | ||
``` | ||
// | ||
// with a generator function | ||
Note that iterable is consumed lazily, meaning that `next()` is called only when | ||
value is needed. | ||
```js | ||
import fromIterable from "@basic-streams/from-iterable" | ||
function* generator() { | ||
@@ -60,3 +60,3 @@ const startTime = Date.now() | ||
} | ||
fromIterable(generator(), 10)(x => { | ||
fromIterable(generator(), 5000)(x => { | ||
console.log(x) | ||
@@ -66,14 +66,20 @@ }) | ||
// > 0 | ||
// > 10 | ||
// > 20 | ||
// > 5000 | ||
// > 10000 | ||
// 0 10 20 | ||
// _________._________._________. | ||
// 0 5000 10000 | ||
// ____.____.____. | ||
``` | ||
// | ||
// with a custom scheduler | ||
You can provide a custom `scheduler`, a function that creates a stream producing | ||
an event after a given time. By default [`later`][later] is used as a scheduler. | ||
```js | ||
import fromIterable from "@basic-streams/from-iterable" | ||
import later from "@basic-streams/later" | ||
function scheduler(time) { | ||
return later(time / 2) | ||
} | ||
fromIterable([1, 2, 3], 10, scheduler)(x => { | ||
fromIterable([1, 2, 3], 6000, scheduler)(x => { | ||
console.log(x) | ||
@@ -86,3 +92,3 @@ }) | ||
// ____1____2____3 | ||
// __1__2__3 | ||
``` | ||
@@ -89,0 +95,0 @@ |
10825
93