strom/README.md

152 lines
3.7 KiB
Markdown
Raw Normal View History

2018-11-29 04:46:12 +00:00
# Mhysa
2017-03-19 08:28:40 +00:00
**Streams and event emitter utils for Node.js**
2017-03-19 08:28:40 +00:00
2018-11-26 01:00:37 +00:00
## Installation
```sh
2018-11-26 06:38:19 +00:00
yarn add mhysa
2018-11-26 01:00:37 +00:00
```
## Basic Usage
2018-11-29 04:46:12 +00:00
The following snippet demonstrates most of Mhysa's current features without much explanation. More
will come!
2018-11-26 06:18:01 +00:00
2018-11-26 01:00:37 +00:00
```js
2018-11-29 04:59:24 +00:00
const {
utils: { sleep, delay, once },
...Mhysa
} = require("mhysa");
2018-11-26 01:00:37 +00:00
async function main() {
2018-11-29 04:59:24 +00:00
const collector = Mhysa.concat(
Mhysa.fromArray(["a\n", "b\n", "c\n"]),
Mhysa.fromArray(["d", "e"]).pipe(Mhysa.join("-")),
)
.pipe(Mhysa.split("\n"))
.pipe(
2018-11-29 04:59:24 +00:00
Mhysa.flatMap(async s => {
await sleep(100);
return delay([s, s.toUpperCase()], 100);
}),
)
2018-11-29 04:59:24 +00:00
.pipe(Mhysa.collect({ objectMode: true }));
2018-11-26 01:00:37 +00:00
const collected = await once(collector, "data");
2018-11-29 04:59:24 +00:00
console.log(collected); // [ 'a', 'A', 'b', 'B', 'c', 'C', 'd-e', 'D-E' ] (after 6 * 100 ms)
2018-11-26 01:00:37 +00:00
}
main();
```
## API
```ts
/**
* Convert an array into a readable stream of its elements
* @param array The array of elements to stream
*/
fromArray(array: any[]): NodeJS.ReadableStream;
2018-11-26 01:00:37 +00:00
/**
* Return a ReadWrite stream that maps streamed chunks
* @param mapper The mapper function, mapping each (chunk, encoding) to a new chunk (or a promise of such)
* @param options
* @param options.readableObjectMode Whether this stream should behave as a readable stream of objects
* @param options.writableObjectMode Whether this stream should behave as a writable stream of objects
*/
map<T, R>(
mapper: (chunk: T, encoding: string) => R,
options?: ThroughOptions,
): NodeJS.ReadWriteStream;
/**
* Return a ReadWrite stream that flat maps streamed chunks
* @param mapper The mapper function, mapping each (chunk, encoding) to an array of new chunks (or a promise of such)
* @param options
* @param options.readableObjectMode Whether this stream should behave as a readable stream of objects
* @param options.writableObjectMode Whether this stream should behave as a writable stream of objects
*/
flatMap<T, R>(
mapper:
| ((chunk: T, encoding: string) => R[])
| ((chunk: T, encoding: string) => Promise<R[]>),
options?: ThroughOptions,
): NodeJS.ReadWriteStream;
/**
* Return a ReadWrite stream that splits streamed chunks using the given separator
* @param separator The separator to split by, defaulting to "\n"
*/
split(
separator?: string | RegExp,
): NodeJS.ReadWriteStream;
/**
* Return a ReadWrite stream that joins streamed chunks using the given separator
* @param separator The separator to join with
*/
join(separator: string): NodeJS.ReadWriteStream;
2018-11-26 01:00:37 +00:00
/**
* Return a ReadWrite stream that collects streamed chunks into an array or buffer
2018-11-26 01:00:37 +00:00
* @param options
* @param options.objectMode Whether this stream should behave as a stream of objects
*/
collect(
options?: ReadableOptions,
): NodeJS.ReadWriteStream;
2018-11-26 01:00:37 +00:00
/**
* Return a stream of readable streams concatenated together
* @param streams The readable streams to concatenate
*/
concat(
2018-11-26 01:00:37 +00:00
...streams: NodeJS.ReadableStream[]
): NodeJS.ReadableStream;
```
### Interfaces
```ts
interface ReadableOptions {
objectMode?: boolean;
}
interface ThroughOptions {
readableObjectMode?: boolean;
writableObjectMode?: boolean;
}
2018-11-26 01:00:37 +00:00
```
2018-11-29 04:59:24 +00:00
### { utils }
2018-11-26 01:00:37 +00:00
```ts
/**
* Resolve after the given delay in milliseconds
*
2018-11-26 06:18:01 +00:00
* @param ms The number of milliseconds to wait
2018-11-26 01:00:37 +00:00
*/
sleep(ms: number): Promise<{}>;
2018-11-26 06:18:01 +00:00
/**
* Resolve a value after the given delay in milliseconds
*
* @param value Value to resolve
* @param ms Number of milliseconds to wait
*/
delay<T>(value: T, ms: number): Promise<T>;
2018-11-26 06:18:01 +00:00
2018-11-26 01:00:37 +00:00
/**
* Resolve once the given event emitter emits the specified event
*
2018-11-26 06:18:01 +00:00
* @param emitter Event emitter to watch
* @param event Event to watch
*/
once<T>(
2018-11-26 06:18:01 +00:00
emitter: NodeJS.EventEmitter,
event: string,
): Promise<T>;
2018-11-26 01:00:37 +00:00
```