1.0.2 • Published 9 months ago

@neosjs/emitter v1.0.2

Weekly downloads
-
License
MIT
Repository
-
Last release
9 months ago

@neosjs/emitter

Npm Version Npm Week Downloads Npm Month Downloads Minzip License Emitter Githu Ci

简易的事件订阅/发布器,可实现多窗口同步消息

安装

pnpm add @neosjs/emitter

npm install @neosjs/emitter

yarn add @neosjs/emitter

使用

基础使用

import NeosEmitter from '@neosjs/emitter'

const emitter = new NeosEmitter()

// 订阅
emitter.on('test', (data) => {
  // …
})

// 发布
emitter.emit('test', 'hello world')

多窗口同步

import NeosEmitter from '@neosjs/emitter'

const emitter = new NeosEmitter({
  channelName: 'neosjs', // 通道名称(默认值:@neosjs-broadcast-emitterevent)
  broadcast: true, // 是否开启广播(多窗口同步消息)
  debug: true, // 是否开启调试(默认值:false)
})

// 窗口B订阅、监听事件
emitter.on('test', (data) => {
  // …
})

// 窗口A发布事件
emitter.emit('test', 'hello world')

参数

NeosEmitter(options: EmitterOptions)

参数说明类型默认值
options配置项EmitterOptions{}
options.channelName通道名称string@neosjs-broadcast-emitterevent
options.broadcast是否开启广播booleanfalse
options.debug是否开启调试booleanfalse

方法

on(event: string, listener: Function)

订阅事件

参数说明类型
event事件名称string
listener事件监听器Function

once(event: string, listener: Function)

订阅一次事件

参数说明类型
event事件名称string
listener事件监听器Function

emit(event: string, data: any)

发布事件

参数说明类型
event事件名称string
data事件数据any

off(event: string, listener?: Function)

取消订阅事件

参数说明类型
event事件名称string
listener事件监听器Function

destroy()

销毁实例

License

MIT License © 2021-PRESENT NeosJS

1.0.2

9 months ago

1.0.1

10 months ago

1.0.0

10 months ago