stream-demux
Advanced tools
Comparing version 1.1.0 to 1.2.0
@@ -35,3 +35,3 @@ const AsyncIterableStream = require('async-iterable-stream'); | ||
getStream(name) { | ||
stream(name) { | ||
return new AsyncIterableStream(() => { | ||
@@ -38,0 +38,0 @@ return this.createDemuxedStream(this.mainStream, name); |
{ | ||
"name": "stream-demux", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "An iterable asynchronous stream demultiplexer.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
# stream-demux | ||
An iterable asynchronous stream demultiplexer. | ||
An asynchronous iterable stream demultiplexer. | ||
Lets you write data to multiple async iterable streams from a central place without keeping any references to those streams. | ||
# Usage | ||
```js | ||
let demux = new StreamDemux(); | ||
(async () => { | ||
// Consume data from 'abc' stream. | ||
let substream = demux.stream('abc'); | ||
for await (let packet of substream) { | ||
console.log('ABC:', packet); | ||
} | ||
})(); | ||
(async () => { | ||
// Consume data from 'def' stream. | ||
let substream = demux.stream('def'); | ||
for await (let packet of substream) { | ||
console.log('DEF:', packet); | ||
} | ||
})(); | ||
(async () => { | ||
for (let i = 0; i < 10; i++) { | ||
await wait(10); | ||
demux.write('abc', 'message-abc-' + i); | ||
demux.write('def', 'message-def-' + i); | ||
} | ||
demux.end('abc'); | ||
demux.end('def'); | ||
})(); | ||
``` | ||
# Goal | ||
The goal of this module is to efficiently distribute data to a large number of named asynchronous streams while facilitating programming patterns which decrease the probability of memory leaks. | ||
Each stream returned by this module is responsible for picking up its own data from a shared source stream - This means that the stream-demux module doesn't hold any references to that streams which it produces via its `stream()` method; this reduces the likelihood of programming mistakes which lead to memory leaks since the streams don't need to be destroyed or cleaned up explicitly. | ||
The downside to making each stream responsible for consuming its own data is that having a lot of concurrent streams can have a negative impact on performance (especially if there are a lot of idle streams). The goal of stream-demux is to keep that overhead to a minimum. |
@@ -35,3 +35,3 @@ const assert = require('assert'); | ||
(async () => { | ||
let substream = demux.getStream('hello'); | ||
let substream = demux.stream('hello'); | ||
for await (let packet of substream) { | ||
@@ -42,3 +42,3 @@ receivedHelloPackets.push(packet); | ||
(async () => { | ||
let substream = demux.getStream('abc'); | ||
let substream = demux.stream('abc'); | ||
for await (let packet of substream) { | ||
@@ -73,3 +73,3 @@ receivedAbcPackets.push(packet); | ||
let receivedPacketsC = []; | ||
let substream = demux.getStream('hello'); | ||
let substream = demux.stream('hello'); | ||
@@ -76,0 +76,0 @@ await Promise.all([ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6915
45