···
Operators in Wonka allow you to transform values from a source before they are sent to a sink. Wonka has the following operators.
`combine` two sources together to a single source. The emitted value will be a combination of the two sources, with all values from the first source being emitted with the first value of the second source _before_ values of the second source are emitted.
···
let sourceTwo = Wonka.fromArray([|4, 5, 6|]);
Wonka.combine(sourceOne, sourceTwo)
-
|> Wonka.subscribe((. (_valOne, _valTwo)) => print_int(_valOne + _valTwo));
/* Prints 56789 (1+4, 2+4, 3+4, 3+5, 3+6) to the console. */
···
subscribe(([valOne, valTwo]) => {
console.log(valOne + valTwo);
-
// Prints 56789 (1+4, 2+4, 3+4, 3+5, 3+6) to the console.
···
`concat` will combine two sources together, subscribing to the next source after the previous source completes.
-
let sourceOne = Wonka.fromArray([|1, 2, 3, 4, 5, 6|]);
-
let sourceTwo = Wonka.fromArray([|6, 5, 4, 3, 2, 1|]);
-
Wonka.concat([|sourceOne, sourceTwo|]) |> Wonka.subscribe((. _val) => print_int(_val));
-
/* Prints 1 2 3 4 5 6 6 5 4 3 2 1 to the console. */
import { fromArray, pipe, concat, subscribe } from 'wonka';
-
const sourceOne = fromArray([1, 2, 3, 4, 5, 6]);
-
const sourceTwo = fromArray([6, 5, 4, 3, 2, 1]);
concat([sourceOne, sourceTwo]),
-
// Prints 1 2 3 4 5 6 6 5 4 3 2 1 to the console.
···
···
`filter` will remove values from a source by passing them through an iteratee that returns a `bool`.
-
let source = Wonka.fromArray([|1, 2, 3, 4, 5, 6|]);
let isEven = (. n) => n mod 2 === 0;
-
source |> Wonka.filter(isEven) |> Wonka.subscribe((. _val) => print_int(_val));
/* Prints 246 to the console. */
import { fromArray, filter, subscribe } from 'wonka';
-
const source = fromArray([1, 2, 3, 4, 5, 6]);
const isEven = n => n % 2 === 0;
// Prints 246 to the console.
···
`map` will transform values from a source by passing them through an iteratee that returns a new value.
-
let source = Wonka.fromArray([|1, 2, 3, 4, 5, 6|]);
let square = (. n) => n * n;
-
source |> Wonka.map(square) |> Wonka.subscribe((. _val) => print_int(_val));
/* Prints 1 4 9 16 25 36 to the console. */
import { fromArray, pipe, map, subscribe } from 'wonka';
-
const source = fromArray([1, 2, 3, 4, 5, 6]);
const square = n => n * n;
// Prints 1 4 9 16 25 36 to the console.
···
-
`merge` two sources together into a single source.
let sourceA = Wonka.fromArray([|1, 2, 3|]);
let sourceB = Wonka.fromArray([|4, 5, 6|]);
-
Wonka.merge([|sourceA, sourceB|]) |> Wonka.subscribe((. _val) => print_int(_val));
-
/* Prints 1 2 3 4 5 6 to the console.
-
import { fromArray, pipe, merge, subscribe } from 'wonka';
const sourceOne = fromArray([1, 2, 3]);
const sourceTwo = fromArray([4, 5, 6]);
-
merge(sourceOne, sourceTwo)
-
// Prints 1 2 3 4 5 6 to the console.
···
let sourceOne = Wonka.fromPromise(promiseOne);
let sourceTwo = Wonka.fromPromise(promiseTwo);
-
let source = Wonka.concat([|sourceOne, sourceTwo|]);
-
|> Wonka.onEnd((.) => print_endline("onEnd"))
-
|> Wonka.subscribe((. _val) => print_endline(_val));
/* Logs ResolveOne after one second, then ResolveTwo after an additional second, then onEnd immediately. */
···
const sourceOne = fromPromise(promiseOne);
const sourceTwo = fromPromise(promiseTwo);
-
const source = concat([sourceOne, sourceTwo]);
// Logs ResolveOne after one second, then ResolveTwo after an additional second, then onEnd immediately.
···
Run a callback on each `Push` signal sent to the sink by the source.
-
let source = Wonka.fromArray([|1, 2, 3, 4, 5, 6|]);
-
|> Wonka.onPush((. _val) => print_string({j|Push $_val|j}))
-
|> Wonka.subscribe((. _val) => print_int(_val));
/* Prints Push 1 1 Push 2 2 Push 3 3 Push 4 4 Push 5 5 Push 6 6 to the console. */
import { fromArray, pipe, onPush, subscribe } from 'wonka';
-
const source = fromArray([1, 2, 3, 4, 5, 6]);
-
console.log(`Push ${val}`);
-
// Prints Push 1 1 Push 2 2 Push 3 3 Push 4 4 Push 5 5 Push 6 6 to the console.
···
Js.Global.setTimeout(() => resolve(. "Resolve"), 1000) |> ignore
-
let source = Wonka.fromPromise(promise);
-
|> Wonka.onStart((.) => print_endline("onStart"))
-
|> Wonka.subscribe((. _val) => print_endline(_val));
/* Logs onStart to the console, pauses for one second to allow the timeout to finish,
then logs "Resolve" to the console. */
···
-
const source = fromPromise(promise);
-
console.log('onStart');
// Logs onStart to the console, pauses for one second to allow the timeout to finish,
// then logs "Resolve" to the console.
Accumulate emitted values of a source in a accumulator, similar to JavaScript `reduce`.
-
let source = Wonka.fromArray([|1, 2, 3, 4, 5, 6|]);
-
|> Wonka.scan((. acc, x) => acc + x, 0)
-
|> Wonka.subscribe((. _val) => print_int(_val));
/* Prints 1 3 6 10 15 21 to the console. */
import { fromArray, pipe, scan, subscribe } from 'wonka';
-
const source = fromArray([1, 2, 3, 4, 5, 6]);
scan((acc, val) => acc + val),
// Prints 1 3 6 10 15 21 to the console.
-
`skip` the specified number of emissions from the source.
-
let source = Wonka.fromArray([|1, 2, 3, 4, 5, 6|]);
-
source |> Wonka.skip(2) |> Wonka.subscribe((. _val) => print_int(_val));
/* Prints 3 4 5 6 to the console, since the first two emissions from the source were skipped.
import { fromArray, pipe, skip, subscribe } from 'wonka';
-
const source = fromArray([1, 2, 3, 4, 5, 6]);
···
let source = Wonka.interval(100);
let notifier = Wonka.interval(500);
-
source |> Wonka.skipUntil(notifier) |> Wonka.subscribe((. _val) => print_int(_val));
/* Skips all values emitted by source (0, 1, 2, 3) until notifier emits at 500ms.
Then logs 4 5 6 7 8 9 10... to the console every 500ms. */
···
// Skips all values emitted by source (0, 1, 2, 3) until notifier emits at 500ms.
···
import { fromArray, pipe, skipWhile, subscribe } from 'wonka';
-
const source = fromArray([1, 2, 3, 4, 5, 6]);
skipWhile(val => val < 5),
// Prints 5 6 to the console, as 1 2 3 4 all return true for the predicate function.
`take` only a specified number of emissions from the source before completing. `take` is the opposite of `skip`.
-
let source = Wonka.fromArray([|1, 2, 3, 4, 5, 6|]);
-
source |> Wonka.take(3) |> Wonka.subscribe((. _val) => print_int(_val));
/* Prints 1 2 3 to the console. */
import { fromArray, pipe, take, subscribe } from 'wonka';
-
const source = fromArray([1, 2, 3, 4, 5, 6]);
// Prints 1 2 3 to the console.
···
`takeLast` will take only the last n emissions from the source.
-
let source = Wonka.fromArray([|1, 2, 3, 4, 5, 6|]);
-
source |> Wonka.takeLast(3) |> Wonka.subscribe((. _val) => print_int(_val));
/* Prints 4 5 6 to the console. */
import { fromArray, pipe, takeLast, subscribe } from 'wonka';
-
const source = fromArray([1, 2, 3, 4, 5, 6]);
// Prints 4 5 6 to the console.
···
let notifier = Wonka.interval(500);
-
|> Wonka.takeUntil(notifier)
-
|> Wonka.subscribe((. _val) => print_int(_val));
/* Pauses 100ms, prints 0, pauses 100ms, prints 1, pauses 100ms, prints 2, pauses 100ms,
prints 3, pauses 100, then completes (notifier emits). */
···
// Pauses 100ms, prints 0, pauses 100ms, prints 1, pauses 100ms, prints 2, pauses 100ms,
···
let source = Wonka.fromArray([|1, 2, 3, 4, 5, 6|]);
-
|> Wonka.takeWhile((. _val) => _val < 5)
-
|> Wonka.subscribe((. _val) => print_int(_val));
/* Prints 1 2 3 4 to the console. */
···
takeWhile(val => val < 5),
// Prints 1 2 3 4 to the console.
···
Operators in Wonka allow you to transform values from a source before they are sent to a sink. Wonka has the following operators.
+
Buffers emissions from an outer source and emits a buffer array of items every time an
+
inner source (notifier) emits.
+
This operator can be used to group values into a arrays on a source. The emitted values will
+
be sent when a notifier fires and will be arrays of all items before the notification event.
+
In combination with `interval` this can be used to group values in chunks regularly.
+
|> Wonka.buffer(Wonka.interval(100))
+
|> Wonka.subscribe((. buffer) => {
+
Js.Array.forEach(num => print_int(num), buffer);
+
/* Prints 1 2; 2 3 to the console. */
+
import { pipe, interval, buffer, take, subscribe } from 'wonka';
+
buffer.forEach(x => console.log(x));
+
); // Prints 1 2; 2 3 to the console.
`combine` two sources together to a single source. The emitted value will be a combination of the two sources, with all values from the first source being emitted with the first value of the second source _before_ values of the second source are emitted.
···
let sourceTwo = Wonka.fromArray([|4, 5, 6|]);
Wonka.combine(sourceOne, sourceTwo)
+
|> Wonka.subscribe((. (a, b)) => print_int(a + b));
/* Prints 56789 (1+4, 2+4, 3+4, 3+5, 3+6) to the console. */
···
subscribe(([valOne, valTwo]) => {
console.log(valOne + valTwo);
+
); // Prints 56789 (1+4, 2+4, 3+4, 3+5, 3+6) to the console.
···
`concat` will combine two sources together, subscribing to the next source after the previous source completes.
+
let sourceOne = Wonka.fromArray([|1, 2, 3|]);
+
let sourceTwo = Wonka.fromArray([|6, 5, 4|]);
+
Wonka.concat([|sourceOne, sourceTwo|])
+
|> Wonka.subscribe((. x) => print_int(x));
+
/* Prints 1 2 3 6 5 4 to the console. */
import { fromArray, pipe, concat, subscribe } from 'wonka';
+
const sourceOne = fromArray([1, 2, 3]);
+
const sourceTwo = fromArray([6, 5, 4]);
concat([sourceOne, sourceTwo]),
+
subscribe(val => console.log(val))
+
); // Prints 1 2 3 6 5 4 to the console.
+
`concatAll` will combine all sources emitted on an outer source together, subscribing to the
+
next source after the previous source completes.
+
It's very similar to `concat`, but instead accepts a source of sources as an input.
+
let sourceOne = Wonka.fromArray([|1, 2, 3|]);
+
let sourceTwo = Wonka.fromArray([|6, 5, 4|]);
+
Wonka.fromList([sourceOne, sourceTwo])
+
|> Wonka.subscribe((. x) => print_int(x));
+
/* Prints 1 2 3 6 5 4 to the console. */
+
import { pipe, fromArray, concatAll, subscribe } from 'wonka';
+
const sourceOne = fromArray([1, 2, 3]);
+
const sourceTwo = fromArray([6, 5, 4]);
+
fromArray([sourceOne, sourceTwo]),
+
subscribe(val => console.log(val))
+
); // Prints 1 2 3 6 5 4 to the console.
···
+
subscribe(val => console.log(val))
+
`delay` delays all emitted values of a source by the given amount of milliseconds.
+
> _Note:_ This operator is only available in JavaScript environments, and will be excluded
+
> when compiling natively.
+
|> Wonka.subscribe((. x) => print_int(x));
+
/* waits 10ms then prints 1, waits 10ms then prints 2, waits 10ms then ends */
+
import { pipe, fromArray, delay, subscribe } from 'wonka';
+
subscribe(val => console.log(val))
+
// waits 10ms then prints 1, waits 10ms then prints 2, waits 10ms then ends
+
`debounce` doesn't emit values of a source until no values have been emitted after
+
a given amount of milliseconds. Once this threshold of silence has been reached, the
+
last value that has been received will be emitted.
+
> _Note:_ This operator is only available in JavaScript environments, and will be excluded
+
> when compiling natively.
+
let sourceA = Wonka.interval(10)
+
let sourceB = Wonka.fromValue(1);
+
Wonka.concat([|sourceA, sourceB|])
+
|> Wonka.debounce((. _x) => 20)
+
|> Wonka.subscribe((. x) => print_int(x));
+
/* The five values from sourceA will be omitted */
+
/* After these values and after 20ms `1` will be logged */
+
import { pipe, interval, take, fromValue, concat, debounce, subscribe } from 'wonka';
+
const sourceA = pipe(interval(10), take(5));
+
const sourceB = fromValue(1);
+
concat([sourceA, sourceB])
+
subscribe(val => console.log(val))
+
// The five values from sourceA will be omitted
+
// After these values and after 20ms `1` will be logged
···
`filter` will remove values from a source by passing them through an iteratee that returns a `bool`.
let isEven = (. n) => n mod 2 === 0;
+
Wonka.fromArray([|1, 2, 3, 4, 5, 6|])
+
|> Wonka.filter(isEven)
+
|> Wonka.subscribe((. x) => print_int(x));
/* Prints 246 to the console. */
import { fromArray, filter, subscribe } from 'wonka';
const isEven = n => n % 2 === 0;
+
fromArray([1, 2, 3, 4, 5, 6]),
+
subscribe(val => console.log(val))
// Prints 246 to the console.
···
`map` will transform values from a source by passing them through an iteratee that returns a new value.
let square = (. n) => n * n;
+
Wonka.fromArray([|1, 2, 3, 4, 5, 6|])
+
|> Wonka.subscribe((. x) => print_int(x));
/* Prints 1 4 9 16 25 36 to the console. */
import { fromArray, pipe, map, subscribe } from 'wonka';
const square = n => n * n;
+
fromArray([1, 2, 3, 4, 5, 6]),
+
subscribe(val => console.log(val))
// Prints 1 4 9 16 25 36 to the console.
···
+
`merge` merges an array of sources together into a single source. It subscribes
+
to all sources that it's passed and emits all their values on the output source.
let sourceA = Wonka.fromArray([|1, 2, 3|]);
let sourceB = Wonka.fromArray([|4, 5, 6|]);
+
Wonka.merge([|sourceA, sourceB|])
+
|> Wonka.subscribe((. x) => print_int(x));
+
/* Prints 1 2 3 4 5 6 to the console. */
+
import { fromArray, pipe, merge, subscribe } from 'wonka';
+
const sourceOne = fromArray([1, 2, 3]);
+
const sourceTwo = fromArray([4, 5, 6]);
+
merge(sourceOne, sourceTwo),
+
subscribe((val) => console.log(val))
+
); // Prints 1 2 3 4 5 6 to the console.
+
`mergeAll` will merge all sources emitted on an outer source into a single one.
+
It's very similar to `merge`, but instead accepts a source of sources as an input.
+
> _Note:_ This operator is also exported as `flatten` which is just an alias for `mergeAll`
+
let sourceA = Wonka.fromArray([|1, 2, 3|]);
+
let sourceB = Wonka.fromArray([|4, 5, 6|]);
+
Wonka.fromList([sourceA, sourceB])
+
|> Wonka.subscribe((. x) => print_int(x));
+
/* Prints 1 2 3 4 5 6 to the console. */
+
import { pipe, fromArray, mergeAll, subscribe } from 'wonka';
const sourceOne = fromArray([1, 2, 3]);
const sourceTwo = fromArray([4, 5, 6]);
+
fromArray([sourceOne, sourceTwo]),
+
subscribe(val => console.log(val))
+
); // Prints 1 2 3 4 5 6 to the console.
+
`mergeMap` allows you to map values of an outer source to an inner source.
+
This allows you to create nested sources for each emitted value, which will
+
all be merged into a single source, like with `mergeAll`.
+
Unlike `concatMap` all inner sources will be subscribed to at the same time
+
and all their values will be emitted on the output source as they come in.
+
|> Wonka.mergeMap((. value) =>
+
Wonka.fromList([value - 1, value]))
+
|> Wonka.subscribe((. x) => print_int(x));
+
/* Prints 0 1 1 2 to the console. */
+
import { pipe, fromArray, mergeMap, subscribe } from 'wonka';
+
mergeMap(x => fromArray([x - 1, x])),
+
subscribe(val => console.log(val))
+
); // Prints 0 1 1 2 to the console.
···
let sourceOne = Wonka.fromPromise(promiseOne);
let sourceTwo = Wonka.fromPromise(promiseTwo);
+
Wonka.concat([|sourceOne, sourceTwo|])
+
|> Wonka.onEnd((.) => print_endline("onEnd"))
+
|> Wonka.subscribe((. x) => print_endline(x));
/* Logs ResolveOne after one second, then ResolveTwo after an additional second, then onEnd immediately. */
···
const sourceOne = fromPromise(promiseOne);
const sourceTwo = fromPromise(promiseTwo);
+
concat([sourceOne, sourceTwo]),
+
onEnd(() => console.log('onEnd')),
+
subscribe(val => console.log(val))
// Logs ResolveOne after one second, then ResolveTwo after an additional second, then onEnd immediately.
···
Run a callback on each `Push` signal sent to the sink by the source.
+
Wonka.fromArray([|1, 2, 3, 4, 5, 6|])
+
|> Wonka.onPush((. x) => print_string({j|Push $x|j}))
+
|> Wonka.subscribe((. x) => print_int(x));
/* Prints Push 1 1 Push 2 2 Push 3 3 Push 4 4 Push 5 5 Push 6 6 to the console. */
import { fromArray, pipe, onPush, subscribe } from 'wonka';
+
fromArray([1, 2, 3, 4, 5, 6]),
+
onPush(val => console.log(`Push ${val}`)),
+
subscribe(val => console.log(val))
+
); // Prints Push 1 1 Push 2 2 Push 3 3 Push 4 4 Push 5 5 Push 6 6 to the console.
···
Js.Global.setTimeout(() => resolve(. "Resolve"), 1000) |> ignore
+
Wonka.fromPromise(promise)
+
|> Wonka.onStart((.) => print_endline("onStart"))
+
|> Wonka.subscribe((. _val) => print_endline(_val));
/* Logs onStart to the console, pauses for one second to allow the timeout to finish,
then logs "Resolve" to the console. */
···
+
onStart(() => console.log('onStart')),
+
subscribe(val => console.log(val))
// Logs onStart to the console, pauses for one second to allow the timeout to finish,
// then logs "Resolve" to the console.
+
`sample` emits the previously emitted value from an outer source every time
+
an inner source (notifier) emits.
+
In combination with `interval` it can be used to get values from a noisy source
+
|> Wonka.sample(Wonka.interval(100))
+
|> Wonka.subscribe((. x) => print_int(x));
+
/* Prints 10 20 to the console. */
+
import { pipe, interval, sample, take, subscribe } from 'wonka';
+
subscribe(x => console.log(x))
+
); // Prints 10 20 to the console.
Accumulate emitted values of a source in a accumulator, similar to JavaScript `reduce`.
+
Wonka.fromArray([|1, 2, 3, 4, 5, 6|])
+
|> Wonka.scan((. acc, x) => acc + x, 0)
+
|> Wonka.subscribe((. x) => print_int(x));
/* Prints 1 3 6 10 15 21 to the console. */
import { fromArray, pipe, scan, subscribe } from 'wonka';
+
fromArray([1, 2, 3, 4, 5, 6]),
scan((acc, val) => acc + val),
+
subscribe(val => console.log(val))
// Prints 1 3 6 10 15 21 to the console.
+
`share` ensures that all subscriptions to the underlying source are shared.
+
By default Wonka's sources are lazy. They only instantiate themselves and begin
+
emitting signals when they're being subscribed to, since they're also immutable.
+
This means that when a source is used in multiple places, their underlying subscription
+
is not shared. Instead, the entire chain of sources and operators will be instantiated
+
The `share` operator prevents this by creating an output source that will reuse a single
+
subscription to the parent source, which will be unsubscribed from when no sinks are
+
listening to it anymore.
+
This is especially useful if you introduce side-effects to your sources,
+
for instance with `onStart`.
+
let source = Wonka.never
+
|> Wonka.onStart((.) => print_endline("start"))
+
/* Without share this would print "start" twice: */
+
import { pipe, never, onStart, share, publish } from 'wonka';
+
onStart(() => console.log('start')),
+
// Without share this would print "start" twice:
+
`skip` the specified number of emissions from the source.
+
Wonka.fromArray([|1, 2, 3, 4, 5, 6|])
+
|> Wonka.subscribe((. x) => print_int(x));
/* Prints 3 4 5 6 to the console, since the first two emissions from the source were skipped.
import { fromArray, pipe, skip, subscribe } from 'wonka';
+
fromArray([1, 2, 3, 4, 5, 6]),
+
subscribe(val => console.log(val))
···
let source = Wonka.interval(100);
let notifier = Wonka.interval(500);
+
|> Wonka.skipUntil(notifier)
+
|> Wonka.subscribe((. x) => print_int(x));
/* Skips all values emitted by source (0, 1, 2, 3) until notifier emits at 500ms.
Then logs 4 5 6 7 8 9 10... to the console every 500ms. */
···
+
subscribe(val => console.log(val))
// Skips all values emitted by source (0, 1, 2, 3) until notifier emits at 500ms.
···
import { fromArray, pipe, skipWhile, subscribe } from 'wonka';
+
fromArray([1, 2, 3, 4, 5, 6]),
skipWhile(val => val < 5),
+
subscribe(val => console.log(val))
// Prints 5 6 to the console, as 1 2 3 4 all return true for the predicate function.
+
`switchMap` allows you to map values of an outer source to an inner source.
+
The inner source's values will be emitted on the returned output source. If
+
a new inner source is returned, because the outer source emitted a new value
+
before the previous inner source completed, the inner source is closed and unsubscribed
+
This is similar to `concatMap` but instead of waiting for the last inner source to complete
+
before emitting values from the next, `switchMap` just cancels the previous inner source.
+
|> Wonka.switchMap((. _value) =>
+
|> Wonka.subscribe((. x) => print_int(x));
+
/* Prints 1 2 3 to the console. */
+
/* The inner interval is cancelled after its first value every time */
+
import { pipe, interval, switchMap, take, subscribe } from 'wonka';
+
// The inner interval is cancelled after its first value every time
+
switchMap(value => interval(40)),
+
subscribe(x => console.log(x))
+
); // Prints 1 2 3 to the console
+
`switchAll` will combined sources emitted on an outer source together, subscribing
+
to only one source at a time, and cancelling the previous inner source, when it hasn't
+
ended while the next inner source is created.
+
It's very similar to `switchMap`, but instead accepts a source of sources.
+
|> Wonka.map((. _value) =>
+
|> Wonka.subscribe((. x) => print_int(x));
+
/* Prints 1 2 3 to the console. */
+
import { pipe, interval, map, switchAll, take, subscribe } from 'wonka';
+
map(() => interval(40)),
+
subscribe(x => console.log(x))
+
); // Prints 1 2 3 to the console
+
These examples are practically identical to the `switchMap` examples, but note
+
that `map` was used instead of using `switchMap` directly. This is because combining
+
`map` with a subsequent `switchAll` is the same as using `switchMap`.
`take` only a specified number of emissions from the source before completing. `take` is the opposite of `skip`.
+
Wonka.fromArray([|1, 2, 3, 4, 5, 6|])
+
|> Wonka.subscribe((. x) => print_int(x));
/* Prints 1 2 3 to the console. */
import { fromArray, pipe, take, subscribe } from 'wonka';
+
fromArray([1, 2, 3, 4, 5, 6]),
+
subscribe(val => console.log(val))
// Prints 1 2 3 to the console.
···
`takeLast` will take only the last n emissions from the source.
+
Wonka.fromArray([|1, 2, 3, 4, 5, 6|]);
+
|> Wonka.subscribe((. x) => print_int(x));
/* Prints 4 5 6 to the console. */
import { fromArray, pipe, takeLast, subscribe } from 'wonka';
+
fromArray([1, 2, 3, 4, 5, 6]),
+
subscribe(val => console.log(val))
// Prints 4 5 6 to the console.
···
let notifier = Wonka.interval(500);
+
|> Wonka.takeUntil(notifier)
+
|> Wonka.subscribe((. x) => print_int(x));
/* Pauses 100ms, prints 0, pauses 100ms, prints 1, pauses 100ms, prints 2, pauses 100ms,
prints 3, pauses 100, then completes (notifier emits). */
···
+
subscribe(val => console.log(val))
// Pauses 100ms, prints 0, pauses 100ms, prints 1, pauses 100ms, prints 2, pauses 100ms,
···
let source = Wonka.fromArray([|1, 2, 3, 4, 5, 6|]);
+
|> Wonka.takeWhile((. x) => x < 5)
+
|> Wonka.subscribe((. x) => print_int(x));
/* Prints 1 2 3 4 to the console. */
···
takeWhile(val => val < 5),
+
subscribe(val => console.log(val))
// Prints 1 2 3 4 to the console.
+
`throttle` emits values of a source, but after each value it will omit all values for
+
the given amount of milliseconds. It enforces a time of silence after each value it
+
receives and skips values while the silence is still ongoing.
+
This is very similar to `debounce` but instead of waiting for leading time before a
+
value it waits for trailing time after a value.
+
> _Note:_ This operator is only available in JavaScript environments, and will be excluded
+
> when compiling natively.
+
|> Wonka.throttle((. _x) => 50)
+
|> Wonka.subscribe((. x) => print_int(x));
+
/* Outputs 0 6 to the console. */
+
import { pipe, interval, throttle, take, subscribe } from 'wonka';
+
subscribe(val => console.log(val))
+
); // Outputs 0 6 to the console.