Code Monkey home page Code Monkey logo

ts-async-event-emitter's Introduction

Async-event-emitter

npm version github actions status code style: prettier

Heavily inspired by https://github.com/sindresorhus/emittery#typescript, without the legacy part

Usage

import { AsyncEventEmitter } from '@prismamedia/async-event-emitter';

// We use an "Enum" to name the events
enum EventKind {
  Pre = 'pre',
  Post = 'post',
}

// We "type" the data carried by the events like this :
const ee = new AsyncEventEmitter<{ [EventKind.Pre]: { at: number }; [EventKind.Post]: { took: number } }>();

// We can listen on events like this :
// "offFirstPre" is a convenient method to unregister the listener later, see below
const offFirstPre = ee.on(EventKind.Pre, ({ at }) => console.log({ first: at }));
ee.on(EventKind.Pre, ({ at }) => console.log({ second: at * 2 }));
ee.on(EventKind.Post, async ({ took }) => console.log({ took }));

// [...]

await ee.emit(EventKind.Pre, { at: 2000 });
// -> { any: 2000 }
// -> { first: 2000 }
// -> { second: 4000 }

await ee.emit(EventKind.Post, { took: 100 });
// -> { any: 100 }
// -> { took: 100 }

// Unregister the first listener
offFirstPre();

// Only the second listener remains
await ee.emit(EventKind.Pre, { at: 10000 });
// -> { any: 10000 }
// -> { second: 20000 }

Other convenient methods are available :

on "config"

// Subscribe to a bunch of events
ee.on({
  // Several listeners for this event
  [EventKind.Pre]: [
    () => console.log({ at }),
    () => console.log({ at: at * 2 }),
  ],
  // Only one here
  [EventKind.Post]: () => console.log({ took }),
});

Be aware that the on "config" method does not support "numeric" event kinds, as javascript transforms the "numeric" keys into "string"

once

// Subscribe to an event for only one execution
ee.once(EventKind.Pre, ({ at }) => console.log({ at }));

wait

// Wait for an event to be triggered
const eventData = await ee.wait(EventKind.Pre);

// Wait for an event to be triggered with a 100ms timeout (if the "timeout" is reached before the event has been triggered an Error will be thrown)
const eventData = await ee.wait(EventKind.Pre, 100);

ts-async-event-emitter's People

Contributors

yvann avatar

Watchers

 avatar James Cloos avatar

Forkers

omcg33

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.