Created
November 14, 2017 18:44
-
-
Save verchol/9b9005ecacef10ad8d518b72b44ff32f to your computer and use it in GitHub Desktop.
using kefir and nodejs stream
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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