Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/neki-dev/make-event
🧩 Easy pattern to create events with subscriptions
https://github.com/neki-dev/make-event
emitter event events listener subscription
Last synced: about 1 month ago
JSON representation
🧩 Easy pattern to create events with subscriptions
- Host: GitHub
- URL: https://github.com/neki-dev/make-event
- Owner: neki-dev
- License: mit
- Created: 2024-11-23T23:27:48.000Z (about 1 month ago)
- Default Branch: main
- Last Pushed: 2024-11-24T10:21:36.000Z (about 1 month ago)
- Last Synced: 2024-11-24T10:24:36.459Z (about 1 month ago)
- Topics: emitter, event, events, listener, subscription
- Language: TypeScript
- Homepage:
- Size: 55.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## 🧩 Make event
[![Version](https://badgen.net/npm/v/make-event)](https://npmjs.com/package/make-event)
[![Size](https://img.badgesize.io/neki-dev/make-event/master/dist/index.js)](https://github.com/neki-dev/make-event/blob/master/dist/index.js)
[![Test](https://github.com/neki-dev/make-event/actions/workflows/test.yml/badge.svg)](https://github.com/neki-dev/make-event/actions/workflows/test.yml)
[![Build](https://github.com/neki-dev/make-event/actions/workflows/build.yml/badge.svg)](https://github.com/neki-dev/make-event/actions/workflows/build.yml)Easy pattern to create events with subscriptions
.
## Install
```sh
npm i make-event
```.
## Usage
### Subscribe to event
```ts
const onUpdate = Events.make();onUpdate((value) => {
console.log(value);
});
```### Unsubcsribe from event
```ts
const onUpdate = Events.make();// method 1
const unsubscribe = onUpdate(() => {
});unsubscribe();
// method 2
const handler = () => {
console.log(true);
}onUpdate(handler);
onUpdate.unsubscribe(handler);
```### Invoke event
```ts
const onUpdate = Events.make();onUpdate.invoke(10);
```### Remove all handlers
```ts
const onUpdate = Events.make();onUpdate.clear();
```.
## Example
```ts
import { Events } from 'make-event';class Player {
public readonly onJump = Events.make();public jump(height: number) {
this.onJump.invoke(height);
}
}// ...
const player = new Player();
player.onJump((height) => {
console.log('onJump1', height);
});const unsubscribe = player.onJump((height) => {
console.log('onJump2', height);
});unsubscribe();
player.jump(10);
```