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