Paunkie
A lodash inspired functional reactive programming library. Async programming in Javascript isn't a daunting task anymore when you use features like Arrow functions, Promises, es6 generators or es7 Async. EventStreams on the other hand add a bit more sugar to the whole process.
Installation
npm i paunkie --save
Examples
Http request stream
Http requests are actually streams but more often than not you end up first filling a buffer with the packets of data and once all the data is transferred and the buffer is full, the callback would be called with the err as first param and concated response as the second. This is how you could achieve the same via paunkie.
var httpRequestStream = ; //Custom implementation of an EventStream //Append the packet of data into an empty stringvar response = await httpRequestStream; //Log out the responseconsole;
Download a file and save in parts
Here the idea is to download a file of size 1024 bytes over http and be able to save it in two parts on the disk.
//File url to be downloaded var url = 'http://www.example.com/1024-bytes-file.txt' //Bytes of data downloaded till now downloadedBytes = 0 //a function that tells which file should the data go to { downloadedBytes += datalength; return Math; } //Create two writers for the two files writers = './512-bytes-file-part-1.txt' './512-bytes-file-part-2.txt' //Create an Http Request Stream httpRequestStream = //Set the file number in a property with the data //Split the out stream into two parts based on which file writer it needs to go to //A list of total bytes written to disk [512, 512] bytesWritten = await partialRequests;
API
onClose(cb: Function)
Attaches listeners for the close
event. The Listner is called with the writeCount
ie. number of times the stream was written.
onValue(:Function)
Attaches listeners for the value
event. The listner is called with the value.
write(:Object)
Write any value to a stream, all the listeners would be triggered with that particular value automatically.
close()
Closes the stream for further writes and also triggers the close
event.
isClosed(): Boolean
Returns true
or false
based on the status of the stream.
filter(predicate: Function): EventStream
Creates a new stream, with only the events that test for truthy via the predicate.
map(iteratee: Function): EventStream
Maps the current stream to a new stream via the iteratee
.
reduce(iteratee: Function, initialValue: Object): Promise
Reduces a stream to a value.
merge(...eventStream: EventStream): EventStream
Merges multiple event streams into a single one.
split(...filters: Function): [EventStream]
Splits the current stream into multiple streams based on the filters.
concat(...eventStream: EventStream): EventStream
Like merge
but maintains the order of the inputted stream.