1.1.5 • Published 4 months ago

cozyevent v1.1.5

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

npm.io

CozyEvent — Fast, Lightweight Event Emitter

npm Build Size Coverage License Downloads PRs withlove styled with prettier

A lightweight, blazing fast, microtask-based asynchronous and synchronous event emitter for JavaScript/TypeScript.

Change log

  • v1.1.0: Performance improvement; 4x faster than version v1.0

Features

  • Supports both synchronous and asynchronous (microtask-based) event emission.
  • Allows multiple listeners per event.
  • Provides once event handling.
  • Enables removal of specific or all event listeners.
  • Designed for performance with a minimal footprint.

Installation

You can install CozyEvent via npm or yarn:

npm install cozyevent

or

yarn add cozyevent

Usage

Importing CozyEvent

ESModule

import { CozyEvent } from 'cozyevent';

CommonJS

const { CozyEvent } = require('cozyevent');

Creating an Event Emitter

const eventEmitter = new CozyEvent();

Registering Event Handlers

on(event: string, handler: EventHandler): void

Registers an event listener that triggers every time the event is emitted.

eventEmitter.on('message', (msg) => {
  console.log(`Received: ${msg}`);
});

once(event: string, handler: EventHandler): void

Registers an event listener that triggers only once.

eventEmitter.once('init', () => {
  console.log('Initialization complete!');
});

Emitting Events

emit(event: string, ...args: any[]): void

Emits a sync event.

eventEmitter.emit('message', 'Hello, World!');

emitAsync(event: string, ...args: any[]): void

Emits an async event emission using microtasks.

eventEmitter.emitAsync('data', { id: 1, name: 'John Doe' });

Removing Event Listeners

off(event: string, handler: EventHandler): void

Removes a specific event listener.

const handler = (msg) => console.log(msg);
eventEmitter.on('chat', handler);
eventEmitter.off('chat', handler);

removeAllListeners(event?: string): void

Removes all listeners for a specific event or all events if no event is specified.

eventEmitter.removeAllListeners('chat');

Extending CozyEvent

You can extend CozyEvent in your own class to create a custom event-driven system:

class SomeClass extends CozyEvent {
  doSomething() {
    console.log('Doing something...');
    this.emit('done', 'Task completed');
  }
}

const instance = new SomeClass();
instance.on('done', (message) => {
  console.log(`Received: ${message}`);
});

instance.doSomething();

Destroying the Event Emitter

destroy(): void

Removes all listeners and cleans up the event emitter.

eventEmitter.destroy();

License

Copyright (c) 2025 Mehmet Ergin Turk

Licensed under the MIT license. See the LICENSE file for details.

(Twitter/x: @papa_alpha_papa),

(Mastodon: @papa_alpha_papa)

(Bluesky: @erginturk.bsky.social)

Author

Developed with ❤️ by M.Ergin Turk

1.1.5

4 months ago

1.1.4

4 months ago

1.1.3

5 months ago

1.1.2

5 months ago

1.1.1

5 months ago

1.1.0

5 months ago

1.0.0

5 months ago

0.1.0

5 months ago