···
`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.
15
-
open Wonka_operators;
13
+
let sourceOne = Wonka.fromArray([|1, 2, 3|]);
14
+
let sourceTwo = Wonka.fromArray([|4, 5, 6|]);
18
-
let sourceOne = fromArray([|1, 2, 3|]);
19
-
let sourceTwo = fromArray([|4, 5, 6|]);
21
-
combine(sourceOne, sourceTwo)
22
-
|> subscribe((. (_valOne, _valTwo)) => print_int(_valOne + _valTwo));
16
+
Wonka.combine(sourceOne, sourceTwo)
17
+
|> Wonka.subscribe((. (_valOne, _valTwo)) => print_int(_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.
50
-
open Wonka_operators;
43
+
let sourceOne = Wonka.fromArray([|1, 2, 3, 4, 5, 6|]);
44
+
let sourceTwo = Wonka.fromArray([|6, 5, 4, 3, 2, 1|]);
53
-
let sourceOne = fromArray([|1, 2, 3, 4, 5, 6|]);
54
-
let sourceTwo = fromArray([|6, 5, 4, 3, 2, 1|]);
56
-
concat([|sourceOne, sourceTwo|]) |> subscribe((. _val) => print_int(_val));
46
+
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. */
···
// Prints 1 2 3 4 5 6 6 5 4 3 2 1 to the console.
69
+
`concatMap` allows you to map values of an outer source to an inner source. The sink will not dispatch the `Pull` signal until the previous value has been emitted. This is in contrast to `mergeMap`, which will dispatch the `Pull` signal for new values even if the previous value has not yet been emitted.
72
+
let source = Wonka.fromArray([|1, 2, 3, 4, 5, 6|]);
75
+
|> Wonka.concatMap((. _val) =>
76
+
Wonka.delay(_val * 1000, Wonka.fromValue(_val))
78
+
|> Wonka.subscribe((. _val) => print_int(_val));
80
+
/* After 1s, 1 will be emitted. After an additional 2s, 2 will be emitted.
81
+
After an additional 3s, 3 will be emitted. After an additional 4s, 4 will be emitted.
82
+
After an additional 5s, 5 will be emitted. After an additional 6s, 6 will be emitted. */
86
+
import { fromArray, pipe, concatMap, delay, fromValue, subscribe } from 'wonka';
88
+
const source = fromArray([1, 2, 3, 4, 5, 6]);
`filter` will remove values from a source by passing them through an iteratee that returns a `bool`.
84
-
open Wonka_operators;
87
-
let source = fromArray([|1, 2, 3, 4, 5, 6|]);
109
+
let source = Wonka.fromArray([|1, 2, 3, 4, 5, 6|]);
let isEven = (. n) => n mod 2 === 0;
90
-
source |> filter(isEven) |> subscribe((. _val) => print_int(_val));
112
+
source |> Wonka.filter(isEven) |> Wonka.subscribe((. _val) => print_int(_val));
/* Prints 246 to the console. */
···
`map` will transform values from a source by passing them through an iteratee that returns a new value.
118
-
open Wonka_sources;
119
-
open Wonka_operators;
122
-
let source = fromArray([|1, 2, 3, 4, 5, 6|]);
139
+
let source = Wonka.fromArray([|1, 2, 3, 4, 5, 6|]);
let square = (. n) => n * n;
125
-
source |> map(square) |> subscribe((. _val) => print_int(_val));
142
+
source |> Wonka.map(square) |> Wonka.subscribe((. _val) => print_int(_val));
/* Prints 1 4 9 16 25 36 to the console. */
···
`merge` two sources together into a single source.
153
-
open Wonka_sources;
154
-
open Wonka_operators;
169
+
let sourceA = Wonka.fromArray([|1, 2, 3|]);
170
+
let sourceB = Wonka.fromArray([|4, 5, 6|]);
157
-
let sourceA = fromArray([|1, 2, 3|]);
158
-
let sourceB = fromArray([|4, 5, 6|]);
160
-
merge([|sourceA, sourceB|]) |> subscribe((. _val) => print_int(_val));
172
+
Wonka.merge([|sourceA, sourceB|]) |> Wonka.subscribe((. _val) => print_int(_val));
/* Prints 1 2 3 4 5 6 to the console.
···
Run a callback when the `End` signal has been sent to the sink by the source, whether by way of the talkback passing the `End` signal or the source being exhausted of values.
187
-
open Wonka_operators;
Js.Promise.make((~resolve, ~reject as _) =>
Js.Global.setTimeout(() => resolve(. "ResolveOne"), 1000) |> ignore
···
Js.Global.setTimeout(() => resolve(. "ResolveTwo"), 2000) |> ignore
201
-
let sourceOne = fromPromise(promiseOne);
202
-
let sourceTwo = fromPromise(promiseTwo);
203
-
let source = concat([|sourceOne, sourceTwo|]);
208
+
let sourceOne = Wonka.fromPromise(promiseOne);
209
+
let sourceTwo = Wonka.fromPromise(promiseTwo);
210
+
let source = Wonka.concat([|sourceOne, sourceTwo|]);
206
-
|> onEnd((.) => print_endline("onEnd"))
207
-
|> subscribe((. _val) => print_endline(_val));
213
+
|> Wonka.onEnd((.) => print_endline("onEnd"))
214
+
|> Wonka.subscribe((. _val) => print_endline(_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.
248
-
open Wonka_sources;
249
-
open Wonka_operators;
255
+
let source = Wonka.fromArray([|1, 2, 3, 4, 5, 6|]);
252
-
let source = fromArray([|1, 2, 3, 4, 5, 6|]);
255
-
|> onPush((. _val) => print_string({j|Push $_val|j}))
256
-
|> subscribe((. _val) => print_int(_val));
258
+
|> Wonka.onPush((. _val) => print_string({j|Push $_val|j}))
259
+
|> 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. */
···
Run a callback when the `Start` signal is sent to the sink by the source.
285
-
open Wonka_operators;
Js.Promise.make((~resolve, ~reject as _) =>
Js.Global.setTimeout(() => resolve(. "Resolve"), 1000) |> ignore
294
-
let source = fromPromise(promise);
292
+
let source = Wonka.fromPromise(promise);
297
-
|> onStart((.) => print_endline("onStart"))
298
-
|> subscribe((. _val) => print_endline(_val));
295
+
|> Wonka.onStart((.) => print_endline("onStart"))
296
+
|> 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. */
···
Accumulate emitted values of a source in a accumulator, similar to JavaScript `reduce`.
335
-
open Wonka_sources;
336
-
open Wonka_operators;
339
-
let source = fromArray([|1, 2, 3, 4, 5, 6|]);
332
+
let source = Wonka.fromArray([|1, 2, 3, 4, 5, 6|]);
342
-
|> scan((. acc, x) => acc + x, 0)
343
-
|> subscribe((. _val) => print_int(_val));
335
+
|> Wonka.scan((. acc, x) => acc + x, 0)
336
+
|> Wonka.subscribe((. _val) => print_int(_val));
/* Prints 1 3 6 10 15 21 to the console. */
···
`skip` the specified number of emissions from the source.
369
-
open Wonka_sources;
370
-
open Wonka_operators;
373
-
let source = fromArray([|1, 2, 3, 4, 5, 6|]);
361
+
let source = Wonka.fromArray([|1, 2, 3, 4, 5, 6|]);
375
-
source |> skip(2) |> subscribe((. _val) => print_int(_val));
363
+
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.
···
Skip emissions from an outer source until an inner source (notifier) emits.
400
-
open Wonka_operators;
387
+
let source = Wonka.interval(100);
388
+
let notifier = Wonka.interval(500);
404
-
let source = interval(100);
405
-
let notifier = interval(500);
407
-
source |> skipUntil(notifier) |> subscribe((. _val) => print_int(_val));
390
+
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. */
···
Skip values emitted from the source while they return `true` for the provided predicate function.
437
-
open Wonka_operators;
438
-
open Wonka_sources;
441
-
let source = fromArray([|1, 2, 3, 4, 5, 6|]);
419
+
let source = Wonka.fromArray([|1, 2, 3, 4, 5, 6|]);
444
-
|> skipWhile((. _val) => _val < 5)
445
-
|> subscribe((. _val) => print_int(_val));
422
+
|> Wonka.skipWhile((. _val) => _val < 5)
423
+
|> Wonka.subscribe((. _val) => print_int(_val));
/* Prints 5 6 to the console, as 1 2 3 4 all return true for the predicate function. */
···
// Prints 5 6 to the console, as 1 2 3 4 all return true for the predicate function.
446
+
`take` only a specified number of emissions from the source before completing. `take` is the opposite of `skip`.
449
+
let source = Wonka.fromArray([|1, 2, 3, 4, 5, 6|]);
451
+
source |> Wonka.take(3) |> Wonka.subscribe((. _val) => print_int(_val));
453
+
/* Prints 1 2 3 to the console. */
457
+
import { fromArray, pipe, take, subscribe } from 'wonka';
459
+
const source = fromArray([1, 2, 3, 4, 5, 6]);
469
+
// Prints 1 2 3 to the console.
474
+
`takeLast` will take only the last n emissions from the source.
477
+
let source = Wonka.fromArray([|1, 2, 3, 4, 5, 6|]);
479
+
source |> Wonka.takeLast(3) |> Wonka.subscribe((. _val) => print_int(_val));
481
+
/* Prints 4 5 6 to the console. */
485
+
import { fromArray, pipe, takeLast, subscribe } from 'wonka';
487
+
const source = fromArray([1, 2, 3, 4, 5, 6]);
497
+
// Prints 4 5 6 to the console.
502
+
Take emissions from an outer source until an inner source (notifier) emits.
505
+
let source = Wonka.interval(100);
506
+
let notifier = Wonka.interval(500);
509
+
|> Wonka.takeUntil(notifier)
510
+
|> Wonka.subscribe((. _val) => print_int(_val));
512
+
/* Pauses 100ms, prints 0, pauses 100ms, prints 1, pauses 100ms, prints 2, pauses 100ms,
513
+
prints 3, pauses 100, then completes (notifier emits). */
517
+
import { interval, pipe, takeUntil, subscribe } from 'wonka';
519
+
const source = interval(100);
520
+
const notifier = interval(500);
524
+
takeUntil(notifier),
530
+
// Pauses 100ms, prints 0, pauses 100ms, prints 1, pauses 100ms, prints 2, pauses 100ms,
531
+
// prints 3, pauses 100, then completes (notifier emits).
536
+
Take emissions from the stream while they return `true` for the provided predicate function. If the first emission does not return `true`, no values will be `Push`ed to the sink.
539
+
let source = Wonka.fromArray([|1, 2, 3, 4, 5, 6|]);
542
+
|> Wonka.takeWhile((. _val) => _val < 5)
543
+
|> Wonka.subscribe((. _val) => print_int(_val));
545
+
/* Prints 1 2 3 4 to the console. */
549
+
import { pipe, fromArray, takeWhile, subscribe } from 'wonka';
551
+
const source = fromArray([1, 2, 3, 4, 5, 6]);
555
+
takeWhile(val => val < 5),
561
+
// Prints 1 2 3 4 to the console.