@async-generators/subject

中文文档
0.5.0发布于 2018/01/18MITTypeScriptESM + CJS近 30 天下载 2754

push items to pulling iterators

原始使用文档
文档目录5 节
  1. Install
  2. Api
  3. Soupler()
  4. Example
  5. Typescript

subject

logo

push items to pulling iterators

NPM version Travis Status Travis Status

Install

npm install @async-generators/subject --save
yarn add @async-generators/subject

This package's main entry points to a commonjs dist. The module entry points to a es2015 module dist. Both require native async-generator support, or be down-compiled with a webpack loader.

Api

Soupler()

Subject that provides three methods: next(item), error(err), and done() to push data and events. When [Symbol.asyncIterator] is called an internal subscription is created. pushed items are buffered (per iterator) until they are pulled by the consuming iterator. If the consuming iterator pulls items slower than the speed they are pushed to the subject, then the internal buffer will continue to grow in size. error(err) will cause the iterator to rethrow the given error to the consumer and dispose of the subject.

Example

example.js

const {Subject} = require('@async-generators/subject');

let subject = new Subject<number>();

async function* source(){
  yield 1; yield 2; yield 3; yield 4;
}

async function main() {
  let reader = async function () {
    for await (let item of subject) {
      console.log("PULL:", item);
    }
  };

  let readers = [reader(), reader()];
  
  let writer = async function () {
    for await (let item of source()){
      console.log("PUSH:", item);
      subject.next(item);
    }
    subject.done();
  }

  await writer();
  await Promise.all(readers);
}

main().catch(console.log);

Execute with the latest node.js 9:

node --harmony example.js

output:

PUSH: 1
PUSH: 2
PULL: 1
PULL: 1
PUSH: 3
PULL: 2
PULL: 2
PUSH: 4
PULL: 3
PULL: 3
PULL: 4
PULL: 4

Typescript

This library is fully typed and can be imported using:

import {Subject} from '@async-generators/subject');

It is also possible to directly execute your properly configured typescript with ts-node:

ts-node --harmony foo.ts

版本与发布记录

@async-generators/subject 共发布 7 个版本,首次发布于 2017/09/24;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.5.0latest2018/01/188 年前
0.4.02018/01/168 年前
0.3.12018/01/138 年前
0.3.02018/01/128 年前
0.2.12017/09/299 年前
0.2.02017/09/299 年前

关于 @async-generators/subject 的常见问题

@async-generators/subject 是什么?

push items to pulling iterators

如何安装 @async-generators/subject?

在项目目录运行 npm install @async-generators/subject,或使用 pnpm add / yarn add / bun add。

@async-generators/subject 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@async-generators/subject 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@async-generators/subject 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=9.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@async-generators/subject 可以商用吗?

@async-generators/subject 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@async-generators/subject 的下载量有多少?

根据 npm 官方下载统计,@async-generators/subject 在 2026/10/07 – 2026/11/05 这 30 天内被下载 2,754 次,日均约 92 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@async-generators/subject 还在维护吗?

最新版本 0.5.0 发布于 2018/01/18(8 年前);至今共发布 7 个版本,其中过去 12 个月发布了 0 个;首次发布于 2017/09/24。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。