make-event
TypeScript icon, indicating that this package has built-in type declarations

1.1.1 • Public • Published

🧩 Make event

Version Size Test Build

Easy pattern to create events with subscriptions

.

Install

npm i make-event

.

Usage

Subscribe to event

const onUpdate = Events.make<number>();

onUpdate((value) => {
  console.log(value);
});

Unsubcsribe from event

const onUpdate = Events.make();

// method 1

const unsubscribe = onUpdate(() => {
});

unsubscribe();

// method 2

const handler = () => {
  console.log(true);
}

onUpdate(handler);

onUpdate.unsubscribe(handler);

Invoke event

const onUpdate = Events.make<number>();

onUpdate.invoke(10);

Remove all handlers

const onUpdate = Events.make();

onUpdate.clear();

.

Example

import { Events } from 'make-event';

class Player {
  public readonly onJump = Events.make<humber>();

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

Package Sidebar

Install

npm i make-event

Weekly Downloads

52

Version

1.1.1

License

MIT

Unpacked Size

9.36 kB

Total Files

14

Last publish

Collaborators

  • neki-development