1.0.0 • Published 4 years ago
@liaoys/events v1.0.0
@liaoys/events
An event emitter.
See example
Installation
npm install @liaoys/eventsUsage
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
eventNameThe name of the eventlistenerThe callback functionprependAdd the listener to the beginning of the listeners array. default: false
Add event listener.
off(eventName: string, listener?: Listener): Events
eventNameThe name of the eventlistenerThe 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
eventNameThe name of the eventlistenerThe callback functionprependAdd 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
eventNameThe name of the eventargsParameters passed to listener.
Trigger the event named eventName.
Returns true if the event had listeners, false otherwise.
1.0.0
4 years ago