我已经使用rxjs
一段时间了,我喜欢如何使用它的运算符进行逻辑编程,而不是命令式编程。然而,我也喜欢node的流,它们也是高度可组合的,所以我的明显反应是同时使用它们,但除了在rxjs的书中为其绑定之外,我还没有看到它被提及很多(实际上,我根本没有)。
所以,我的问题是,我如何利用RxJS上npm中的所有转换流?或者,这可能吗
示例:-
var fs = require('fs');
var csv = require('csv-parse')({delimiter:';'});
var src = fs.createReadStream('./myFile.csv');
src.pipe(csv).pipe(process.stdout);
本质上,我想这样做:-
var fs = require('fs');
var csv = require('csv-parse')({delimiter:';'});
var rx= require('rx-node');
var src = fs.createReadStream('./myFile.csv');
var obj = rx.fromReadableStream(src);
obj.pipe(csb).map(x=>console.log(x));
过去我被告知要使用highland
,但我在这里严格寻找rxjs
解决方案。
您不必使用rx-node
,但您可以!记住:All streams are event emitters!
。
准备:input.txt
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
运行:
npm install through2 split2 rx rx-node
并且在index.js
:中
var Rx = require('rx');
Rx.Node = require('rx-node');
var fs = require('fs');
var th2 = require('through2');
var split2 = require('split2');
var file = fs.createReadStream('./input.txt').on('error', console.log.bind(console, 'fs err'));
var transform = th2(function(ch, en, cb) {
cb(null, ch.toString());
}).on('error', function(err) {
console.log(err, err.toString());
});
// All streams are event emitters ! (one way without using rx-node)
// var subs = Rx.Observable.fromEvent(transform, 'data').share();
// subs
// .map(value => 'Begin line: ' + value)
// .subscribe(value => console.log(value));
// rx-node has convenience functions (another way)
var subs = Rx.Node.fromTransformStream(transform).share()
.map(value => 'Begin line: ' + value)
.subscribe(value => console.log(value));
file.pipe(split2()).pipe(transform);
输出:
Begin line: Hello World!
Begin line: Hello World!
Begin line: Hello World!
Begin line: Hello World!
Begin line: Hello World!
EdinM给出了一个在节点转换流中使用RxJS的好的一般示例,但您最初的问题仍未得到解答。由于几天前我遇到了几乎相同的问题,我想努力为那些不熟悉在Node中使用RxJS的人回答这个问题。我将使用csv-streamify
,而不是使用csv-parse
模块。让我们设置基本结构:
test_data.csv:
thing,name,owner,loc
chair,sitty,billy,san fran
table,setty,bryan,new oak
运行:
$ npm install rx rx-node csv-streamify
index.js:
"use strict";
const Rx = require('rx');
Rx.Node = require('rx-node');
const fs = require('fs');
const csv = require('csv-streamify');
//Setting up the transform-stream CSV parser
let config = {
delimiter: ',', // comma, semicolon, whatever
newline: 'n', // newline character (use rn for CRLF files)
quote: '"', // what's considered a quote
empty: '', // empty fields are replaced by this
//objectMode: true, //parses csv table into an array of objects
//columns: true //uses column headers for the object fields
};
let parseCsv = csv(config);
//Setting up the RxJS Observer
function onNext (x) {
//do your side-effects here, after the data has
//gone through the observables operator chain
console.log('Next: ' + x);
};
function onError (err) {
console.log('Error: ' + err);
};
function onComplete () {
console.log('Completed');
};
let readStream = fs.createReadStream('test_files/test_data.csv');
readStream.pipe(parseCsv);
let subscription = Rx.Node.fromTransformStream(parseCsv)
//do something with the data with an operator such as:
//.map()
.subscribe(onNext, onError, onComplete);
现在让我们运行代码:
$ node index.js
我们将得到这个输出:
Next: ["thing","name","owner","locr"]
Next: ["chair","sitty","billy","san franr"]
Next: ["table","setty","bryan","new oak"]
Completed
结语
如果您在csv配置对象中将objectMode
和columns
设置为true,然后使用映射运算符来投影此sideEffect
函数,如下所示:
function sideEffect (v){
console.log(v)
return v;
};
let subscription = Rx.Node.fromTransformStream(parseCsv)
.map(sideEffect)
.subscribe(onNext, onError, onComplete);
您将得到以下输出:
{ thing: 'chair',
name: 'sitty',
owner: 'billy',
'locr': 'san franr' }
Next: [object Object]
{ thing: 'table',
name: 'setty',
owner: 'bryan',
'locr': 'new oak' }
Next: [object Object]
Completed