1.1.2 • Published 12 months ago

@jotter/emitter v1.1.2

Weekly downloads
-
License
MIT
Repository
github
Last release
12 months ago

EventEmitter

version download license

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 to
  • listener - the function to call when event is emitted
  • context - (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 to
  • listener - the function to call when event is emitted
  • context - (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 emit
  • args - 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 from
  • listener - 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.

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.

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

1.1.2

12 months ago

1.1.1

1 year ago

1.0.2

1 year ago

1.1.0

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago

0.1.0

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago