Coder Social home page Coder Social logo

evt's Introduction

💧A type safe replacement for Node's EventEmitter💧

Home - Documentation - Chat


'evt' is intended to be a replacement for 'events'.
It enables and encourages functional programming and makes heavy use of typescript's type inference features to provide type safety while keeping things concise and elegant 🍸.

Suitable for any JS runtime env (deno, node, old browser, react-native ...)

  • ✅ It is both a Deno and an NPM module.
  • ✅ Lightweight, no dependency.
  • ✅ No polyfills needed, the NPM module is transpile down to ES3

Can be imported in TypeScript projects using version >= 3.4 (Mar 2019) and in any plain JS projects.

Install / Import

In Deno:

import { Evt } from "https://deno.land/x/evt/mod.ts";

Anywhere else:

> npm install --save evt
import { Evt } from "evt"; 

TL;DR*

import { Evt } from "evt";

const evtText = new Evt<string>();
const evtTime = new Evt<number>();

evtText.attach(text => console.log(text));
evtTime.attachOnce(time => console.log(time));

evtText.post("hi!"); //Prints "hi!"
evtTime.post(123);   //Prints "123"
evtTime.post(1234);  //Prints nothing

OR

import { Evt, to } from "evt";

const evt = new Evt<
    [ "text",  string ] | 
    [ "time",  number ]
>();

evt.$attach(to("text"), text => console.log(text));
evt.$attachOnce(to("time"), time => console.log(time));

evt.post(["text", "hi!"]);
evt.post(["time", 123]);
evt.post(["time", 1234]);

*Those are introductory examples, EVT can do much more than this.

Try it

Run some examples

Motivation

There are a lot of things that can't easily be done with EventEmitter:

  • Enforcing type safety.
  • Removing a particular listener ( if the callback is an anonymous function ).
  • Adding a one-time listener for the next event that meets a condition.
  • Waiting (via a Promise) for one thing or another to happen.
    Example: waiting at most one second for the next message, stop waiting if the socket disconnects.

Concerning RxJS:

  • It introduces a lot of abstractions. It's a big jump from EventEmitter.
  • It is often needed to resort to custom type guards, the filter operator breaks the type inference.
  • Tend to be quite verbose.
  • It could be months before it eventually supports Deno.

EVT is an attempt to address all these points while trying to remain as accessible as EventEmitter.


Get started

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.