如何将nodejs计数器可读的流示例转换为简化的施工版本?

发布于 2025-02-12 02:01:46 字数 2074 浏览 0 评论 0 原文

我参考此doc

_construct()方法在我的版本中从未调用。为什么?

注意:在简化的构造中,您提供了 _CONSTRUCT()方法的实现。 }

这是一个可读的流计数器的工人阶级版本:

const { Readable } = require('stream');

class Counter extends Readable {
  constructor(options) {
    super(options);
    this._max = 10;
    this._index = 1;
  }

  _read() {
    const i = this._index++;
    if (i > this._max) {
      this.push(null);
    } else {
      const str = String(i);
      const buf = Buffer.from(str, 'utf-8');
      this.push(buf);
    }
  }
}

const readable = new Counter();

readable.on('readable', function() {
  console.log('readable');

  let data;

  while ((data = this.read()) !== null) {
    console.log(String(data));
  }
});

readable.on('close', function() {
  console.log('close');
});

这是我无法使用的简化施工版本:

const { Readable } = require('stream');

const counter = new Readable({

  construct() {
    this._max = 10;
    this._index = 1;
    console.log(this._max); // This is never executed
  },

  read() {
    console.log(this._max); // This is undefined
    this.push(null);
  }

  // read() {
  //   const i = this._index++;
  //   if (i > this._max) {
  //     this.push(null);
  //   } else {
  //     const str = String(i);
  //     const buf = Buffer.from(str, 'utf-8');
  //     this.push(buf);
  //   }
  // }

});

counter.on('readable', function() {
  let data;

  while ((data = this.read()) !== null) {
    console.log(String(data));
  }
});

counter.on('close', function() {
  console.log('close');
});

文档说:

const { Writable } = require('node:stream');

const myWritable = new Writable({
  construct(callback) {
    // Initialize state and load resources...
  },
  write(chunk, encoding, callback) {
    // ...
  },
  destroy() {
    // Free resources...
  }
});

现在我很困惑。认为初始化代码属于 _CONSTRUCT()方法?

I refer to this doc

https://nodejs.org/api/stream.html#simplified-construction

The _construct() method is never called in my version. Why?

Note: In simplified construction you provide the implementation of _construct() method in the constructor options {construct() {...}, ...}

This is a working class version of a readable stream counter example:

const { Readable } = require('stream');

class Counter extends Readable {
  constructor(options) {
    super(options);
    this._max = 10;
    this._index = 1;
  }

  _read() {
    const i = this._index++;
    if (i > this._max) {
      this.push(null);
    } else {
      const str = String(i);
      const buf = Buffer.from(str, 'utf-8');
      this.push(buf);
    }
  }
}

const readable = new Counter();

readable.on('readable', function() {
  console.log('readable');

  let data;

  while ((data = this.read()) !== null) {
    console.log(String(data));
  }
});

readable.on('close', function() {
  console.log('close');
});

This is my not working simplified construction version:

const { Readable } = require('stream');

const counter = new Readable({

  construct() {
    this._max = 10;
    this._index = 1;
    console.log(this._max); // This is never executed
  },

  read() {
    console.log(this._max); // This is undefined
    this.push(null);
  }

  // read() {
  //   const i = this._index++;
  //   if (i > this._max) {
  //     this.push(null);
  //   } else {
  //     const str = String(i);
  //     const buf = Buffer.from(str, 'utf-8');
  //     this.push(buf);
  //   }
  // }

});

counter.on('readable', function() {
  let data;

  while ((data = this.read()) !== null) {
    console.log(String(data));
  }
});

counter.on('close', function() {
  console.log('close');
});

The docs say:

const { Writable } = require('node:stream');

const myWritable = new Writable({
  construct(callback) {
    // Initialize state and load resources...
  },
  write(chunk, encoding, callback) {
    // ...
  },
  destroy() {
    // Free resources...
  }
});

Now I'm confused. Thought initialization code belongs into the _construct() method?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鱼窥荷 2025-02-19 02:01:46

如我上面的评论所述, _CONSTRUCT()在节点版本15.x中添加。为了进一步参考,请按照以下示例:

const { Readable } = require('stream');

const counter = new Readable({

  construct(callback) {
    this._max = 10;
    this._index = 1;
    callback(); // Signal completion of initialization
  },

  read() {
    const i = this._index++;
    if (i > this._max) {
      this.push(null);
    } else {
      const str = String(i);
      const buf = Buffer.from(str, 'utf-8');
      this.push(buf);
    }
  }

});

counter.on('readable', function() {
  let data;

  while ((data = this.read()) !== null) {
    console.log(String(data));
  }
});

counter.on('close', function() {
  console.log('close');
});

As stated in my comment above, _construct()is added in Node Version 15.x. For further reference, follows the working example:

const { Readable } = require('stream');

const counter = new Readable({

  construct(callback) {
    this._max = 10;
    this._index = 1;
    callback(); // Signal completion of initialization
  },

  read() {
    const i = this._index++;
    if (i > this._max) {
      this.push(null);
    } else {
      const str = String(i);
      const buf = Buffer.from(str, 'utf-8');
      this.push(buf);
    }
  }

});

counter.on('readable', function() {
  let data;

  while ((data = this.read()) !== null) {
    console.log(String(data));
  }
});

counter.on('close', function() {
  console.log('close');
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文