Attributes
Popular
Repository
Current version released
4 years ago
Dependencies
Versions
- v2.5.7Latest
- v2.5.6
- v2.5.5
- v2.5.3
- v2.5.2
- v2.5.1
- v2.5.0
- v2.4.23
- v2.4.22
- v2.4.21
- v2.4.21-rc.0
- v2.4.20
- v2.4.19
- v2.4.18
- v2.4.17
- v2.4.16
- v2.4.15
- v2.4.13
- v2.4.11
- v2.4.10
- v2.4.8
- v2.4.7
- v2.4.6
- v2.4.5
- v2.4.4
- v2.4.3
- v2.4.2
- v2.4.1
- v2.4.0
- v2.3.3
- v2.3.2
- v2.3.1
- v2.3.0
- v2.2.2
- v2.2.1
- v2.2.0
- v2.1.1
- v2.1.0
- v2.0.0
- v1.11.2
- v1.11.1
- v1.11.0
- v1.10.2
- v1.10.1
- v1.10.0
- v1.9.14
- v1.9.13
- v1.9.12
- v1.9.11
- v1.9.10
- v1.9.9
- v1.9.8
- v1.9.7
- v1.9.6
- v1.9.5
- v1.9.4
- v1.9.3
- v1.9.2
- v1.9.1
- v1.9.0
- v1.8.11
- v1.8.10
- v1.8.9
- v1.8.8
- v1.8.7
- v1.8.6
- v1.8.5
- v1.8.4
- v1.6.8
- v1.6.7
- v1.6.6
- v1.6.5
- v1.6.4
- 1.8.3
- 1.8.2
- 1.8.1
- 1.8.0
- 1.7.14
- 1.7.13
- 1.7.12
- 1.7.11
- 1.7.10
- 1.7.9
- 1.7.8
- 1.7.7
- 1.7.6
- 1.7.5
- 1.7.4
- 1.7.3
- 1.7.2
- 1.7.1
- 1.7.0
💧EventEmitter’s typesafe replacement💧
'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 …)
- ✅ It is both a Deno and an NPM module. ( Achieved with Denoify )
- ✅ Lightweight, no dependency.
- ✅ No polyfills needed, the NPM module is transpiled down to ES3
Can be imported in TypeScript projects using version >= 3.4 (Mar 2019) and in any plain JS projects.
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 ]
>();
//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]);
*Those are introductory examples, EVT can do much more than this.
Who is using it
Install / Import
In Deno:
import { Evt } from "https://deno.land/x/evt/mod.ts";
Anywhere else:
$ npm install --save evt
import { Evt } from "evt";
Import from HTML, with CDN
<script src="//unpkg.com/evt/bundle.min.js"></script>
<script>
const { Evt } = window["evt"];
</script>
Try it
Motivations
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
.