eventemitter-decorator-mixin
TypeScript icon, indicating that this package has built-in type declarations

0.0.3 • Public • Published

eventemitter-decorator-mixin

Provides an @event method decorator for EventEmitters

import { Server, Socket } from 'net';
import { EventEmitterDecoratorMixin, event } from 'eventemitter-decorator-mixin';

@EventEmitterDecoratorMixin()
class MyServer extends Server {
  @event('connection')
  private _onConnection(connection: Socket) {
    // ...
  }
}

Is equivalent to:

import { Server, Socket } from 'net';

class MyServer extends Server {
  constructor(...args: any[]) {
    super(...args);
    this.on('connection', this._onConnection.bind(this));
  }

  private _onConnection(connection: Socket) {
    // ...
  }
}

Features

  • Works on any subclass of an EventEmitter
  • Is compatible with custom EventEmitter libraries as well as long as they provide an emit function with this signature:
public emit(event: string | symbol, ...args: any[]): boolean;

It is important to note that listeners attached using a decorator currently cannot be detached. A pull request enabling support for this feature would be appreciated.

License

This software is licensed under the GNU Lesser General Public License v3.0. Please see the LICENSE file in the root of the project for the license text.

Readme

Keywords

Package Sidebar

Install

npm i eventemitter-decorator-mixin

Weekly Downloads

9

Version

0.0.3

License

LGPL-3.0

Unpacked Size

17.6 kB

Total Files

12

Last publish

Collaborators

  • campbellbrendene