https://github.com/parfeon/event-emitter
Easy to use strongly-typed event emitter.
https://github.com/parfeon/event-emitter
Last synced: 5 months ago
JSON representation
Easy to use strongly-typed event emitter.
- Host: GitHub
- URL: https://github.com/parfeon/event-emitter
- Owner: parfeon
- License: mit
- Created: 2022-11-22T00:30:32.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-05-14T08:53:19.000Z (about 3 years ago)
- Last Synced: 2025-08-08T21:26:45.852Z (11 months ago)
- Language: TypeScript
- Size: 110 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Event listener
[](https://github.com/parfeon/event-emitter/actions/workflows/run-tests.yml) [](https://www.npmjs.org/package/@randombytes/event-emitter) [](https://codecov.io/github/parfeon/event-emitter)
Small and easy to use event listener / emitter with strongly-typed functions.
## Install
#### NPM
```sh
npm install @randombytes/event-emitter
```
#### Yarn
```shell
yarn add @randombytes/event-emitter
```
## Import
```typescript
import { EventEmitter } from '@randombytes/event-emitter'
```
## Usage
```typescript
// Prepare events map for emitter.
import {EventEmitter} from "./emitter";
// Events map type which will be used by EventEmitter to assist
// with functions usage.
type TestEvents = {
'case-one': (title: string) => void
'case-two': (title: string, subtitle: string) => void
'case-three': (title: string, flag: boolean) => void
}
// Create event emitter instance.
const emitter = new EventEmitter()
// Add listener which will be called for each emitted target events.
const listenerToken = emitter.on('case-two', (title) => {
// Handle `case-two` event with passed title.
})
// Add listener which will be called only once for emitted
// target event.
emitter.once('case-three', (title, flags) => {
// Handle `case-three` event with passed title and flags.
})
// Emit events assisted by IDE for list of expected arguments.
emitter.emit('case-two', 'Event title')
emitter.emit('case-three', 'One time event title', true)
// Unregister listener when it will be required
listenerToken.invalidate()
// or remove all listeners at once.
emitter.removeAllListeners()
```