rafa
Version:
Rafa.js is a Javascript framework for building concurrent applications.
63 lines (46 loc) • 1.63 kB
Markdown
Return a function that handles a variable number of arguments, converts
those arguments to a single value, and then writes that value to a
channel, which is enumerated by this stream.
The returned function is meant to be used as an event handler.
<aside>
```js
// listener(kind: String, Channel): ((...args) => _)
var button = document.querySelector("button[value='2']")
var stream = Rafa.stream();
var result = 0;
stream.each(mouseEvent => result = mouseEvent.target.value);
button.addEventListener("click", stream.listener());
button.click();
// result: 2
```
</aside>
The listener can also generate different types of messages to the stream,
i.e. value, error, and done messages, by setting the kind parameter to either
"Value", "Error", or "Done".
<aside>
```js
var button = document.querySelector("button[value='oops']")
var stream = Rafa.stream();
var result = 0;
stream.error(mouseEvent => result = mouseEvent.target.value);
button.addEventListener("click", stream.listener("Error"));
button.click();
// result: oops
```
</aside>
If a channel is not provided, the listener method will create one automatically
and configure it with a buffer size of 0 (see Channels). You can also configure
the channel it will create by passing in the size and action parameters that
a Channel constructor expects.
<aside>
```js
var button = document.querySelector("button[value='2']")
var stream = Rafa.stream();
var result = 0;
stream.each(mouseEvent => result += mouseEvent.target.value);
button.addEventListener("click", stream.listener("Value", 1, "rotate"));
button.click();
button.click();
// result: 4
```
</aside>