In Following Code :
const { Writable } = require("stream");
class Custome extends Writable {
constructor(option) {
super(option);
}
_write(chunk, config, callback) {
console.log(chunk.toString());
setTimeout(() => {
callback(null)
}, 1000);
}
}
const log = new Custome({ highWaterMark: 16,defaultEncoding:"utf-8" });
log.write("0");
log.on("drain",()=>{
console.log("Strem drained")
})
for (let i = 1; i < 51; i++) {
const writable = log.write(i.toString());
console.log(writable);
}
Here is custom writable stream with watermark of 16 Bytes, I thought as each number can be represent by 1 byte in utf-8, so buffer will at first 16 number i.e. till 15 after that overflowing will start, and further data will may get lost, But there was 11 true, and after that all false, excepted was 16 true. After buffer is full and then buffer data is processed , drain event will be emitted, but it was emitted at end, do known why? and all number was printed sync without any loss why? As buffer full and there is no space data should be lost na?
Excepted
>0
>true * 15
>false (remaining)
1 ... 15
and remaining log but with missing number
Actual
>0
>true * 11
> false ...
>1 to 50
Multiple code Execution