@liaoys/events
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

@liaoys/events

An event emitter.

See example

Installation

npm install @liaoys/events

Usage

import Events from "@liaoys/events"

const event = new Events()

// Add event listener
event.on('foo', (param) => {
  console.log(`foo triggered! parameter is '${param}'`)
})

// Add an event listener, and it will be invoked only once
event.once('foo', () => {
  console.log('foo emitted!')
})

// trigger event
event.emit('foo', 'Hi!')

type: Listener

(...args: any[]) => void;

A callback function, accept some parameters from emit method of instance.

Instance methods

on(eventName: string, listener: Listener, prepend?: boolean): Events

  • eventName The name of the event
  • listener The callback function
  • prepend Add the listener to the beginning of the listeners array. default: false

Add event listener.

off(eventName: string, listener?: Listener): Events

  • eventName The name of the event
  • listener The callback function

Remove the specified listener from the listeners array for the event named eventName.

If without the parameter listener, will remove all the listeners from the listeners array for the event named eventName.

once(eventName: string, listener: Listener, prepend?: boolean): Events

  • eventName The name of the event
  • listener The callback function
  • prepend Add the listener to the beginning of the listeners array. default: false

Add an event listener, and it will be invoked only once.

emit(eventName, ...args: any[]): boolean

  • eventName The name of the event
  • args Parameters passed to listener.

Trigger the event named eventName.

Returns true if the event had listeners, false otherwise.

Package Sidebar

Install

npm i @liaoys/events

Weekly Downloads

1

Version

1.0.0

License

ISC

Unpacked Size

69.4 kB

Total Files

35

Last publish

Collaborators

  • liaoyuesheng