Channel - Gozala/streamer GitHub Wiki
Channel is another core abstraction of streamer library. Channels implement same protocol as streams there for all the streamer functions can be applied to them as well.
Streams are lazy and are "pull" driven, which is useful for representing a sequences of items that has to be pulled from some source (for example read content from server), as items will be aggregated lazily only on demand. On the other hand there are sequences that are "push" driven, for example user generated UI events. Those are generated regardless of consumers demand, although consumer can either handle, queue up, or ignore these requests. Channels are designed to cover such scenarios by providing publish/subscribe model via lazy streams. If there is a consumers / subscribers reading from channel at the moment of item aggregation they all will get item as head and same channel as tail for rest of items. If there are no subscribers item will be just ignored.
Library exports Channel
function that may be used to create a channel where items can be queued up:
var channel = Channel()
enqueue(1, channel)
enqueue(2, channel)
enqueue(3, channel)
Since Channel
returns just a normal stream (well it's non-side effect free and it supports item pushing) any of the streamer function may be used with it:
print(map(function(x) { return x * x }, channel))
enqueue(2, channel)
enqueue(3, channel)
// => <stream 4 9
print(map(function(_) { return '2:' + _ }, channel))
enqueue(4, channel)
close(channel)
// => 16 />
// => <stream 2:4 />
Also as shown in example above channels may be closed via close function, so that items will no longer will be enqueued.
enqueue(5, channel)
print(channel)
// => <stream />