1---
2title: Operators
3order: 1
4---
5
6Operators in Wonka allow you to transform values from a source before they are sent to a sink. Wonka has the following operators.
7
8## buffer
9
10Buffers emissions from an outer source and emits a buffer array of items every time an
11inner source (notifier) emits.
12
13This operator can be used to group values into a arrays on a source. The emitted values will
14be sent when a notifier fires and will be arrays of all items before the notification event.
15
16In combination with `interval` this can be used to group values in chunks regularly.
17
18```reason
19Wonka.interval(50)
20 |> Wonka.buffer(Wonka.interval(100))
21 |> Wonka.take(2)
22 |> Wonka.subscribe((. buffer) => {
23 Js.Array.forEach(num => print_int(num), buffer);
24 print_endline(";");
25 });
26/* Prints 1 2; 2 3 to the console. */
27```
28
29``` typescript
30import { pipe, interval, buffer, take, subscribe } from 'wonka';
31
32pipe(
33 interval(50),
34 buffer(interval(100)),
35 take(2),
36 subscribe(buffer => {
37 buffer.forEach(x => console.log(x));
38 console.log(';');
39 })
40); // Prints 1 2; 2 3 to the console.
41```
42
43## combine
44
45`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.
46
47```reason
48let sourceOne = Wonka.fromArray([|1, 2, 3|]);
49let sourceTwo = Wonka.fromArray([|4, 5, 6|]);
50
51Wonka.combine(sourceOne)
52 |> sourceTwo
53 |> Wonka.subscribe((. (a, b)) => print_int(a + b));
54
55/* Prints 56789 (1+4, 2+4, 3+4, 3+5, 3+6) to the console. */
56```
57
58```typescript
59import { fromArray, pipe, combine, subscribe } from 'wonka';
60
61const sourceOne = fromArray([1, 2, 3]);
62const sourceTwo = fromArray([4, 5, 6]);
63
64pipe(
65 combine(sourceOne, sourceTwo),
66 subscribe(([valOne, valTwo]) => {
67 console.log(valOne + valTwo);
68 })
69); // Prints 56789 (1+4, 2+4, 3+4, 3+5, 3+6) to the console.
70```
71
72## concat
73
74`concat` will combine two sources together, subscribing to the next source after the previous source completes.
75
76```reason
77let sourceOne = Wonka.fromArray([|1, 2, 3|]);
78let sourceTwo = Wonka.fromArray([|6, 5, 4|]);
79
80Wonka.concat([|sourceOne, sourceTwo|])
81 |> Wonka.subscribe((. x) => print_int(x));
82/* Prints 1 2 3 6 5 4 to the console. */
83```
84
85```typescript
86import { fromArray, pipe, concat, subscribe } from 'wonka';
87
88const sourceOne = fromArray([1, 2, 3]);
89const sourceTwo = fromArray([6, 5, 4]);
90
91pipe(
92 concat([sourceOne, sourceTwo]),
93 subscribe(val => console.log(val))
94); // Prints 1 2 3 6 5 4 to the console.
95```
96
97## concatAll
98
99`concatAll` will combine all sources emitted on an outer source together, subscribing to the
100next source after the previous source completes.
101
102It's very similar to `concat`, but instead accepts a source of sources as an input.
103
104```reason
105let sourceOne = Wonka.fromArray([|1, 2, 3|]);
106let sourceTwo = Wonka.fromArray([|6, 5, 4|]);
107
108Wonka.fromList([sourceOne, sourceTwo])
109 |> Wonka.concatAll
110 |> Wonka.subscribe((. x) => print_int(x));
111/* Prints 1 2 3 6 5 4 to the console. */
112```
113
114```typescript
115import { pipe, fromArray, concatAll, subscribe } from 'wonka';
116
117const sourceOne = fromArray([1, 2, 3]);
118const sourceTwo = fromArray([6, 5, 4]);
119
120pipe(
121 fromArray([sourceOne, sourceTwo]),
122 concatAll,
123 subscribe(val => console.log(val))
124); // Prints 1 2 3 6 5 4 to the console.
125```
126
127## concatMap
128
129`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.
130
131```reason
132let source = Wonka.fromArray([|1, 2, 3, 4, 5, 6|]);
133
134source
135|> Wonka.concatMap((. _val) =>
136 Wonka.delay(_val * 1000, Wonka.fromValue(_val))
137 )
138|> Wonka.subscribe((. _val) => print_int(_val));
139
140/* After 1s, 1 will be emitted. After an additional 2s, 2 will be emitted.
141After an additional 3s, 3 will be emitted. After an additional 4s, 4 will be emitted.
142After an additional 5s, 5 will be emitted. After an additional 6s, 6 will be emitted. */
143```
144
145```typescript
146import { fromArray, pipe, concatMap, delay, fromValue, subscribe } from 'wonka';
147
148const source = fromArray([1, 2, 3, 4, 5, 6]);
149
150pipe(
151 source,
152 concatMap(val => {
153 return pipe(
154 fromValue(val),
155 delay(val * 1000)
156 );
157 }),
158 subscribe(val => console.log(val))
159);
160```
161
162
163## delay
164
165`delay` delays all emitted values of a source by the given amount of milliseconds.
166
167> _Note:_ This operator is only available in JavaScript environments, and will be excluded
168> when compiling natively.
169
170```reason
171Wonka.fromList([1, 2])
172 |> Wonka.delay(10)
173 |> Wonka.subscribe((. x) => print_int(x));
174/* waits 10ms then prints 1, waits 10ms then prints 2, waits 10ms then ends */
175```
176
177```typescript
178import { pipe, fromArray, delay, subscribe } from 'wonka';
179
180pipe(
181 fromArray([1, 2]),
182 delay(10)
183 subscribe(val => console.log(val))
184);
185// waits 10ms then prints 1, waits 10ms then prints 2, waits 10ms then ends
186```
187
188## debounce
189
190`debounce` doesn't emit values of a source until no values have been emitted after
191a given amount of milliseconds. Once this threshold of silence has been reached, the
192last value that has been received will be emitted.
193
194> _Note:_ This operator is only available in JavaScript environments, and will be excluded
195> when compiling natively.
196
197```reason
198let sourceA = Wonka.interval(10)
199 |> Wonka.take(5);
200let sourceB = Wonka.fromValue(1);
201
202Wonka.concat([|sourceA, sourceB|])
203 |> Wonka.debounce((. _x) => 20)
204 |> Wonka.subscribe((. x) => print_int(x));
205/* The five values from sourceA will be omitted */
206/* After these values and after 20ms `1` will be logged */
207```
208
209```typescript
210import { pipe, interval, take, fromValue, concat, debounce, subscribe } from 'wonka';
211
212const sourceA = pipe(interval(10), take(5));
213const sourceB = fromValue(1);
214
215pipe(
216 concat([sourceA, sourceB])
217 debounce(() => 20),
218 subscribe(val => console.log(val))
219);
220
221// The five values from sourceA will be omitted
222// After these values and after 20ms `1` will be logged
223```
224
225## filter
226
227`filter` will remove values from a source by passing them through an iteratee that returns a `bool`.
228
229```reason
230let isEven = (. n) => n mod 2 === 0;
231
232Wonka.fromArray([|1, 2, 3, 4, 5, 6|])
233 |> Wonka.filter(isEven)
234 |> Wonka.subscribe((. x) => print_int(x));
235/* Prints 246 to the console. */
236```
237
238```typescript
239import { fromArray, filter, subscribe } from 'wonka';
240
241const isEven = n => n % 2 === 0;
242
243pipe(
244 fromArray([1, 2, 3, 4, 5, 6]),
245 filter(isEven),
246 subscribe(val => console.log(val))
247);
248
249// Prints 246 to the console.
250```
251
252## map
253
254`map` will transform values from a source by passing them through an iteratee that returns a new value.
255
256```reason
257let square = (. n) => n * n;
258
259Wonka.fromArray([|1, 2, 3, 4, 5, 6|])
260 |> Wonka.map(square)
261 |> Wonka.subscribe((. x) => print_int(x));
262/* Prints 1 4 9 16 25 36 to the console. */
263```
264
265```typescript
266import { fromArray, pipe, map, subscribe } from 'wonka';
267
268const square = n => n * n;
269
270pipe(
271 fromArray([1, 2, 3, 4, 5, 6]),
272 map(square),
273 subscribe(val => console.log(val))
274);
275
276// Prints 1 4 9 16 25 36 to the console.
277```
278
279## merge
280
281`merge` merges an array of sources together into a single source. It subscribes
282to all sources that it's passed and emits all their values on the output source.
283
284```reason
285let sourceA = Wonka.fromArray([|1, 2, 3|]);
286let sourceB = Wonka.fromArray([|4, 5, 6|]);
287
288Wonka.merge([|sourceA, sourceB|])
289 |> Wonka.subscribe((. x) => print_int(x));
290/* Prints 1 2 3 4 5 6 to the console. */
291```
292
293```typescript
294import { fromArray, pipe, merge, subscribe } from 'wonka';
295
296const sourceOne = fromArray([1, 2, 3]);
297const sourceTwo = fromArray([4, 5, 6]);
298
299pipe(
300 merge(sourceOne, sourceTwo),
301 subscribe((val) => console.log(val))
302); // Prints 1 2 3 4 5 6 to the console.
303```
304
305## mergeAll
306
307`mergeAll` will merge all sources emitted on an outer source into a single one.
308It's very similar to `merge`, but instead accepts a source of sources as an input.
309
310> _Note:_ This operator is also exported as `flatten` which is just an alias for `mergeAll`
311
312```reason
313let sourceA = Wonka.fromArray([|1, 2, 3|]);
314let sourceB = Wonka.fromArray([|4, 5, 6|]);
315
316Wonka.fromList([sourceA, sourceB])
317 |> Wonka.mergeAll
318 |> Wonka.subscribe((. x) => print_int(x));
319/* Prints 1 2 3 4 5 6 to the console. */
320```
321
322```typescript
323import { pipe, fromArray, mergeAll, subscribe } from 'wonka';
324
325const sourceOne = fromArray([1, 2, 3]);
326const sourceTwo = fromArray([4, 5, 6]);
327
328pipe(
329 fromArray([sourceOne, sourceTwo]),
330 mergeAll,
331 subscribe(val => console.log(val))
332); // Prints 1 2 3 4 5 6 to the console.
333```
334
335## mergeMap
336
337`mergeMap` allows you to map values of an outer source to an inner source.
338This allows you to create nested sources for each emitted value, which will
339all be merged into a single source, like with `mergeAll`.
340
341Unlike `concatMap` all inner sources will be subscribed to at the same time
342and all their values will be emitted on the output source as they come in.
343
344```reason
345Wonka.fromList([1, 2])
346 |> Wonka.mergeMap((. value) =>
347 Wonka.fromList([value - 1, value]))
348 |> Wonka.subscribe((. x) => print_int(x));
349/* Prints 0 1 1 2 to the console. */
350```
351
352```typescript
353import { pipe, fromArray, mergeMap, subscribe } from 'wonka';
354
355pipe(
356 fromArray([1, 2]),
357 mergeMap(x => fromArray([x - 1, x])),
358 subscribe(val => console.log(val))
359); // Prints 0 1 1 2 to the console.
360```
361
362## onEnd
363
364Run 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.
365
366```reason
367let promiseOne =
368 Js.Promise.make((~resolve, ~reject as _) =>
369 Js.Global.setTimeout(() => resolve(. "ResolveOne"), 1000) |> ignore
370 );
371
372let promiseTwo =
373 Js.Promise.make((~resolve, ~reject as _) =>
374 Js.Global.setTimeout(() => resolve(. "ResolveTwo"), 2000) |> ignore
375 );
376
377let sourceOne = Wonka.fromPromise(promiseOne);
378let sourceTwo = Wonka.fromPromise(promiseTwo);
379
380Wonka.concat([|sourceOne, sourceTwo|])
381 |> Wonka.onEnd((.) => print_endline("onEnd"))
382 |> Wonka.subscribe((. x) => print_endline(x));
383
384/* Logs ResolveOne after one second, then ResolveTwo after an additional second, then onEnd immediately. */
385```
386
387```typescript
388import { fromPromise, pipe, concat, onEnd, subscribe } from 'wonka';
389
390const promiseOne = new Promise(resolve => {
391 setTimeout(() => {
392 resolve('ResolveOne');
393 }, 1000);
394});
395const promiseTwo = new Promise(resolve => {
396 setTimeout(() => {
397 resolve('ResolveTwo');
398 }, 2000);
399});
400
401const sourceOne = fromPromise(promiseOne);
402const sourceTwo = fromPromise(promiseTwo);
403
404pipe(
405 concat([sourceOne, sourceTwo]),
406 onEnd(() => console.log('onEnd')),
407 subscribe(val => console.log(val))
408);
409
410// Logs ResolveOne after one second, then ResolveTwo after an additional second, then onEnd immediately.
411```
412
413## onPush
414
415Run a callback on each `Push` signal sent to the sink by the source.
416
417> _Note:_ This operator is also exported as `tap` which is just an alias for `onPush`
418
419```reason
420Wonka.fromArray([|1, 2, 3, 4, 5, 6|])
421 |> Wonka.onPush((. x) => print_string({j|Push $x|j}))
422 |> Wonka.subscribe((. x) => print_int(x));
423/* Prints Push 1 1 Push 2 2 Push 3 3 Push 4 4 Push 5 5 Push 6 6 to the console. */
424```
425
426```typescript
427import { fromArray, pipe, onPush, subscribe } from 'wonka';
428
429pipe(
430 fromArray([1, 2, 3, 4, 5, 6]),
431 onPush(val => console.log(`Push ${val}`)),
432 subscribe(val => console.log(val))
433); // Prints Push 1 1 Push 2 2 Push 3 3 Push 4 4 Push 5 5 Push 6 6 to the console.
434```
435
436## onStart
437
438Run a callback when the `Start` signal is sent to the sink by the source.
439
440```reason
441let promise =
442 Js.Promise.make((~resolve, ~reject as _) =>
443 Js.Global.setTimeout(() => resolve(. "Resolve"), 1000) |> ignore
444 );
445
446Wonka.fromPromise(promise)
447 |> Wonka.onStart((.) => print_endline("onStart"))
448 |> Wonka.subscribe((. _val) => print_endline(_val));
449/* Logs onStart to the console, pauses for one second to allow the timeout to finish,
450then logs "Resolve" to the console. */
451```
452
453```typescript
454import { pipe, onStart, fromPromise, subscribe } from 'wonka';
455
456const promise = new Promise(resolve => {
457 setTimeout(() => {
458 resolve('Resolve');
459 }, 1000);
460});
461
462pipe(
463 fromPromise(promise),
464 onStart(() => console.log('onStart')),
465 subscribe(val => console.log(val))
466);
467
468// Logs onStart to the console, pauses for one second to allow the timeout to finish,
469// then logs "Resolve" to the console.
470```
471
472## sample
473
474`sample` emits the previously emitted value from an outer source every time
475an inner source (notifier) emits.
476
477In combination with `interval` it can be used to get values from a noisy source
478more regularly.
479
480```reason
481Wonka.interval(10)
482 |> Wonka.sample(Wonka.interval(100))
483 |> Wonka.take(2)
484 |> Wonka.subscribe((. x) => print_int(x));
485/* Prints 10 20 to the console. */
486```
487
488``` typescript
489import { pipe, interval, sample, take, subscribe } from 'wonka';
490
491pipe(
492 interval(10),
493 sample(interval(100)),
494 take(2),
495 subscribe(x => console.log(x))
496); // Prints 10 20 to the console.
497```
498
499## scan
500
501Accumulate emitted values of a source in a accumulator, similar to JavaScript `reduce`.
502
503```reason
504Wonka.fromArray([|1, 2, 3, 4, 5, 6|])
505 |> Wonka.scan((. acc, x) => acc + x, 0)
506 |> Wonka.subscribe((. x) => print_int(x));
507/* Prints 1 3 6 10 15 21 to the console. */
508```
509
510```typescript
511import { fromArray, pipe, scan, subscribe } from 'wonka';
512
513pipe(
514 fromArray([1, 2, 3, 4, 5, 6]),
515 scan((acc, val) => acc + val, 0),
516 subscribe(val => console.log(val))
517);
518
519// Prints 1 3 6 10 15 21 to the console.
520```
521
522## share
523
524`share` ensures that all subscriptions to the underlying source are shared.
525
526By default Wonka's sources are lazy. They only instantiate themselves and begin
527emitting signals when they're being subscribed to, since they're also immutable.
528This means that when a source is used in multiple places, their underlying subscription
529is not shared. Instead, the entire chain of sources and operators will be instantiated
530separately every time.
531
532The `share` operator prevents this by creating an output source that will reuse a single
533subscription to the parent source, which will be unsubscribed from when no sinks are
534listening to it anymore.
535
536This is especially useful if you introduce side-effects to your sources,
537for instance with `onStart`.
538
539```reason
540let source = Wonka.never
541 |> Wonka.onStart((.) => print_endline("start"))
542 |> Wonka.share;
543
544/* Without share this would print "start" twice: */
545Wonka.publish(source);
546Wonka.publish(source);
547```
548
549```typescript
550import { pipe, never, onStart, share, publish } from 'wonka';
551
552const source = pipe(
553 never
554 onStart(() => console.log('start')),
555 share
556);
557
558// Without share this would print "start" twice:
559publish(source);
560publish(source);
561```
562
563## skip
564
565`skip` the specified number of emissions from the source.
566
567```reason
568Wonka.fromArray([|1, 2, 3, 4, 5, 6|])
569 |> Wonka.skip(2)
570 |> Wonka.subscribe((. x) => print_int(x));
571/* Prints 3 4 5 6 to the console, since the first two emissions from the source were skipped.
572```
573
574```typescript
575import { fromArray, pipe, skip, subscribe } from 'wonka';
576
577pipe(
578 fromArray([1, 2, 3, 4, 5, 6]),
579 skip(2),
580 subscribe(val => console.log(val))
581);
582```
583
584## skipUntil
585
586Skip emissions from an outer source until an inner source (notifier) emits.
587
588```reason
589let source = Wonka.interval(100);
590let notifier = Wonka.interval(500);
591
592source
593 |> Wonka.skipUntil(notifier)
594 |> Wonka.subscribe((. x) => print_int(x));
595
596/* Skips all values emitted by source (0, 1, 2, 3) until notifier emits at 500ms.
597Then logs 4 5 6 7 8 9 10... to the console every 500ms. */
598```
599
600```typescript
601import { interval, pipe, skipUntil, subscribe } from 'wonka';
602
603const source = interval(100);
604const notifier = interval(500);
605
606pipe(
607 source,
608 skipUntil(notifier),
609 subscribe(val => console.log(val))
610);
611
612// Skips all values emitted by source (0, 1, 2, 3) until notifier emits at 500ms.
613// Then logs 4 5 6 7 8 9 10... to the console every 500ms.
614```
615
616## skipWhile
617
618Skip values emitted from the source while they return `true` for the provided predicate function.
619
620```reason
621let source = Wonka.fromArray([|1, 2, 3, 4, 5, 6|]);
622
623source
624|> Wonka.skipWhile((. _val) => _val < 5)
625|> Wonka.subscribe((. _val) => print_int(_val));
626
627/* Prints 5 6 to the console, as 1 2 3 4 all return true for the predicate function. */
628```
629
630```typescript
631import { fromArray, pipe, skipWhile, subscribe } from 'wonka';
632
633pipe(
634 fromArray([1, 2, 3, 4, 5, 6]),
635 skipWhile(val => val < 5),
636 subscribe(val => console.log(val))
637);
638
639// Prints 5 6 to the console, as 1 2 3 4 all return true for the predicate function.
640```
641
642## switchMap
643
644`switchMap` allows you to map values of an outer source to an inner source.
645The inner source's values will be emitted on the returned output source. If
646a new inner source is returned, because the outer source emitted a new value
647before the previous inner source completed, the inner source is closed and unsubscribed
648from.
649
650This is similar to `concatMap` but instead of waiting for the last inner source to complete
651before emitting values from the next, `switchMap` just cancels the previous inner source.
652
653```reason
654Wonka.interval(50)
655 |> Wonka.switchMap((. _value) =>
656 Wonka.interval(40))
657 |> Wonka.take(3)
658 |> Wonka.subscribe((. x) => print_int(x));
659/* Prints 1 2 3 to the console. */
660/* The inner interval is cancelled after its first value every time */
661```
662
663```typescript
664import { pipe, interval, switchMap, take, subscribe } from 'wonka';
665
666pipe(
667 interval(50),
668 // The inner interval is cancelled after its first value every time
669 switchMap(value => interval(40)),
670 take(3),
671 subscribe(x => console.log(x))
672); // Prints 1 2 3 to the console
673```
674
675## switchAll
676
677`switchAll` will combined sources emitted on an outer source together, subscribing
678to only one source at a time, and cancelling the previous inner source, when it hasn't
679ended while the next inner source is created.
680
681It's very similar to `switchMap`, but instead accepts a source of sources.
682
683```reason
684Wonka.interval(50)
685 |> Wonka.map((. _value) =>
686 Wonka.interval(40))
687 |> Wonka.switchAll
688 |> Wonka.take(3)
689 |> Wonka.subscribe((. x) => print_int(x));
690/* Prints 1 2 3 to the console. */
691```
692
693```typescript
694import { pipe, interval, map, switchAll, take, subscribe } from 'wonka';
695
696pipe(
697 interval(50),
698 map(() => interval(40)),
699 switchAll,
700 take(3),
701 subscribe(x => console.log(x))
702); // Prints 1 2 3 to the console
703```
704
705These examples are practically identical to the `switchMap` examples, but note
706that `map` was used instead of using `switchMap` directly. This is because combining
707`map` with a subsequent `switchAll` is the same as using `switchMap`.
708
709## take
710
711`take` only a specified number of emissions from the source before completing. `take` is the opposite of `skip`.
712
713```reason
714Wonka.fromArray([|1, 2, 3, 4, 5, 6|])
715 |> Wonka.take(3)
716 |> Wonka.subscribe((. x) => print_int(x));
717/* Prints 1 2 3 to the console. */
718```
719
720```typescript
721import { fromArray, pipe, take, subscribe } from 'wonka';
722
723pipe(
724 fromArray([1, 2, 3, 4, 5, 6]),
725 take(3),
726 subscribe(val => console.log(val))
727);
728
729// Prints 1 2 3 to the console.
730```
731
732## takeLast
733
734`takeLast` will take only the last n emissions from the source.
735
736```reason
737Wonka.fromArray([|1, 2, 3, 4, 5, 6|]);
738 |> Wonka.takeLast(3)
739 |> Wonka.subscribe((. x) => print_int(x));
740/* Prints 4 5 6 to the console. */
741```
742
743```typescript
744import { fromArray, pipe, takeLast, subscribe } from 'wonka';
745
746pipe(
747 fromArray([1, 2, 3, 4, 5, 6]),
748 takeLast(3),
749 subscribe(val => console.log(val))
750);
751
752// Prints 4 5 6 to the console.
753```
754
755## takeUntil
756
757Take emissions from an outer source until an inner source (notifier) emits.
758
759```reason
760let source = Wonka.interval(100);
761let notifier = Wonka.interval(500);
762
763source
764 |> Wonka.takeUntil(notifier)
765 |> Wonka.subscribe((. x) => print_int(x));
766
767/* Pauses 100ms, prints 0, pauses 100ms, prints 1, pauses 100ms, prints 2, pauses 100ms,
768prints 3, pauses 100, then completes (notifier emits). */
769```
770
771```typescript
772import { interval, pipe, takeUntil, subscribe } from 'wonka';
773
774const source = interval(100);
775const notifier = interval(500);
776
777pipe(
778 source,
779 takeUntil(notifier),
780 subscribe(val => console.log(val))
781);
782
783// Pauses 100ms, prints 0, pauses 100ms, prints 1, pauses 100ms, prints 2, pauses 100ms,
784// prints 3, pauses 100, then completes (notifier emits).
785```
786
787## takeWhile
788
789Take 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.
790
791```reason
792let source = Wonka.fromArray([|1, 2, 3, 4, 5, 6|]);
793
794source
795 |> Wonka.takeWhile((. x) => x < 5)
796 |> Wonka.subscribe((. x) => print_int(x));
797
798/* Prints 1 2 3 4 to the console. */
799```
800
801```typescript
802import { pipe, fromArray, takeWhile, subscribe } from 'wonka';
803
804const source = fromArray([1, 2, 3, 4, 5, 6]);
805
806pipe(
807 source,
808 takeWhile(val => val < 5),
809 subscribe(val => console.log(val))
810);
811
812// Prints 1 2 3 4 to the console.
813```
814
815## throttle
816
817`throttle` emits values of a source, but after each value it will omit all values for
818the given amount of milliseconds. It enforces a time of silence after each value it
819receives and skips values while the silence is still ongoing.
820
821This is very similar to `debounce` but instead of waiting for leading time before a
822value it waits for trailing time after a value.
823
824> _Note:_ This operator is only available in JavaScript environments, and will be excluded
825> when compiling natively.
826
827```reason
828Wonka.interval(10)
829 |> Wonka.throttle((. _x) => 50)
830 |> Wonka.take(2)
831 |> Wonka.subscribe((. x) => print_int(x));
832/* Outputs 0 6 to the console. */
833```
834
835```typescript
836import { pipe, interval, throttle, take, subscribe } from 'wonka';
837
838pipe(
839 interval(10),
840 throttle(() => 50)
841 takew(2),
842 subscribe(val => console.log(val))
843); // Outputs 0 6 to the console.
844```