GithubHelp home page GithubHelp logo

tenry92 / typed-event-emitter Goto Github PK

View Code? Open in Web Editor NEW
18.0 1.0 3.0 99 KB

Alternative EventEmitter class for JavaScript and TypeScript.

TypeScript 100.00%
eventemitter emit-events listener javascript typescript

typed-event-emitter's Introduction

typed-event-emitter

This module provides an alternative API than the well known event emitting interfaces used in the browser (DOM) or node.js. Instead of accepting arbitrary strings as the event name, this module forces you to register your events in your class. Consequently, the style of binding and emitting events differs a little bit, ensuring already at binding time that the events actually exists.

Install

Via npm:

$ npm install typed-event-emitter

Usage

Take a look at the following snippet (TypeScript):

import { EventEmitter } from 'typed-event-emitter';

class MyClass extends EventEmitter {
  public readonly onValueChanged = this.registerEvent<[number]>();
  
  private _value: number;
  
  constructor(value: number) {
    // initialize EventEmitter
    super();
    
    this._value = value;
  }
  
  get value() {
    return this._value;
  }
  
  set value(value: number) {
    this._value = value;
    this.emit(this.onValueChanged, this._value);
  }
}

let instance = new MyClass(0);
instance.onValueChanged(newValue => {
  console.log(`Value changed: ${newValue}`);
});

instance.value = 27;

First, the EventEmitter is loaded from the module. Any class that shall emit events must extend that EventEmitter. If your class has its own constructor, make sure to call super().

Any events your class shall be able to emit must be registered in the form:

onFooBar = this.registerEvent<callbackArgTypes>();

Where onFooBar can be any name (it doesn't need to begin with on) and callbackArgTypes must be an array of the argument types the callback accepts. With this, you can see the signature your function must have when you're about to bind a listener to that event.

To fire/emit an event (only possible from within your event emitter), you have to call this.emit(this.onFooBar, ...), where this.onFooBar is the event to emit and ... any number of parameters that will be passed to the listeners.

JavaScript

The code shown above can also be written in JavaScript (node.js):

const EventEmitter = require('typed-event-emitter').EventEmitter;

class MyClass extends EventEmitter {
  constructor(value) {
    // initialize EventEmitter
    super();
    
    /* newValue: number */
    this.onValueChanged = this.registerEvent();
    
    this._value = value;
  }
  
  get value() {
    return this._value;
  }
  
  set value(value) {
    this._value = value;
    this.emit(this.onValueChanged, this._value);
  }
}
 
let instance = new MyClass(0);
instance.onValueChanged(newValue => {
  console.log(`Value changed: ${newValue}`);
});
 
instance.value = 27;

Note that the events are registered explicitly within the constructor. Make sure to initialize them after calling super().

Changelog

3.0.0 (2021-09-07)

  • BREAKING CHANGE (TypeScript): registerEvent<(arg: number) => any>() now is registerEvent<[number]>()
  • Add unit tests (run npm test)

2.0.0 (2019-08-06)

  • Make methods more type safe. This is a breaking change if used in a TypeScript project (rather than plain JavaScript), as this requires TypeScript 3.0+.

1.1.0 (2018-10-01)

  • Add support for ES5.

1.0.0 (2016-07-18)

  • Initial release.

License

typed-event-emitter is licensed under the MIT License.

typed-event-emitter's People

Contributors

d-fischer avatar tenry92 avatar

Stargazers

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

Watchers

 avatar

Forkers

d-fischer

typed-event-emitter's Issues

Unbinding listener during an event calling can be unsafe

Here a simple demonstration:

const { EventEmitter } = require("typed-event-emitter")

class Test extends EventEmitter {
  constructor() {
    super()
    this.onEvent = this.registerEvent()
  }
  
  emitEvent() {
    this.emit(this.onEvent)
  }
}

const test = new Test()

const listener1 = test.onEvent(function() {
  console.log("listener1")
  listener1.unbind()
})

const listener2 = test.onEvent(function() {
  console.log("listener2") // <-- this one won't be called
})

test.emitEvent()

Does anyone face this problem as well?

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.