Skip to content

typescript-package/event-emitter

Repository files navigation

@typescript-package/event-emitter

Gitter Discord Twitter

npm version

GitHub issues GitHub forks GitHub stars GitHub license

GitHub Sponsors Patreon Sponsors

A lightweight TypeScript package for event emitter.

Table of contents

Installation

npm install @typescript-package/event-emitter --save-peer

Api

import {
  // Abstract.
  EventEmitterBase,
  NamedEventEmitterBase,
  // Concrete.
  EventEmitter,
  NamedEventEmitter,
} from '@typescript-package/event-emitter';

Abstract

EventEmitterBase

The base abstraction class for an event emitter pattern with replaceable listeners adapter and asynchronous capabilities.

import { EventEmitter } from '@typescript-package/event-emitter';

EventEmitterBase

NamedEventEmitterBase

A base abstraction class that implements a named event emitter pattern with replaceable listeners adapter and asynchronous capabilities.

import { NamedEventEmitterBase } from '@typescript-package/event-emitter';

NamedEventEmitterBase

Concrete

EventEmitter

A concrete class for an event emitter pattern with replaceable listeners adapter and asynchronous capabilities.

import { EventEmitter } from '@typescript-package/event-emitter';

const eventEmitter = new EventEmitter({
  // adapter: ListenersSetAdapter,
  async: false
});

eventEmitter.on((msg) => console.log(`Received: ${msg}`));
eventEmitter.emit('Hello, World!');

EventEmitter

NamedEventEmitter

A concrete class that implements a named event emitter pattern with replaceable listeners adapter and asynchronous capabilities.

import { NamedEventEmitter } from '@typescript-package/event-emitter';

const eventEmitter = new NamedEventEmitter({async: false}, {
  'event1': [(msg: string) => {
    console.log(`Listener 1: ${msg}`);
  }],
  'event2': [(num: number) => {
    console.log(`Event 2 received number: ${num}`);
  }]
});

eventEmitter.clear('event1')
eventEmitter.on('event1', (msg: string) => {
  console.log(`Listener 2: ${msg}`);
});

eventEmitter.on('event2', (num: number) => {
  console.log(`Event 2 received number: ${num}`);
});

eventEmitter.emit('event1', 'Hello, World!');
eventEmitter.listeners('event1')?.forEach(listener => {
  listener('Hello, World!');
});

NamedEventEmitter

Type

EventListeners

A type representing a partial mapping of event names to their listener arrays.

import { EventListeners } from '@typescript-package/event-emitter';

EventListeners

ListenersFor

A type representing the listeners for a specific event in a named event emitter, utilizing a specified listeners adapter and supporting asynchronous capabilities.

import { ListenersFor } from '@typescript-package/event-emitter';

ListenersFor

Contributing

Your contributions are valued! If you'd like to contribute, please feel free to submit a pull request. Help is always appreciated.

Support

If you find this package useful and would like to support its and general development, you can contribute through one of the following payment methods. Your support helps maintain the packages and continue adding new.

Support via:

or via Trust Wallet

Thanks for your support!

Code of Conduct

By participating in this project, you agree to follow Code of Conduct.

GIT

Commit

Please follow the following commit message conventions:

Versioning

The package follows Semantic Versioning 2.0.0 for all releases. The versioning format is:

Given a version number MAJOR.MINOR.PATCH, increment the:

  • MAJOR version when you make incompatible API changes,
  • MINOR version when you add functionality in a backwards-compatible manner, and
  • PATCH version when you make backwards-compatible bug fixes.

Additional labels for pre-release and build metadata are available as extensions to the MAJOR.MINOR.PATCH format.

FAQ How should I deal with revisions in the 0.y.z initial development phase?

The simplest thing to do is start your initial development release at 0.1.0 and then increment the minor version for each subsequent release.

How do I know when to release 1.0.0?

If your software is being used in production, it should probably already be 1.0.0. If you have a stable API on which users have come to depend, you should be 1.0.0. If you’re worrying a lot about backwards compatibility, you should probably already be 1.0.0.

License

MIT © typescript-package (license)

Related packages