ts-typed-events1.1.1 • Public • Published
Strongly typed event emitters for TypeScript.
This is a TypeScript project, although the compiled source is available in the npm package, it is solely designed around the purpose of making events easier to work with in TypeScript.
Using Node's EventEmitter class is nice; it's a useful software engineering paradigm. However in TypeScript you loose out on TypeScript's robust type checking when publishing and subscribing to events.
The aim of this is to leverage TypeScript's generic types to allow for compile- time type checking. We also move the events into their own class, so you don't have to inherit or mixin any of our classes, just use these single Event emitters.
;event.on;event.emit"some string"; // prints `hey we got the string: some string`
Events without types (signals)
;// emit the event in 1 secondsetTimeout, 1000;;console.log"1 sec later we got", emitted;
Note: async only works with
on as that can trigger multiple
Class with multiple events
;dog.events.called.onceconsole.log;dog.callIt"good boy"; // now the called event should trigger and pipe the string to console.logdog.events.didSomethingComplex.on;dog.doComplexTask; // the above callback is hit!dog.callIt"still a good boy"; // the first console.log callback is not fired, because it was only fired `once`
Class inheriting events
;pug.events.called.onceconsole.log;pug.callIt"good boy"; // this still works from the super's events!pug.events.snort.on;pug.makeSnort;
All the functions are documented clearly using jsdoc style comments, so your favorite IDE should pick up the documentation cleanly.
However, if you prefer external docs, they are available online here: https://jacobfischer.github.io/ts-typed-events/