yavent
v0.1.4
Published
A flexible, lightweight event subscriber & emitter
Downloads
907
Readme
yavent
A lightweight & flexible event library.
This library makes it easy to create ad-hoc events. Just call either makeEvent
or makeEvents
to create a pair of subscriber and emitter functions. The subscriber function hooks callbacks up to the event, and the emitter function triggers those callbacks. Here is an example (using Typescript):
import { Event, makeEvent } from 'yavent'
// Create a subscriber & emitter pair:
const [onMessage, emitMessage]: Event<string> = makeEvent()
// Subscribe:
const unsubscribe = onMessage(message => {
console.log(message)
})
// Emit:
emitMessage('Hello, world!')
// Unsubscribe:
unsubscribe()
The subscriber and emitter returned from makeEvent
only manage a single event. If you would like to use multiple named events with the same subscriber and emitter pair, just use makeEvents
instead:
import { Events, makeEvents } from 'yavent'
// Gives the payload type for each event:
interface MyEvents {
message: string
cancel: undefined
}
// Create a subscriber & emitter pair for our events:
const [on, emit]: Events<MyEvents> = makeEvents()
// Subscribe:
const unsubscribes = [
on('message', message => console.log(message)),
on('cancel', () => console.log('bye!'))
]
// Emit some events:
emit('message', 'Hello, world!')
emit('cancel', undefined)
// Unsubscribe:
unsubscribes.forEach(f => f())
That is the entire library.
- Zero external dependencies
- 100% test coverage
- Built-in definitions for Typescript & Flow
- 0.3K minified + gzip
Why yet another event library?
Minimalism isn't about doing less; it's about doing a few things really, really well. Yavent is the simplest, highest-quality event library I know how to write.