@jotter/emitter
v1.1.2
Published
Simple and modern emitter library.
Downloads
9
Maintainers
Readme
EventEmitter
Simple and modern event emitter library.
一个功能丰富的事件订阅/发布库,方便您在应用程序中实现事件的订阅、发布和取消订阅。
Install
npm
npm install @jotter/emitter
browser
https://cdn.jsdelivr.net/npm/@jotter/emitter/dist/index.global.js
Usage
import EventEmitter from '@jotter/emitter'
const emitter = new EventEmitter()
function handleMessage(arg1, arg2) {
// ...
console.log('message 1: ', arg1, arg2)
}
emitter.on('message', handleMessage)
emitter.once('message', function(data) {
console.log('message 2:', data)
})
emitter.emit('message', 'hello', 'world')
// message 1: hello world
// message 2: hello
emitter.off('message', handleMessage)
API
Instance Methods
on
on(type: string | symbol, listener: Function, context?: any): this;
Subscribe to an event
type
- the name of the event to subscribe tolistener
- the function to call when event is emittedcontext
- (OPTIONAL) - the context to bind the event callback to
once
once(type: string | symbol, listener: Function, context?: any): this;
Subscribe to an event only once.
type
- the name of the event to subscribe tolistener
- the function to call when event is emittedcontext
- (OPTIONAL) - the context to bind the event callback to
emit
emit(type: string | symbol, ...args: any[]): this;
Trigger a named event
type
- the event name to emitargs
- any number of arguments to pass to the event subscribers
off
off(type: string | symbol, listener?: Function): this;
Unsubscribe from an event type.
If no listener are provided, it cancels all listeners on that event type.
type
- the name of the event to unsubscribe fromlistener
- the function used when binding to the event
clear
clear(type?: string | symbol): this;
Unsubscribe from an event or all events.
type
- the name of the event to unsubscribe from
get
get(type?: string | symbol | '*'): Set<EventHandler> | Map<string | symbol, Set<EventHandler>>;
Get all listeners of the subscribed event type.
type
- the name of the event to unsubscribe from.- If the type is
*
, return the handler for all subscribed event types. - If the type is empty, return the subscribed event Map.
- If the type is
size
size(type?: string | symbol | '*'): number;
Get the number of listeners for the specified event type.
type
- The event type to get the number of listeners for.- If the type is
*
, return the number of handlers for all subscribed event types. - If the type is empty, return the number of subscribed event types.
- If the type is
has
has(type: string | symbol): boolean;
Check the subscribed event type has listener.
type
- the name of the event to unsubscribe from
Thanks
mitt , tiny-emitter , pico-emitter , emittery