An open API service indexing awesome lists of open source software.

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.

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()
```