0

假设这customWS是一个可写流..

  util.inherits(customWS, stream.Writable);

我们实现我们的逻辑来处理_write()如下所示的写入..

  customWS.prototype._write = function(chunk, encoding, done) {
    // ...
  }

现在要使用customWS该类,我们将执行以下操作..

  aReadableStream.pipe(new customWS()).on('finish', callback);

那么callback函数的参数是什么?

我可以通过一个callback喜欢..

  function(data) {
    // ...    
  }

..还是固定的?

如果它不固定那么如何在customWS类中实现这样的回调?

有什么像..

  // in the implementation of customWS class
  customWS.prototype._finish = function(user_specified_callback) {
    user_specified_callback(some_data_say_a_bool_val);
  }

  // in the code, where i use the customWS class
  aReadableStream.pipe(new customWS()).on('finish', function(flag) {
    if (flag) {
      console.log('yes');
    }
  });
4

1 回答 1

0

您通常不需要做任何事情来支持finish(). 此处记录了该finish事件及其签名。它没有任何参数,它只是通知可写流已“关闭”并且不能再写入。

如果作为流实现者,您需要在可写流的“关闭”之前专门做一些事情,那么在撰写本文时,您或多或少不走运。有一个PR可以将此功能添加到可写流。如果您正在实现一个转换流,您将拥有_flush()您想要的,但这仅在您实现双工流(不是仅可写的)时才有用。

于 2016-04-04T15:48:55.227 回答