https://github.com/serviejs/events
Tiny type-safe event emitter
https://github.com/serviejs/events
browser event-emitter eventemitter events tiny
Last synced: about 1 month ago
JSON representation
Tiny type-safe event emitter
- Host: GitHub
- URL: https://github.com/serviejs/events
- Owner: serviejs
- License: mit
- Created: 2019-06-12T05:10:10.000Z (over 6 years ago)
- Default Branch: main
- Last Pushed: 2023-12-12T07:23:09.000Z (almost 2 years ago)
- Last Synced: 2024-12-11T01:13:07.591Z (10 months ago)
- Topics: browser, event-emitter, eventemitter, events, tiny
- Language: TypeScript
- Size: 1.53 MB
- Stars: 26
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Security: SECURITY.md
Awesome Lists containing this project
README
# Events
[![NPM version][npm-image]][npm-url]
[![NPM downloads][downloads-image]][downloads-url]
[![Build status][build-image]][build-url]
[![Build coverage][coverage-image]][coverage-url]
[![Bundle size][bundlephobia-image]][bundlephobia-url]> Tiny type-safe event emitter.
## Installation
```
npm install @servie/events --save
```## Usage
```ts
import { Emitter, once, ALL_EVENTS } from "@servie/events";// Define an events interface using keys & argument tuples.
interface Events {
test: [boolean];
other: [string];
}// Initialize an `Emitter` using your `Events` interface.
const events = new Emitter();// Add or remove listeners.
const listener = (...args) => console.log(args);
const off = events.on("test", listener);// Emit an event.
events.emit("test", true);// Listen to _all_ events, i.e. for debugging.
const eachListener = ({ type, args }) => console.log(type, args);
const off = events.on(ALL_EVENTS, eachListener);// "Once" utility.
const runOnce = (...args) => console.log(args);
once(events, "test", runOnce);// Remove the listener.
off();
```## TypeScript
This project is written using [TypeScript](https://github.com/Microsoft/TypeScript) and publishes the definitions directly to NPM.
## License
MIT
[npm-image]: https://img.shields.io/npm/v/@servie/events
[npm-url]: https://npmjs.org/package/@servie/events
[downloads-image]: https://img.shields.io/npm/dm/@servie/events
[downloads-url]: https://npmjs.org/package/@servie/events
[build-image]: https://img.shields.io/github/workflow/status/serviejs/events/CI/main
[build-url]: https://github.com/serviejs/events/actions/workflows/ci.yml?query=branch%3Amain
[coverage-image]: https://img.shields.io/codecov/c/gh/serviejs/events
[coverage-url]: https://codecov.io/gh/serviejs/events
[bundlephobia-image]: https://img.shields.io/bundlephobia/minzip/@servie/events.svg
[bundlephobia-url]: https://bundlephobia.com/result?p=@servie/events