我不知道ES6語法,但這裏是一個傳統的變換流接收TAP輸出和輸出對象描述各種解析的部分:
const Transform = require('stream').Transform;
const inherits = require('util').inherits;
const Parser = require('tap-parser');
function MyTransform() {
const self = this;
this._parser = new Parser();
Transform.call(this, { readableObjectMode: true });
this._parser.on('complete', function(results) {
self.push({ type: 'complete', results });
}).on('assert', function(assert) {
self.push({ type: 'assert', assert });
}).on('comment', function(comment) {
self.push({ type: 'comment', comment });
}).on('plan', function(plan) {
self.push({ type: 'plan', plan });
}).on('version', function(version) {
self.push({ type: 'version', version });
}).on('bailout', function(reason) {
self.push({ type: 'bailout', reason });
}).on('extra', function(extra) {
self.push({ type: 'extra', extra });
});
}
inherits(MyTransform, Transform);
MyTransform.prototype._write = function(chunk, encoding, cb) {
this._parser.write(chunk, cb);
};
MyTransform.prototype._flush = function(cb) {
const self = this;
this._parser.end(function() {
self.push(null);
cb();
});
};
爲什麼需要擴展解析器?你不能只是實例化一個解析器,只是像正常一樣附加事件監聽器? – mscdex
理想情況下,我想在內部使用'writeable's'事件偵聽器來創建一個可以傳入和傳出的'transform'流。 – Breedly