x/evt@v1.11.2
💧EventEmitter's typesafe replacement💧
Home - Documentation - v2.0 🚀
'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 browsers, react-native ...)
require
(CJS) or import
(ESM) as long as you install evt@beta
Can be imported in TypeScript projects using version >= 3.4 (Mar 2019) and in any plain JS projects.
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 ]
>();
//Mind the '$' prefixing 'attach'
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]);
in React, it let you attach event listeners without having to worry about detaching them.
import { useState } from "react";
import { Evt } from "evt";
import { useEvt } from "evt/hooks";
const evtTick = Evt.create();
setInterval(()=> evtTick.post(), 1000);
function App(){
const [count, setCount]= useState(0);
useEvt(ctx=> {
evtTick.attach(ctx, ()=> setCount(count+1));
},[count]);
return <h1>tick count: {count}</h1>;
}
*Those are introductory examples, EVT can do much more than this.
import { Evt } from "https://deno.land/x/evt/mod.ts";
$ npm install --save evt
# OR npm install --save evt@beta for the latest features
import { Evt } from "evt";
<script src="//unpkg.com/evt/bundle.min.js"></script>
<script>
const { Evt } = window["evt"];
</script>
There are a lot of things that can't easily be done with EventEmitter
:
Why would someone pick EVT over RxJS:
EventEmitter
.EVT is an attempt to address all these points while trying to remain as accessible as EventEmitter
.