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```reason
418Wonka.fromArray([|1, 2, 3, 4, 5, 6|])
419 |> Wonka.onPush((. x) => print_string({j|Push $x|j}))
420 |> Wonka.subscribe((. x) => print_int(x));
421/* Prints Push 1 1 Push 2 2 Push 3 3 Push 4 4 Push 5 5 Push 6 6 to the console. */
422```
423
424```typescript
425import { fromArray, pipe, onPush, subscribe } from 'wonka';
426
427pipe(
428 fromArray([1, 2, 3, 4, 5, 6]),
429 onPush(val => console.log(`Push ${val}`)),
430 subscribe(val => console.log(val))
431); // Prints Push 1 1 Push 2 2 Push 3 3 Push 4 4 Push 5 5 Push 6 6 to the console.
432```
433
434## onStart
435
436Run a callback when the `Start` signal is sent to the sink by the source.
437
438```reason
439let promise =
440 Js.Promise.make((~resolve, ~reject as _) =>
441 Js.Global.setTimeout(() => resolve(. "Resolve"), 1000) |> ignore
442 );
443
444Wonka.fromPromise(promise)
445 |> Wonka.onStart((.) => print_endline("onStart"))
446 |> Wonka.subscribe((. _val) => print_endline(_val));
447/* Logs onStart to the console, pauses for one second to allow the timeout to finish,
448then logs "Resolve" to the console. */
449```
450
451```typescript
452import { pipe, onStart, fromPromise, subscribe } from 'wonka';
453
454const promise = new Promise(resolve => {
455 setTimeout(() => {
456 resolve('Resolve');
457 }, 1000);
458});
459
460pipe(
461 fromPromise(promise),
462 onStart(() => console.log('onStart')),
463 subscribe(val => console.log(val))
464);
465
466// Logs onStart to the console, pauses for one second to allow the timeout to finish,
467// then logs "Resolve" to the console.
468```
469
470## sample
471
472`sample` emits the previously emitted value from an outer source every time
473an inner source (notifier) emits.
474
475In combination with `interval` it can be used to get values from a noisy source
476more regularly.
477
478```reason
479Wonka.interval(10)
480 |> Wonka.sample(Wonka.interval(100))
481 |> Wonka.take(2)
482 |> Wonka.subscribe((. x) => print_int(x));
483/* Prints 10 20 to the console. */
484```
485
486``` typescript
487import { pipe, interval, sample, take, subscribe } from 'wonka';
488
489pipe(
490 interval(10),
491 sample(interval(100)),
492 take(2),
493 subscribe(x => console.log(x))
494); // Prints 10 20 to the console.
495```
496
497## scan
498
499Accumulate emitted values of a source in a accumulator, similar to JavaScript `reduce`.
500
501```reason
502Wonka.fromArray([|1, 2, 3, 4, 5, 6|])
503 |> Wonka.scan((. acc, x) => acc + x, 0)
504 |> Wonka.subscribe((. x) => print_int(x));
505/* Prints 1 3 6 10 15 21 to the console. */
506```
507
508```typescript
509import { fromArray, pipe, scan, subscribe } from 'wonka';
510
511pipe(
512 fromArray([1, 2, 3, 4, 5, 6]),
513 scan((acc, val) => acc + val),
514 subscribe(val => console.log(val))
515);
516
517// Prints 1 3 6 10 15 21 to the console.
518```
519
520## share
521
522`share` ensures that all subscriptions to the underlying source are shared.
523
524By default Wonka's sources are lazy. They only instantiate themselves and begin
525emitting signals when they're being subscribed to, since they're also immutable.
526This means that when a source is used in multiple places, their underlying subscription
527is not shared. Instead, the entire chain of sources and operators will be instantiated
528separately every time.
529
530The `share` operator prevents this by creating an output source that will reuse a single
531subscription to the parent source, which will be unsubscribed from when no sinks are
532listening to it anymore.
533
534This is especially useful if you introduce side-effects to your sources,
535for instance with `onStart`.
536
537```reason
538let source = Wonka.never
539 |> Wonka.onStart((.) => print_endline("start"))
540 |> Wonka.share;
541
542/* Without share this would print "start" twice: */
543Wonka.publish(source);
544Wonka.publish(source);
545```
546
547```typescript
548import { pipe, never, onStart, share, publish } from 'wonka';
549
550const source = pipe(
551 never
552 onStart(() => console.log('start')),
553 share
554);
555
556// Without share this would print "start" twice:
557publish(source);
558publish(source);
559```
560
561## skip
562
563`skip` the specified number of emissions from the source.
564
565```reason
566Wonka.fromArray([|1, 2, 3, 4, 5, 6|])
567 |> Wonka.skip(2)
568 |> Wonka.subscribe((. x) => print_int(x));
569/* Prints 3 4 5 6 to the console, since the first two emissions from the source were skipped.
570```
571
572```typescript
573import { fromArray, pipe, skip, subscribe } from 'wonka';
574
575pipe(
576 fromArray([1, 2, 3, 4, 5, 6]),
577 skip(2),
578 subscribe(val => console.log(val))
579);
580```
581
582## skipUntil
583
584Skip emissions from an outer source until an inner source (notifier) emits.
585
586```reason
587let source = Wonka.interval(100);
588let notifier = Wonka.interval(500);
589
590source
591 |> Wonka.skipUntil(notifier)
592 |> Wonka.subscribe((. x) => print_int(x));
593
594/* Skips all values emitted by source (0, 1, 2, 3) until notifier emits at 500ms.
595Then logs 4 5 6 7 8 9 10... to the console every 500ms. */
596```
597
598```typescript
599import { interval, pipe, skipUntil, subscribe } from 'wonka';
600
601const source = interval(100);
602const notifier = interval(500);
603
604pipe(
605 source,
606 skipUntil(notifier),
607 subscribe(val => console.log(val))
608);
609
610// Skips all values emitted by source (0, 1, 2, 3) until notifier emits at 500ms.
611// Then logs 4 5 6 7 8 9 10... to the console every 500ms.
612```
613
614## skipWhile
615
616Skip values emitted from the source while they return `true` for the provided predicate function.
617
618```reason
619let source = Wonka.fromArray([|1, 2, 3, 4, 5, 6|]);
620
621source
622|> Wonka.skipWhile((. _val) => _val < 5)
623|> Wonka.subscribe((. _val) => print_int(_val));
624
625/* Prints 5 6 to the console, as 1 2 3 4 all return true for the predicate function. */
626```
627
628```typescript
629import { fromArray, pipe, skipWhile, subscribe } from 'wonka';
630
631pipe(
632 fromArray([1, 2, 3, 4, 5, 6]),
633 skipWhile(val => val < 5),
634 subscribe(val => console.log(val))
635);
636
637// Prints 5 6 to the console, as 1 2 3 4 all return true for the predicate function.
638```
639
640## switchMap
641
642`switchMap` allows you to map values of an outer source to an inner source.
643The inner source's values will be emitted on the returned output source. If
644a new inner source is returned, because the outer source emitted a new value
645before the previous inner source completed, the inner source is closed and unsubscribed
646from.
647
648This is similar to `concatMap` but instead of waiting for the last inner source to complete
649before emitting values from the next, `switchMap` just cancels the previous inner source.
650
651```reason
652Wonka.interval(50)
653 |> Wonka.switchMap((. _value) =>
654 Wonka.interval(40))
655 |> Wonka.take(3)
656 |> Wonka.subscribe((. x) => print_int(x));
657/* Prints 1 2 3 to the console. */
658/* The inner interval is cancelled after its first value every time */
659```
660
661```typescript
662import { pipe, interval, switchMap, take, subscribe } from 'wonka';
663
664pipe(
665 interval(50),
666 // The inner interval is cancelled after its first value every time
667 switchMap(value => interval(40)),
668 take(3),
669 subscribe(x => console.log(x))
670); // Prints 1 2 3 to the console
671```
672
673## switchAll
674
675`switchAll` will combined sources emitted on an outer source together, subscribing
676to only one source at a time, and cancelling the previous inner source, when it hasn't
677ended while the next inner source is created.
678
679It's very similar to `switchMap`, but instead accepts a source of sources.
680
681```reason
682Wonka.interval(50)
683 |> Wonka.map((. _value) =>
684 Wonka.interval(40))
685 |> Wonka.switchAll
686 |> Wonka.take(3)
687 |> Wonka.subscribe((. x) => print_int(x));
688/* Prints 1 2 3 to the console. */
689```
690
691```typescript
692import { pipe, interval, map, switchAll, take, subscribe } from 'wonka';
693
694pipe(
695 interval(50),
696 map(() => interval(40)),
697 switchAll,
698 take(3),
699 subscribe(x => console.log(x))
700); // Prints 1 2 3 to the console
701```
702
703These examples are practically identical to the `switchMap` examples, but note
704that `map` was used instead of using `switchMap` directly. This is because combining
705`map` with a subsequent `switchAll` is the same as using `switchMap`.
706
707## take
708
709`take` only a specified number of emissions from the source before completing. `take` is the opposite of `skip`.
710
711```reason
712Wonka.fromArray([|1, 2, 3, 4, 5, 6|])
713 |> Wonka.take(3)
714 |> Wonka.subscribe((. x) => print_int(x));
715/* Prints 1 2 3 to the console. */
716```
717
718```typescript
719import { fromArray, pipe, take, subscribe } from 'wonka';
720
721pipe(
722 fromArray([1, 2, 3, 4, 5, 6]),
723 take(3),
724 subscribe(val => console.log(val))
725);
726
727// Prints 1 2 3 to the console.
728```
729
730## takeLast
731
732`takeLast` will take only the last n emissions from the source.
733
734```reason
735Wonka.fromArray([|1, 2, 3, 4, 5, 6|]);
736 |> Wonka.takeLast(3)
737 |> Wonka.subscribe((. x) => print_int(x));
738/* Prints 4 5 6 to the console. */
739```
740
741```typescript
742import { fromArray, pipe, takeLast, subscribe } from 'wonka';
743
744pipe(
745 fromArray([1, 2, 3, 4, 5, 6]),
746 takeLast(3),
747 subscribe(val => console.log(val))
748);
749
750// Prints 4 5 6 to the console.
751```
752
753## takeUntil
754
755Take emissions from an outer source until an inner source (notifier) emits.
756
757```reason
758let source = Wonka.interval(100);
759let notifier = Wonka.interval(500);
760
761source
762 |> Wonka.takeUntil(notifier)
763 |> Wonka.subscribe((. x) => print_int(x));
764
765/* Pauses 100ms, prints 0, pauses 100ms, prints 1, pauses 100ms, prints 2, pauses 100ms,
766prints 3, pauses 100, then completes (notifier emits). */
767```
768
769```typescript
770import { interval, pipe, takeUntil, subscribe } from 'wonka';
771
772const source = interval(100);
773const notifier = interval(500);
774
775pipe(
776 source,
777 takeUntil(notifier),
778 subscribe(val => console.log(val))
779);
780
781// Pauses 100ms, prints 0, pauses 100ms, prints 1, pauses 100ms, prints 2, pauses 100ms,
782// prints 3, pauses 100, then completes (notifier emits).
783```
784
785## takeWhile
786
787Take 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.
788
789```reason
790let source = Wonka.fromArray([|1, 2, 3, 4, 5, 6|]);
791
792source
793 |> Wonka.takeWhile((. x) => x < 5)
794 |> Wonka.subscribe((. x) => print_int(x));
795
796/* Prints 1 2 3 4 to the console. */
797```
798
799```typescript
800import { pipe, fromArray, takeWhile, subscribe } from 'wonka';
801
802const source = fromArray([1, 2, 3, 4, 5, 6]);
803
804pipe(
805 source,
806 takeWhile(val => val < 5),
807 subscribe(val => console.log(val))
808);
809
810// Prints 1 2 3 4 to the console.
811```
812
813## throttle
814
815`throttle` emits values of a source, but after each value it will omit all values for
816the given amount of milliseconds. It enforces a time of silence after each value it
817receives and skips values while the silence is still ongoing.
818
819This is very similar to `debounce` but instead of waiting for leading time before a
820value it waits for trailing time after a value.
821
822> _Note:_ This operator is only available in JavaScript environments, and will be excluded
823> when compiling natively.
824
825```reason
826Wonka.interval(10)
827 |> Wonka.throttle((. _x) => 50)
828 |> Wonka.take(2)
829 |> Wonka.subscribe((. x) => print_int(x));
830/* Outputs 0 6 to the console. */
831```
832
833```typescript
834import { pipe, interval, throttle, take, subscribe } from 'wonka';
835
836pipe(
837 interval(10),
838 throttle(() => 50)
839 takew(2),
840 subscribe(val => console.log(val))
841); // Outputs 0 6 to the console.
842```