Giter Club home page Giter Club logo

tsee's Introduction

NPM Version NPM Downloads

tsee

Typed EventEmitter implemented with typed args, based on nodejs EventEmitter.
Fully implements NodeJS.EventEmitter type, provides interface & proxy class.

Check 'tseep'! Same api but world fastest event emitter

Install & use

npm i tsee

Simple usage:

import { EventEmitter } from "tsee";

const events = new EventEmitter<{
    foo: (a: number, b: string) => void;
}>();

// foo's arguments is fully type checked
events.emit("foo", 123, "hello world");

Cast any other compatible to NodeJS.EventEmitter to typed:

import { asTypedEventEmitter } from "tsee";

const typedEmitter = asTypedEventEmitter<{
    foo: (a: number, b: string) => void;
    boo: (a: number, b: string) => void;
}>(myEmitter);

typedEmitter.emit("foo", 123, "hello world");

Advenced usage for non default event emitters

If you use custom EventEmitter implementation, you can simply cast it to tsee.IEventEmitter interface:

import { CustomEventEmitter } from 'my-event-emitter';
import * as tsee from 'tsee';

// Simple type case
const typed = new CustomEventEmitter() as any as tsee.IEventEmitter<{ ... }>;

// Functional cast with `NodeJS.EventEmitter` type constraints
const typed = asTypedEventEmitter<{ ... }>(new CustomEventEmitter());

tsee.EventEmitter class is implemented EventEmitter from 'events' package.

Api

EventEmitter<T> where T extends { [eventName]: Call signature }.

EventEmitter.emit's args is fully typed based on events map.

For foo event in example above, signature is: emit(eventName: 'foo', a: number, b: string).

tsee's People

Contributors

morglod avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

tsee's Issues

"once" subscriptions broke other "on" subscription

In this example

import { EventEmitter } from './index';

const events = new EventEmitter<{
    foo: (a: number, b: string) => void,
}>();

events.once('foo', (a,b) => {
    console.log('ones listener foo', a,b)
})

events.on('foo', (a, b) => {
    console.log('default listener foo', a, b);
});

events.emit('foo', 1, 'first time');
events.emit('foo', 2, 'second time');

i expect to see logs like

ones listener foo 1 first time
default listener foo 1 first time
default listener foo 2 second time

but i see only

ones listener foo 1 first time
default listener foo 2 second time

I think problem in this line https://github.com/Morglod/tsee/blob/master/src/ee.ts#L17

because this.events modifides during call e in line https://github.com/Morglod/tsee/blob/master/src/ee.ts#L30

adding typed events to an extended class that extends EventEmitter?

hi, i'm creating a class that extends the Stream class, which extends EventEmitter. does this module support adding typed events to such classes?

interface MyEvents {
  'foo': () => void;
}

// how do I fit tsee here so that `MyClass` has the `foo` event?
class MyClass extends Stream {
  constructor() {
  }
}

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.