Skip to content

Commit 3fbf027

Browse files
committed
docs: mention .createEvent()
1 parent 50c0cd9 commit 3fbf027

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

README.md

+13
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,19 @@ Removes the event listener for the given event type.
177177

178178
Removes all event listeners for the given event type. If no event `type` is provided, removes all existing event listeners.
179179

180+
### `.createEvent(type[, data])`
181+
182+
Creates a strongly-typed `Event` instance for the given `type`. Optionally, accepts `data` if the event type describes one.
183+
184+
```ts
185+
const emitter = new Emitter<{ greet: [string]; ping: [never] }>()
186+
187+
const greetEvent = emitter.createEvent('greet', 'John')
188+
const pingEvent = emitter.createEvent('ping')
189+
```
190+
191+
> The `.createEvent()` method is primarily meant for creating event instances that are _going to be reused across different emitters_. That is handy if you want to implement `event.stopPropagation()` in your event flow since that requires a single event shared between multiple emitters.
192+
180193
## Types
181194

182195
Apart from being strongly-typed from the ground up, the library provides you with a few helper types to annotate your own implementations.

0 commit comments

Comments
 (0)