@@ -15,9 +15,9 @@ checking when publishing and subscribing to events; or you must write very
1515brittle function overloads for _ every function_ : ` on ` , ` off ` , ` once ` , ` emit ` ,
1616etc. This is annoying and tedious.
1717
18- The aim of this is to leverage TypeScript's generic types to allow for compile-
19- time type checking. We also move the events into their own functions, so you
20- don't have to inherit or mixin any of our classes, just use these single Event
18+ The aim of this is to leverage TypeScript's generics to allow for compile-time
19+ type checking. We also move the events into their own functions, so you don't
20+ have to inherit or mixin any of our classes, just use these single Event
2121emitters. It is also un-opinionated, exposing functional and object oriented
2222building blocks in this library so you can use it best works in your project.
2323
@@ -65,36 +65,6 @@ event.on((str) => {
6565event .emit (' some string' ); // prints `hey we got the string: some string`
6666```
6767
68- ### Sealed Events
69-
70- The ` Event ` class has exposed the member function ` emit ` , which means any bit
71- of code that can your event to listen, can also act as an event emitter.
72-
73- Often you'll find you do not want to trust bits of code with that
74- responsibility. To that end this module exposes an alternative type of events
75- and API to generate Events called ` SealedEvents ` that cannot self emit, and
76- a separate emitter function
77-
78- ``` ts
79- import { createEmitter , SealedEvent } from ' ts-typed-events' ;
80-
81- const { event, emit } = createEmitter <string >();
82-
83- event instanceof SealedEvent ; // true
84-
85- event .on ((str ) => {
86- console .log (' Emitted string:' , str );
87- });
88-
89- emit (' emitter says hi' ); // prints: 'Emitted string: emitter says hi'
90-
91- ' emit' in event ; // === false
92- ```
93-
94- ** Note** : you can also use ` createEventEmitter ` if you wish the ` event ` type to
95- be ` instanceof Event ` . However bear in mind that event has access to the
96- emitter. This module exposes both for API uniformity.
97-
9868### async/await usage
9969
10070You can register event listeners via traditional callbacks, or if no callback is
@@ -141,6 +111,33 @@ console.log('were any callbacks invoked during the emit?', emitted);
141111// printed: `were any callbacks invoked during the emit? false`
142112```
143113
114+ ### Combining this within Classes
115+
116+ As this module intends to replace the "built-in" [ EventEmitter class] , you
117+ can use it much more freely because it is not a class you ** must** inherit like
118+ with the [ EventEmitter class] . To replicate similar functionality you can make
119+ each event a separate member variable of your.
120+
121+ ``` ts
122+ class Dog {
123+ private timesBarked = 0 ;
124+ // NOTE: the emitter is private here, and the event is public.
125+ // This allows the class to completely control when it emits.
126+ private emitBarked = createEmitter ();
127+
128+ public barked = this .emitBarked .event ;
129+
130+ public bark() {
131+ this .timesBarked += 1 ;
132+ this .emitBarked ();
133+ }
134+ }
135+
136+ const dog = new Dog ();
137+ dog .barked .on (() => console .log (' The dog barked!' ));
138+ dog .bark (); // prints: `The dog barked!`;
139+ ```
140+
144141### Alternative Syntax
145142
146143The method, ` createEmitter ` , returns the emitter function, with
@@ -161,33 +158,36 @@ emit(); // prints `did we get something?: undefined`
161158console .log (emit === emit .emit ); // print `true`
162159```
163160
164- ### Combining this within Classes
161+ #### Sealed Events
165162
166- As this module intends to replace the "built-in" [ EventEmitter class] , you
167- can use it much more freely because it is not a class you ** must** inherit like
168- with the [ EventEmitter class] . To replicate similar functionality you can make
169- each event a separate member variable of your.
163+ The ` Event ` class has exposed the member function ` emit ` , which means any bit
164+ of code that can your event to listen, can also act as an event emitter.
165+
166+ Often you'll find you do not want to trust bits of code with that
167+ responsibility. To that end this module exposes an alternative type of events
168+ and API to generate Events called ` SealedEvents ` that cannot self emit, and
169+ a separate emitter function
170170
171171``` ts
172- class Dog {
173- private timesBarked = 0 ;
174- // NOTE: the emitter is private here, and the event is public.
175- // This allows the class to completely control when it emits.
176- private emitBarked = createEmitter ();
172+ import { createEmitter , SealedEvent } from ' ts-typed-events' ;
177173
178- public barked = this . emitBarked . event ;
174+ const { event, emit } = createEmitter < BigInt >() ;
179175
180- public bark() {
181- this .timesBarked += 1 ;
182- this .emitBarked ();
183- }
184- }
176+ event instanceof SealedEvent ; // true
185177
186- const dog = new Dog ();
187- dog .barked .on (() => console .log (' The dog barked!' ));
188- dog .bark (); // prints: `The dog barked!`;
178+ event .on ((int ) => {
179+ console .log (' Emitted BigInt:' , int );
180+ });
181+
182+ emit (1337n ); // prints: 'Emitted BigInt: 1337'
183+
184+ ' emit' in event ; // === false
189185```
190186
187+ ** Note** : you can also use ` createEventEmitter ` if you wish the ` event ` type to
188+ be ` instanceof Event ` . However bear in mind that event has access to the
189+ emitter. This module exposes both for API uniformity.
190+
191191## Other Notes
192192
193193This library leverages TypeScript's interfaces and generics very heavily to do
0 commit comments