Balou9 / event_emitter

A NodeJS-like EventEmitter for Deno in Typescript.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

event_emitter

A NodeJS-like EventEmitter for Deno written in 100% Typescript.

Usage

Basic

import EventEmitter from "https://deno.land/x/event_emitter/mod.ts";

const emitter = new EventEmitter();

emitter.on("SayHello", (to: string) => {
  if (!to) {
    console.log("hello!");
  } else {
    console.log("hello" + to + "!");
  }
});

emitter.emit("SayHello");
// hello!
emitter.emit("SayHello", " world");
// hello world!
emitter.emit("SayHello", ", again, world");
// hello, again, world!

Extend EventEmitter Class

import EventEmitter from 'https://deno.land/x/event_emitter/mod.ts';

class NewClass extends EventEmitter {
  public constructor() {
    super();
  }

  public createEvent(): NewClass {
    this.emit('event', 'The createEvent() method was called');
    return this; // Chainable
  }
}

const instance: NewClass = new NewClass();
instance.on('event', (message: string): void => {
  console.log(`Message received: ${message}`);
});
instance.createEvent();
// Message received: The createEvent() method was called

Documentation

API

Dependencies

None!

Permissions

None!

Differences (between this and NodeJS version)

  • No runtime type checks (Deno / TypeScript support compile time checks)
  • Error handling may require some more work.
  • Does not implement deprecated methods

Licensing

Licensed under the permissive MIT license. See LICENCE file for more information

This project is based on the open source DenoLibs / deno_template template.

About

A NodeJS-like EventEmitter for Deno in Typescript.

License:MIT License


Languages

Language:TypeScript 96.1%Language:JavaScript 3.9%