@arction/eventer

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

Simple and fast event emitter.

原始使用文档
文档目录7 节
  1. Installation
  2. Documentation
  3. Getting started
  4. Subscribe to topic
  5. Unsubscribe from topic
  6. Emiting events
  7. Development instructions

Eventer

Simple and fast event emitter library. Subscription is based on a unique Token, which can be used to unsubscribe from the topic.

The Eventer is used in LightningChart® JS charting library. https://www.arction.com/

Installation

npm install --save @arction/eventer

Documentation

Online documentation is available at arction.github.io/eventer

Getting started

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

Subscribe to topic

Subscribing to a topic is easy.

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

Multiple subscriptions to a single topic can exist.

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

Unsubscribe from topic

You can unsubscribe from a single topic by using the Token returned by eventer when you subscribed to the topic.

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

eventer.off(token)

You can also unsubscibe all listeners from a topic.

eventer.topicOff('topic')

It is also possible to unsubscribe all topics and all listeners.

eventer.allOff()

Emiting events

Events can be emitted for a topic. The emit can contain 0 or more arguments that will be provided to the topic listeners.

eventer.emit('topic')

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

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

Development instructions

The project is developed using TypeScript. Build system of the project heavily relies on Node.js. Dependencies are managed with npm, therefore, remember to run npm install before starting of anything else.

There are several npm scripts, which are used for development process:

NameCommandDescription
buildnpm run buildrun build system
testnpm testrun tests and watch
lintnpm run lintrun static analyzer and watch
docsnpm run docsgenerate typedoc documentation
ci:testnpm run ci:testrun tests once
ci:lintnpm run ci:lintrun static analyzer once
ci:watchnpm run ci:watchrun CI circle and watch

版本与发布记录

@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 是什么?

Simple and fast event emitter.

如何安装 @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 和提交记录判断。