Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/filipchalupa/custom-listenable
Create custom listenable with addListener and removeListener methods.
https://github.com/filipchalupa/custom-listenable
Last synced: about 15 hours ago
JSON representation
Create custom listenable with addListener and removeListener methods.
- Host: GitHub
- URL: https://github.com/filipchalupa/custom-listenable
- Owner: FilipChalupa
- Created: 2023-05-02T13:29:39.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-05-09T12:31:22.000Z (over 1 year ago)
- Last Synced: 2024-11-02T17:07:55.489Z (5 days ago)
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/custom-listenable
- Size: 229 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Custom listenable [![npm](https://img.shields.io/npm/v/custom-listenable.svg)](https://www.npmjs.com/package/custom-listenable) ![npm type definitions](https://img.shields.io/npm/types/custom-listenable.svg)
Create custom listenable with `addListener` and `removeListener` methods.
## Installation
```bash
npm install custom-listenable
```## Usage
```js
import { listenable } from 'custom-listenable'// Create listenable
const myListenable = listenable()// Prepare callback for new data
const callback = (data) => {
console.log('Listener called')
console.log(data)
}// Listen to new data
myListenable.addListener(callback)// Stop listening after 5 seconds
setTimeout(() => {
myListenable.removeListener(callback)
}, 5000)// Emit data every second
setInterval(() => {
const data = {
random: Math.random(),
date: new Date(),
}
myListenable.emit(data)
}, 1000)// Listen once
myListenable.addListener(
(data) => {
console.log('Listener called once')
console.log(data)
},
{ once: true },
)
```### TypeScript
You can specify type of data that will be emitted.
```ts
const myListenable = listenable<{ random: number; date: Date }>()
```