Skip to content

An Event Emitter in TypeScript that supports custom events

Notifications You must be signed in to change notification settings

markeasting/EventEmitter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 

Repository files navigation

EventEmitter

An Event Emitter in TypeScript that supports custom events.

The custom event arguments are supported by intellisense :)

Usage

Instantiate the emitter

import { EventEmitter } from './EventEmitter';

const events = new EventEmitter();

Create and emit custom events

import { MyCustomEvent, SomeOtherEvent } from './MyCustomEvent';

events.emit(new MyCustomEvent({ type: 'click', someValue: 33 }));

events.emit(new SomeOtherEvent(42069));

Listen to custom events

/* Args are type hinted based on the given event :) */
events.on(MyCustomEvent, args => {
  console.log(args.type, args.someValue); // Outputs: 'click', 33
});

events.on(SomeOtherEvent, value => {
  console.log(value); // Outputs: 42069
});

once()

/* Respond to an event only once */
events.once(SomeOtherEvent, value => {
  console.log(value); // Outputs: 42069
});

About

An Event Emitter in TypeScript that supports custom events

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published