Gathering detailed insights and metrics for rettime
Gathering detailed insights and metrics for rettime
npm install rettime
Typescript
Module System
Node Version
NPM Version
Total Downloads
1,105
Last Day
1
Last Week
1
Last Month
7
Last Year
1,105
Minified
Minified + Gzipped
Latest Version
0.2.0
Package Id
rettime@0.2.0
Unpacked Size
35.37 kB
Size
6.75 kB
File Count
6
NPM Version
10.7.0
Node Version
18.20.4
Publised On
05 Aug 2024
Cumulative downloads
Total Downloads
Last day
0%
1
Compared to previous day
Last week
0%
1
Compared to previous week
Last month
16.7%
7
Compared to previous month
Last year
0%
1,105
Compared to previous year
4
Type-safe dependency-free EventTarget-inspired event emitter for browser and Node.js.
Emitter
can't do.Event
instances. A bit less verbosity than a common EventTarget
..emitAsPromise()
and .emitAsGenerator()
to build more complex event-driven systems.[!WARNING] This library does not have performance as the end goal. In fact, since it operates on events and supports event cancellation, it will likely be slower than other emitters out there.
EventTarget
?The EventTarget
API is fantastic. It works in the browser and in Node.js, dispatches actual events, supports cancellation, etc. At the same time, it has a number of flaws that prevent me from using it for anything serious:
type
in new Event(type)
is not a type argument in lib.dom.ts
. It's always string
. It means it's impossible to narrow it down to a literal string type to achieve type safety..prependListener()
. There is no way to add a listener to run first, before other existing listeners..removeAllListeners()
. You have to remove each individual listener by hand. Good if you own the listeners, not so good if you don't..listenerCount()
or knowing if a dispatched event had any listeners (the boolean
returned from .dispatch()
indicates if the event has been prevented, not whether it had any listeners)..on()
over .addEventListener()
. I prefer passing data than constructing new MessageEvent()
all the time.Emitter
(in Node.js)?The Emitter
API in Node.js is great as well. But...
Emitter
does not work in the browser.1npm install rettime
.on(type, listener)
Adds an event listener for the given event type.
1const emitter = new Emitter<{ hello: [string] }>() 2 3emitter.on('hello', 'John') // ✅ 4emitter.on('hello', 123) // ❌ number is not assignable to type string 5emitter.on('hello') // ❌ missing data argument of type string
.once(type, listener)
Adds a one-time event listener for the given event type.
.earlyOn(type, listener)
Prepends a listener for the given event type.
1const emitter = new Emitter<{ hello: [string, number] }>() 2 3emitter.on('hello', () => 1) 4emitter.earlyOn('hello', () => 2) 5 6const results = await emitter.emitAsPromise('hello') 7// [2, 1]
.earlyOnce(type, listener)
Prepends a one-time listener for the given event type.
.emit(type[, data])
Emits the given event with optional data.
1const emitter = new Emitter<{ hello: [string] }>() 2 3emitter.on('hello', (event) => console.log(event.data)) 4 5emitter.emit('hello', 'John')
.emitAsPromise(type[, data])
Emits the given event with optional data, and returns a Promise that resolves with the returned data of all matching event listeners, or rejects whenever any of the matching event listeners throws an error.
1const emitter = new Emitter<{ hello: [number, Promise<number>] }>() 2 3emitter.on('hello', async (event) => { 4 await sleep(100) 5 return event.data + 1 6}) 7emitter.on('hello', async (event) => event.data + 2) 8 9const values = await emitter.emitAsPromise('hello', 1) 10// [2, 3]
.emitAsGenerator(type[, data])
Emits the given event with optional data, and returns a generator function that exhausts all matching event listeners. Using a generator gives you granular control over what listeners are called.
1const emitter = new Emitter<{ hello: [string, number] }>() 2 3emitter.on('hello', () => 1) 4emitter.on('hello', () => 2) 5 6for (const listenerResult of emitter.emitAsGenerator('hello', 'John')) { 7 // Stop event emission if a listener returns a particular value. 8 if (listenerResult === 1) { 9 break 10 } 11}
.listeners([type])
Returns the list of all event listeners matching the given event type. If no event type
is provided, returns the list of all existing event listeners.
.listenerCount([type])
Returns the number of the event listeners matching the given event type. If no event type
is provided, returns the total number of existing listeners.
.removeListener(type, listener)
Removes the event listener for the given event type.
.removeAllListeners([type])
Removes all event listeners for the given event type. If no event type
is provided, removes all existing event listeners.
No vulnerabilities found.
No security vulnerabilities found.