Mirror: 馃帺 A tiny but capable push & pull stream library for TypeScript and Flow
1--- 2title: Background 3order: 0 4--- 5 6In a lot of daily tasks in programming we come across patterns where 7we deal with lists of values. In JavaScript we'd reach to arrays to 8collect them, and luckily there are plenty of methods built-in 9to modify such an array, such as `map`, `filter` and `reduce`. 10 11Things become more complex when we're dealing with lists that 12are infinite. In such a case we may reach to iterables. We could 13expect an iterable that continuously outputs numbers, counting up 14infinitely, or rather until it reaches the maximum integer. 15 16When we're dealing with asynchronous lists of values things also 17become more complex. We're often confronted with event streams, 18where events or even regular values come in over time. 19 20In either case what we're dealing with are essentially [immutable, 21asynchronous iterables](https://medium.com/@andrestaltz/2-minute-introduction-to-rx-24c8ca793877). 22 23Wonka is a library to provide a primitive to solve these problems and 24is both an iterable programming library _and_ a reactive stream programming 25library. 26 27It can be compared to observables and iterables in one library, but is 28based on and essentially a ["callbag" library](https://staltz.com/why-we-need-callbags.html). 29 30## Sources, Operators, and Sinks 31 32When we're thinking of solving problems with streams, it's always 33a good idea to look at how we're solving problems with arrays. 34 35Since Wonka's streams are an entirely new primitive, Wonka has to provide 36all utilities that you as a developer may need to work with them. 37Specifically we have to make sure that it's easy to _create_, _transform_, 38and _consume_ these streams. 39 40If we compare these utilities to arrays, _creating_ an array is similar to 41creating a stream. So Wonka has utilities such as [`fromArray`](../api/sources.md#fromArray) to 42create a new source. 43 44A **source** is what we call a stream in Wonka. This is because it 45doesn't strictly follow the definition or specification of observables nor 46iterables. So we're calling them **sources** since they're just a **source** 47of values over time. 48 49Next we would like to _transform_ sources to make them useful. 50Like with arrays we may want to map, filter, and reduce them, 51so Wonka has **operators** like [`filter`](../api/operators.md#filter) and [`map`](../api/operators.md#map). 52But since Wonka is like a toolkit, it comes with a lot more utilities than 53just that. 54 55In general, **operators** will accept some arguments and a source 56and output a new, transformed source. 57 58Lastly, the sources we create wouldn't be of much use if we weren't 59able to _consume_ them. This is similar to using `forEach` on an 60array to iterate over its values. Wonka has a [`subscribe`](../api/sinks.md#subscribe) function which 61works similarly to how an observable's subscribe method may work. 62This is because Wonka's sources are entirely cancellable. 63 64To summarise, Wonka's streams are _sources_ of values, which 65can be transformed using _operators_, which create new _sources_. 66If we want to consume a _source_ we use a _sink_.