https://github.com/benallfree/callem
Callem is a Typescript-first micro event library.
https://github.com/benallfree/callem
Last synced: 8 months ago
JSON representation
Callem is a Typescript-first micro event library.
- Host: GitHub
- URL: https://github.com/benallfree/callem
- Owner: benallfree
- Created: 2021-02-06T10:23:32.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2021-02-18T18:43:25.000Z (over 4 years ago)
- Last Synced: 2025-02-17T17:04:16.072Z (8 months ago)
- Language: TypeScript
- Homepage:
- Size: 98.6 KB
- Stars: 3
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# Callem
Callem is a Typescript-first micro event library based on a convenient observer pattern often found in React and similar tools.
# Installation
```
npm i callem
```# Usage
Create an event
```typescript
import {callem} from 'callem'
const [onMyEvent, emitMyEvent] = callem<{id:number}>()const unsubscribe = onMyEvent( e => {
console.log(`Event is ${e.id}`)
})emitMyEvent({id: 42})
```# Advanced Usage
Suppose you want to await an event with a timeout. Let's see it in action.
```typescript
type MyEventData = {id:number}
const [onMyEvent, emitMyEvent] = callem()export const awaitEventWithTimeout = () => {
return new Promise((resolve, reject) => {
let tid: ReturnType
const unsub = onMyEvent((eventData) => {
clearTimeout(tid)
unsub()
resolve(eventData)
})
tid = setTimeout(() => {
unsub()
reject(new Error(`Timed out waiting for event data`))
}, 5000)
})
}const myEventData = await awaitEventWithTimeout()
```