Emiteer is a lightweight and flexible event emitter library for JavaScript and TypeScript. It allows you to manage events and event listeners in your applications with ease.
You can install Emiteer via npm:
npm install emiteer
First, import Emiteer in your JavaScript or TypeScript file:
import Emiteer from 'emiteer'
Then, create an instance of Emiteer:
const emiteer = new Emiteer()
You can add event listeners using the on
method:
const subscriptionToken = emiteer.on('eventName', (payload, eventName) => {
console.log(`Received event: ${eventName}, Payload:`, payload)
})
Another Example:
function logPayload(payload) {
console.log(`Logging payload: ${payload}`)
}
emiteer.on('payload:show', logPayload)
You can also subscribe to an event that will be triggered only once using the once
method:
const onceToken = emiteer.once('eventNameOnce', (payload, eventName) => {
console.log(`Received event once: ${eventName}, Payload:`, payload)
})
You can emit events using the emit
method:
emiteer.emit('eventName', { message: 'Hello, Emiteer!' })
Another example:
function logPayload(payload) {
console.log(`Logging payload: ${payload}`)
}
emiteer.on('payload:show', logPayload)
// ------------- another file, component, fn
const payload = { foo: 'bar' }
emiteer.emit('payload:show', payload)
// "Logging payload: {foo: bar}"
To remove event listeners, you can use the off
method:
emiteer.off('eventName', subscriptionToken)
destroyAllHandlers
: Clears all event handlers.destroyAllHandlersForEvent(event: string)
: Clears event handlers for a specific event.getSubscribers
: Retrieves a copy of all subscribers mapped by event name.getSubscribersByName(event: string)
: Retrieves a copy of subscribers for a specific event.hasSubscriber(event: string, token?: string)
: Checks if a subscriber exists for an event.hasSubscribers(event?: string | null)
: Checks if any subscribers exist.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.