@gartz/iterable-stream
v1.0.1
Published
A tiny JavaScript lib that makes Stream and Observables easy to use as much as Promises are.
Downloads
3
Maintainers
Readme
IterableStream
The first for-await...of stream implementation, IterableStream lib returns a PromiseStream that is easy to subscribe, fork, pipe, and debug.
If you are having hard times with Stream API and Observables, going crazy with RxJS and BaconJS methods and complex debugging, this is the lib for your team. It's so simple that any engineer can write streams, test, and deploy great reactive applications.
Setup guide
npm install @gartz/iterable-stream
or
yarn install @gartz/iterable-stream
then include it:
import IterableStream from '@gartz/iterable-stream';
Create a stream
The function IterableStream
have two params:
callbackFunction(input, enqueue, close)
and input
.
Callback arguments:
input
is the ref to the 2nd argument from IterableStreamenqueue
is aFunction
that will add a value to the streamclose
is aFunction
that should be called when the stream is closed
Example:
const myIterableStream = IterableStream(async (input, enqueue, close) => {
enqueue('this');
enqueue('is');
enqueue('simple');
close()
});
This stream will output: "this"
, "is"
, "simple"
Reading a stream
Create an async function and use for async
.
async function printMyIterableStream(iterableStream) {
for async (const value of iterableStream) {
console.log(value);
}
}
printMyIterableStream(myIterableStream);
// Output:
// this
// is
// simple
API
A IterableStream will return a object that expose the stream, the object also have a public API that facilitate the criation of new streams with data.
Motivation
I want to create something that is simple but at same time powerful using the edge ES19 specifications.
This lib uses Promises and Generators to take full advantage of JavaScript, the code is tiny, therefore it's very powerful.
This project is inspired by:
- https://github.com/tc39/proposal-observable
- https://developer.mozilla.org/en-US/docs/Web/API/Streams_API
This project implements:
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for-await...of
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/asyncIterator
Both designs are very interesting, but somewhat hard to implement and debug.