Wait for drain when write returns false in demux

This commit is contained in:
Jerry Kurian
2019-09-10 18:13:13 -04:00
parent ee3d9b9ded
commit 9d280b1662
2 changed files with 62 additions and 143 deletions

View File

@@ -62,7 +62,7 @@ class Demux extends Writable {
}
// Throttles when one stream is not writable
public _write(chunk: any, encoding?: any, cb?: any) {
public async _write(chunk: any, encoding: any, cb: any) {
const destKey = this.demuxer(chunk);
if (this.streamsByKey[destKey] === undefined) {
this.streamsByKey[destKey] = {
@@ -70,21 +70,12 @@ class Demux extends Writable {
writable: true,
};
}
let res = false;
if (this.streamsByKey[destKey].writable && this.isWritable) {
res = this.streamsByKey[destKey].stream.write(chunk, encoding, cb);
}
if (!res && this.isWritable) {
this.isWritable = false;
this.streamsByKey[destKey].writable = false;
this.nonWritableStreams.push(destKey);
this.streamsByKey[destKey].stream.once("drain", () => {
this.nonWritableStreams.filter(key => key !== destKey);
this.isWritable = this.nonWritableStreams.length === 0;
this.streamsByKey[destKey].stream.write(chunk, encoding, cb);
if (this.isWritable) {
if (!this.streamsByKey[destKey].stream.write(chunk, encoding, cb)) {
await new Promise((resolve, reject) => {
this.streamsByKey[destKey].stream.once("drain", () => {
resolve();
this.emit("drain");
}
});
});
}
}