@studimax/event
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

@studimax/event

npm Code: TypeScript Made By: StudiMax

A fully typed Node Event system.

Installation

npm install @studimax/event
# or
yarn add @studimax/event

Usage

Define an EventMap type that allows your IDE to autocomplete your event handlers and emitters.

import { EventEmitter } from '@studimax/event'
type MyEvents = {
    start: () => void;
    end: (at:Date) => void;
};
const emitter = new EventEmitter<MyEvents>();

emitter.on('start', () => {
    console.log('started');
});
emitter.on('end', (at:Date) => {
    console.log('ended at', at);
});

emitter.emit('start');
emitter.emit('end', new Date());

SubEventEmitter

SubEventEmitter can receive events from his parent, but parent can't listen to events from his children.

import { EventEmitter } from '@studimax/event';

type MyEvents = {
    start: () => void;
    end: (at:Date) => void;
};
const emitter = new EventEmitter<MyEvents>();

const subEmitter = emitter.subEmitter();

emitter.on('start', () => {
    console.log('parent started');
});

subEmitter.on('start', () => {
    console.log('child started');
});

emitter.emit('start'); // parent and child received the event
subEmitter.emit('start'); // only child started received the event

Additional methods

  • removeChildrenListener(event, listener): Remove listeners in children too of a specific event.
  • removeAllChildrenListeners(event?): Remove all listeners in children too of a specific event.
  • getChildren(): Get all children EventEmitter.
  • hasChild(child): Check if a child EventEmitter is in the children list.
  • link(child): Link a child EventEmitter.
  • unlink(child): Unlink a child EventEmitter or all children if no child is specified.
  • isLinked(): Check if a child EventEmitter is linked to his parent.
  • link(parent): Link to a parent EventEmitter.
  • unlink(): Unlink from a parent EventEmitter.

Package Sidebar

Install

npm i @studimax/event

Weekly Downloads

0

Version

1.0.1

License

MIT

Unpacked Size

31.4 kB

Total Files

13

Last publish

Collaborators

  • maxscharwath