@arction/eventer

中文文档
1.0.2发布于 2022/09/21MITTypeScriptESM + CJS零依赖近 30 天下载 1万

简单而快的事件发射器.

中文使用文档
文档目录7 节
  1. 安装
  2. 文档
  3. 开始
  4. 订阅主题
  5. 从主题取消订阅
  6. 发生事件
  7. 发展指示

偶数

简单快活事件发送库. 订阅基于一个独特的Token,可用于不订阅这个话题.

Eventer 用于闪电图® 联合来文图书馆. https://www.arction.com/ (中文(简体) )

安装

npm install --save @arction/eventer

文档

在线文件可上网查阅 arction.github.io/eventer (英语)

开始

import { Eventer } from '@arction/eventer'

const eventer = new Eventer()

// new topic is created, handler is added as the first subscriber to the topic
const token1 = eventer.on('topic1', () => console.log('I am called from topic1'))
// 'topic1' already exists, so new listener is simply added to existing collection of handlers
const token2 = eventer.on('topic1', () => console.log('I am also called from topic1'))
// new collection of handlers is created for the second topic
// the function is added to it
eventer.on('topic2', () => console.log('I am called from topic2'))

eventer.emit('topic1')
// calls two functions which listen to the event
//
// outputs:
//    I am called from topic1
//    I am also called from topic1

eventer.emit('topic2')
// calls single functions which listen to the event
//
// outputs:
//    I am called from topic2

// remove the second listener from the first topic by token 
eventer.off(token2)

eventer.emit('topic1')
// calls single functions which listen to the event
//
// outputs:
//    I am called from topic1

// remove all listeners from all topics
eventer.allOff()

eventer.emit('topic2')
// nothing happened, since all subscriptions were terminated

订阅主题

订阅一个主题很容易.

eventer.on('topic', ()=>{ console.log('Topic handler') })

单个话题的多订阅可以存在.

eventer.on('topic', ()=>{ console.log('Topic handler') })
eventer.on('topic2', ()=>{ console.log('Topic 2 handler') })
eventer.on('topic3', ()=>{ console.log('Topic 3 handler') })

从主题取消订阅

您可以使用 脚本 当您订阅话题时, 由事件器返回 .

const token = eventer.on('topic', ()=>{ console.log('Topic handler') })

eventer.off(token)

您也可以从话题中解密所有听众 .

eventer.topicOff('topic')

还有可能取消所有话题和所有听众的订阅.

eventer.allOff()

发生事件

事件可以针对一个话题发布. 发音可以包含0个或更多个会提供给话题听众的参数.

eventer.emit('topic')

eventer.on('topic2', ( arg1, arg2 ) => console.log( arg1, arg2 ))

eventer.emit('topic2', 'Hello ', 'World')

发展指示

该项目使用TypeScript开发. 项目的构建系统严重依赖于Node.js. 依赖性由 无因此,记住运行 npm 安装 在开始其他事情之前.

有几个 npm 脚本,用于发展进程:

名称命令说明
构建npm 运行构建运行构建系统
测试npm 测试运行测试和监视
线性npm 运行 lint运行静态分析器和监视
文档npm 运行文件生成类型文档
ci:测试npm 运行 ci: test运行一次测试
坐标:lintnpm 运行 ci:lint运行一次静态分析器
ci:观察npm 运行 ci: watch运行 CI 圆并监视

版本与发布记录

@arction/eventer 共发布 3 个版本,首次发布于 2019/07/04;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.2latest2022/09/214 年前
1.0.12019/08/057 年前
1.0.02019/07/047 年前

关于 @arction/eventer 的常见问题

@arction/eventer 是什么?

简单而快的事件发射器.

如何安装 @arction/eventer?

在项目目录运行 npm install @arction/eventer,或使用 pnpm add / yarn add / bun add。

@arction/eventer 支持 TypeScript 吗?

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

@arction/eventer 支持 ESM 还是 CommonJS?

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

@arction/eventer 可以商用吗?

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

@arction/eventer 的下载量有多少?

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

@arction/eventer 还在维护吗?

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