GithubHelp home page GithubHelp logo

denosaurs / event Goto Github PK

View Code? Open in Web Editor NEW
42.0 3.0 5.0 53 KB

๐Ÿ“† Strictly typed event emitter with asynciterator support

Home Page: https://deno.land/x/event

License: MIT License

TypeScript 100.00%
deno typed event-emitter asynciterator events hacktoberfest

event's Introduction

event

Tags CI Status Dependencies License

Strictly typed event emitter with asynciterator support.

Events should be defined as a literal object type where the key is the event name, and the value is a tuple with any amount of elements of any type.

The constructor takes an optional argument which defines the maximum amount of listeners per event, which defaults to 10. If this limit is surpassed, an error is thrown.


โš ๏ธ Events must be a type, and can't be an interface due to their design differences.


type Events = {
  foo: [string];
  bar: [number, boolean];
};

class MyClass extends EventEmitter<Events> {}
const MyClassInstance = new MyClass();

function listener(num, bool) {}

// add a listener to the bar event
MyClassInstance.on("bar", listener);

// remove a listener from the bar event
MyClassInstance.off("bar", listener);

// remove all listeners from the bar event
MyClassInstance.off("bar");

// remove all listeners from the event emitter
MyClassInstance.off();

// add a one-time listener to the bar event
MyClassInstance.once("bar", listener);

// on, once, and off are chainable
MyClassInstance.on("bar", listener).off("bar", listener);

// emit the bar event with the wanted data
MyClassInstance.emit("bar", 42, true);

// listen to all events with an async iterator
for await (const event of MyClassInstance) {
  if (event.name === "bar") {
    // event.value is of type [number, boolean]
  }
}

// listen to a specific event with an async iterator
for await (const [num, bool] of MyClassInstance.on("bar")) {
}

// removes all listeners and closes async iterators
MyClassInstance.close("bar");

Maintainers

Other

Contribution

Pull request, issues and feedback are very welcome. Code style is formatted with deno fmt and commit messages are done following Conventional Commits spec.

Licence

Copyright 2020-present, the denosaurs team. All rights reserved. MIT license.

event's People

Contributors

artiomtr avatar crowlkats avatar eliassjogreen 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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

event's Issues

@denosaurs/event in JSR

I'm porting my deno.land/x module to JSR. However, while @denosaurs/event name exists, nothing exists in the package.

When does it get pushed to JSR?

mapping for denoland/dnt

Hi, I'm trying to convert my first NodeJS project to deno.

EvenEmiter is a very common NodeJS feature, and I do not know how to convert event usage to NodeJS code with dnt.

Do you know how to do that?

dnt issue

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.