【问题标题】:Implement finish event handler of custom writable stream class实现自定义可写流类的完成事件处理程序
【发布时间】:2016-07-24 03:57:57
【问题描述】:

假设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');
    }
  });

【问题讨论】:

    标签: node.js writable


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多