Giter VIP home page Giter VIP logo

event_emitter's Introduction

event_emitter Build Status

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.

event_emitter's People

Contributors

balou9 avatar dependabot[bot] avatar iosamuel avatar realjoshbyrnes avatar

Stargazers

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

Watchers

 avatar  avatar  avatar

event_emitter's Issues

Methods return EventEmitter type instead of this

There's an issue I overlooked when initially authoring this.
Currently, the return type is EventEmitter.

This works fine in most cases, but when extending EventEmitter, the type should be this, otherwise chained calls may fail in TypeScript.

A fix should be implemented shortly.

Possible enhancements

public removeListener(

  if (arr.indexOf(listener) !== -1) {
    arr.splice(arr.indexOf(listener), 1);
    this.emit("removeListener", eventName, listener);
    if (arr.length === 0) {
      this.events.delete(eventName);
    }
  }

indexOf(listener) is called twice

by checking (arr.length === 1) before we splice we can avoid arr.splice entirely if delete the thing anyway

Also not sure if it is possible with your implementation to remove a listener added with .once()

.listeners() is not implemented.

Currently .listeners(eventName) works as an alias for .rawListeners(eventName)

When used after calling .once() or.prependOnceListener, this may cause some issues.

Rename project to event_emitter

Now that the repository has been moved to the "denolibs" org, the name "deno_event_emitter" is redundant. "event_emitter" seems to make more sense.

Fix is on the way.

Other take on the matter

Hey,
Not really an issue but I would like to share with you: ts-evt.
For now, it is a node module but I was thinking about porting it to Deno.
It is very different from EventEmitter but much powerfull in many ways.
I think a new runtime is an opportunity for changing the rules.

I would love to have your feedback.
Feel free to delete the issue if you find it inappropriate.

Example could use some simplifying.

The example creates a subclass and then uses a chainable method. Neither of those idead are necessary to use the EventEmitter and obscure its elegance.

I suggest these changes, but I know this subjective so feel free to ignore all of this and close it if you like.

import EventEmitter from 'https://raw.github.com/ozjd/deno_event_emitter/master/mod.ts';

const emitter = new EventEmitter()

emitter.on('event', payload => {
  console.log('event', payload)
})

emitter.emit('event', 'hello world!')
emitter.emit('event', 'hello, again, world!')

And then also give an example of using it as a subclass:

class Collection extends EventEmitter {
  items: object[]

  public constructor() {
    super() 
  }

  public add(item:object) {
    this.items.push(item)
    this.emit('added', item)
  }
}

const c = new Collection()

c.on('added', item => console.log('added', item))

c.add('A')
c.add('B')

Module fails to compile when TypeScript's strict option is enabled

If you attempt to compile the example in the README with a tsconfing where compilerOptions.strict is true, the following compilation errors occur:

error TS7053: Element implicitly has an 'any' type because expression of type '"listener"' can't be used to index type 'Function'.
  Property 'listener' does not exist on type 'Function'.

► https://deno.land/x/event_emitter/lib/mod.ts:104:31

104       unwrappedListeners[i] = arr[i]["listener"] || arr[i];
                                  ~~~~~~~~~~~~~~~~~~

error TS2339: Property 'listener' does not exist on type '(...args: any[]) => void'.

► https://deno.land/x/event_emitter/lib/mod.ts:153:13

153     wrapped.listener = listener;
                ~~~~~~~~

error TS2345: Argument of type 'string | symbol | undefined' is not assignable to parameter of type 'string | symbol'.    Type 'undefined' is not assignable to type 'string | symbol'.

► https://deno.land/x/event_emitter/lib/mod.ts:178:25

178     if (this.events.has(eventName)) {
                            ~~~~~~~~~

error TS2345: Argument of type 'string | symbol | undefined' is not assignable to parameter of type 'string | symbol'.    Type 'undefined' is not assignable to type 'string | symbol'.

► https://deno.land/x/event_emitter/lib/mod.ts:179:42

179       const listeners = (this.events.get(eventName) as Function[]).slice(); // Create a copy; We use it AFTER it's deleted.
                                             ~~~~~~~~~

error TS2345: Argument of type 'string | symbol | undefined' is not assignable to parameter of type 'string | symbol'.    Type 'undefined' is not assignable to type 'string | symbol'.

► https://deno.land/x/event_emitter/lib/mod.ts:180:26

180       this.events.delete(eventName);
                             ~~~~~~~~~


Found 5 errors.

Relevancy?

I created this EventEmitter to be able to quickly port some of my NodeJS code to Deno, as well as having a simpler interface than what was available at the time.

Deno now has some node-compatible libraries such as https://deno.land/std/node/events.ts

This, coupled with the fact that I'm no longer using Deno (this may change in the future) leads me to question if it's worthwhile to keep this project? If anyone's interested in taking it over ( @Balou9 ? ) I'd be interested in hearing from you.

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.