@ivlasov/rxsync

1.2.0 • Public • Published

rxsync

A lightweight library that operates on rxjs to execute a stream chains sequentially or in parallel with defined limit of simultaneous executions.

Motivation

Async/waterfall, async/parallel or async/eachLimit is very easy and useful libraries but for callbacks. This library uses RxJs and allows to make a stream chains in nice format

Installation

npm install @ivlasov/rxsync --save

Usage

Waterfall

const rxsync = require('@ivlasov/rxsync');

rxsync.waterfall([
    ()=>{
        return Observable.of(1);
    },
    (res)=>{
        return Observable.of(res+1);
    },
    (res)=>{
        return Observable.of(res+1);
    }
]).subscribe(res=>{
    console.log(res) //3
});

Parallel

Creates a stream with parallel execution of input streams. Returns an object with results of all streams executions, when the last one is finished

const rxsync = require('@ivlasov/rxsync');

rxsync.parallel({
    one: ()=>{
        return Observable.of(1);
    },
    two: ()=>{
        return Observable.of(2);
    },    
    three: ()=>{
        return Observable.of(3);
    }
}).subscribe(res=>{
    console.log(res);
    /*
        {
            one: 1,
            two: 2,
            three: 3
        }
    */
});

eachLimit

Creates a stream with parallel execution of array of streams and limitation of simultaneous executions

const rxsync = require('@ivlasov/rxsync');
const array=[1,2,3,4,5,6,7,8,9,10]

//rxsync.eachLimit(streams, limit);

rxsync.eachLimit(array.map(item=>{
    return Observable.of(item);
}), 5).subscribe(res=>{
    console.log(res); //[1,2,3,4,5,6,7,8,9,10]    
});

Dependencies (2)

Dev Dependencies (0)

    Package Sidebar

    Install

    npm i @ivlasov/rxsync

    Weekly Downloads

    24

    Version

    1.2.0

    License

    ISC

    Unpacked Size

    4.49 kB

    Total Files

    3

    Last publish

    Collaborators

    • ivlasov