paunkie

1.0.0 • Public • Published

Paunkie Build Status

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 = HttpRequestStream('http://www.example.com'); //Custom implementation of an EventStream
 
//Append the packet of data into an empty string
var response = await httpRequestStream.reduce((memory, data) => memory += data, '');
 
//Log out the response
console.log(response);
 

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
  part = (data) => {
    downloadedBytes += data.length;
    return Math.floor(downloadedBytes / 512);
  },
  
  //Create two writers for the two files
  writers = [
    new FileWrite('./512-bytes-file-part-1.txt'),
    new FileWrite('./512-bytes-file-part-2.txt')
  ],
  
  //Create an Http Request Stream
  httpRequestStream = HttpRequestStream(url)
    
    //Set the file number in a property with the data
    .map(data => {data, part: part(data)})
    
    //Split the out stream into two parts based on which file writer it needs to go to
    .split( x => x.part === 0, x => x.part === 1),
  
  //A list of total bytes written to disk [512, 512] 
  bytesWritten = await partialRequests.map((req, i) => {
    return req.reduce((bytesWritten, x) => bytesWritten + await writers[i].write(x.data), 0);
  });  
   

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.

Readme

Keywords

none

Package Sidebar

Install

npm i paunkie

Weekly Downloads

0

Version

1.0.0

License

ISC

Last publish

Collaborators

  • tusharmathur