Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

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

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