Skip to content

Instantly share code, notes, and snippets.

@verchol
Created November 14, 2017 18:44
Show Gist options
  • Save verchol/9b9005ecacef10ad8d518b72b44ff32f to your computer and use it in GitHub Desktop.
Save verchol/9b9005ecacef10ad8d518b72b44ff32f to your computer and use it in GitHub Desktop.
using kefir and nodejs stream
const { Duplex } = require('stream');
const kefir = require('kefir');
class MyWritable extends Duplex {
constructor(options) {
// Calls the stream.Writable() constructor
super(options);
this.buffer = [];
// ...
}
_write(chunk, encoding, callback) {
// ...
this.buffer.push(chunk);
this.push(chunk);
callback(null, chunk);
}
_read(size) {
}
}
const fs = require('fs');
let fStream = fs.createWriteStream("file");
let custom = new MyWritable();
let stream = kefir.stream(emitter => {
var count = 0;
emitter.emit(count);
var intervalId = setInterval(() => {
count++;
if (count < 5) {
emitter.emit(`test${count}\n`);
} else {
emitter.end();
}
}, 1000);
return () => {
clearInterval(intervalId);
}
}).log();
stream.onValue((v)=>{
custom.write(v, null, ()=>{
console.log(`after write`);
});
})
stream.onEnd(custom.end.bind(custom));
custom.pipe(fStream);
fStream.on('finish', ()=>{
console.log('finished');
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment