0.0.5 • Published 10 months ago

@chiui/event-cat v0.0.5

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

@chiui/event-cat

😼 @chiui/event-cat is an event emitter JavaScript library that is small, lightweight, and easy to use. It supports CommonJS, ESM, and UMD modules.

Installation

You can install @chiui/event-cat using npm:

npm install @chiui/event-cat

Usage

import EventCat from '@chiui/event-cat';
// Create an instance
const eventCat = EventCat.create();
// Listen to events
eventCat.on('cat-meow', (data) => {
  console.log(data); // Output: '🐱 Meow! Feed me!'
});
eventCat.emit('cat-meow', '🐱 Meow! Feed me!');
// Listen to events once
eventCat.once('pizza-ready', (data) => {
  console.log(data); // Output: 'šŸ• Your pizza is ready!'
});
eventCat.emit('pizza-ready', 'šŸ• Your pizza is ready!');
eventCat.emit('pizza-ready', 'šŸ• Your pizza is ready!'); // No output, as the listener is removed
// Remove event listeners
const handler = (data) => {
  console.log(data);
};
eventCat.on('alien-invasion', handler);
eventCat.emit('alien-invasion', 'šŸ‘½ Warning! Aliens spotted!'); // Output: 'šŸ‘½ Warning! Aliens spotted!'
eventCat.off('alien-invasion', handler);
eventCat.emit('alien-invasion', 'šŸ‘½ Warning! Aliens spotted!'); // No output, as the listener is removed
// Clear events
eventCat.on('dog-bark', handler);
eventCat.clear('dog-bark');
eventCat.emit('dog-bark', '🐶 Woof woof!'); // No output, as the event is cleared
// Clear all events
eventCat.clear();

Features

  • Event Listening: Use on to listen to events and emit to trigger them.
  • Once Listeners: Use once to listen to an event only once. The listener is automatically removed after it is triggered.
  • Remove Listeners: Use off to remove specific event listeners.
  • Clear Events: Use clear to remove all listeners for a specific event or all events.
  • Singleton Instance: Use EventCat.event to access a global singleton instance of EventCat.

Testing

The library is tested using bun. You can run the tests using:

bun test

License

This project is licensed under the MIT License - see the LICENSE file for details.

0.0.5

10 months ago

0.0.4

10 months ago

0.0.3

10 months ago

0.0.2

10 months ago

0.0.1

10 months ago