项目作者: fangedhex

项目描述 :
A small typescript library that provide some eventbus system that is """easier""" to use.
高级语言: TypeScript
项目地址: git://github.com/fangedhex/eventbus.git
创建时间: 2020-06-04T12:24:06Z
项目社区:https://github.com/fangedhex/eventbus

开源协议:MIT License

下载


EventBus

GitHub
@fangedhex/eventbus" alt="npm (scoped)">
GitHub Workflow Status
Libraries.io dependency status for GitHub repo
@fangedhex/eventbus" alt="npm bundle size (scoped)">
@fangedhex/eventbus" alt="npm">
@fangedhex/eventbus" alt="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 :

  1. {
  2. "compilerOptions": {
  3. "experimentalDecorators": true,
  4. "emitDecoratorMetadata": true
  5. }
  6. }

Reflect-metadata

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

  1. import "reflect-metadata";
  2. // YOUR CODE HERE

Example

  1. import { EventHandler, Eventbus, Listener } from "@fangedhex/eventbus";
  2. // 1 - Create your events
  3. class MyCustomEvent {
  4. constructor(public readonly value: string) {}
  5. }
  6. class MyCustomEvent2 {
  7. constructor(public readonly value: number) {}
  8. }
  9. // 2 - Create your listener class
  10. class MyListener {
  11. @EventHandler
  12. onSomething(ev: MyCustomEvent) {
  13. console.info("TEST : " + ev.value);
  14. }
  15. @EventHandler
  16. onSomething2(ev: MyCustomEvent2) {
  17. console.info("TEST2 : " + ev.value);
  18. }
  19. }
  20. // 3 - And how to use it
  21. const eventbus = new Eventbus();
  22. const listener = new MyListener();
  23. eventbus.registerListener(listener);
  24. eventbus.dispatch(new MyCustomEvent("myvalue"));
  25. eventbus.dispatch(new MyCustomEvent2(5.5));
  26. eventbus.dispatch(new MyCustomEvent2(5.8));

Contributing

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