偶数
简单快活事件发送库. 订阅基于一个独特的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 | 运行一次测试 |
| 坐标:lint | npm 运行 ci:lint | 运行一次静态分析器 |
| ci:观察 | npm 运行 ci: watch | 运行 CI 圆并监视 |