queue - Gozala/streamer GitHub Wiki
Queue is another core abstraction of streamer library. Queues 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 incoming requests of some sort. Requests are generated regardless of consumers demand, although they can either handle, queue up, or ignore these requests. Queues are designed to cover such scenarios by queuing up requests in a lazy stream that consumer may handle on it's convenience (Ideally consumption should happen immediately as otherwise queue will grow taking up more and more memory).
Library exports Queue
function that may be used to create a stream where items
can be queued up:
var queue = Queue()
enqueue(1, queue)
enqueue(2, queue)
enqueue(3, queue)
Since Queue
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 }, queue)) // => <stream 1 4 9
Note: Make sure to don't hold references to the transformed queues since they are (most likely long) lazy streams (lazily cache their items)
It's also possible to close queues so that no more items will be enqueued in them:
enqueue(4, queue)
close(queue)
// => 16 />
enqueue(5, queue)
print(queue) // => <stream />
Also be aware that queues behave slightly different than regular streams. Each item of the queue can be consumed only exactly by one consumer and it works on first in first out bases:
var queue = Queue()
enqueue(1, queue)
enqueue(2, queue)
enqueue(3, queue)
close(queue)
print(map(function(_) { return '1:' + _ }, queue))
print(map(function(_) { return '2:' + _ }, queue))
// <stream 1:1 1:2 1:3 />
// <stream />