Skip to content

A small typescript library that provide some eventbus system that is """easier""" to use.

License

Notifications You must be signed in to change notification settings

fangedhex/eventbus

Folders and files

NameName
Last commit message
Last commit date

Latest commit

20b30d0 · Jun 16, 2021

History

42 Commits
Jun 16, 2021
Sep 9, 2020
Sep 9, 2020
Sep 8, 2020
Aug 31, 2020
Aug 31, 2020
Jun 16, 2021
Jun 4, 2020
Jun 15, 2020
Aug 31, 2020
Aug 31, 2020
Sep 7, 2020
Jun 4, 2020
Nov 7, 2020
Jun 4, 2020
Jun 16, 2021
Jun 16, 2021
Jun 4, 2020
Aug 31, 2020

Repository files navigation

EventBus

GitHub npm (scoped) GitHub Workflow Status Libraries.io dependency status for GitHub repo npm bundle size (scoped) npm npm

Description

A small typescript library that provide some eventbus system that is """easier""" to use.

I made this library that I will use for my bot written in Typescript : I did a separate one so if you can use it if you find it useful :)

tsconfig.json

You need at least this 2 options :

{
  "compilerOptions": {
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true
  }
}

Reflect-metadata

You should import reflect-metadata at the top of your main file like this :

import "reflect-metadata";

// YOUR CODE HERE

Example

import { EventHandler, Eventbus, Listener } from "@fangedhex/eventbus";

// 1 - Create your events
class MyCustomEvent {
  constructor(public readonly value: string) {}
}

class MyCustomEvent2 {
  constructor(public readonly value: number) {}
}

// 2 - Create your listener class
class MyListener {
  @EventHandler
  onSomething(ev: MyCustomEvent) {
    console.info("TEST : " + ev.value);
  }

  @EventHandler
  onSomething2(ev: MyCustomEvent2) {
    console.info("TEST2 : " + ev.value);
  }
}

// 3 - And how to use it
const eventbus = new Eventbus();
const listener = new MyListener();
eventbus.registerListener(listener);

eventbus.dispatch(new MyCustomEvent("myvalue"));
eventbus.dispatch(new MyCustomEvent2(5.5));
eventbus.dispatch(new MyCustomEvent2(5.8));

Contributing

If you find any bugs or you want to give feedback feel free to do so :).