Skip to content

prestons18/events

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@prestonarnold/events

A lightweight, async event bus for TypeScript/JavaScript applications.

Installation

npm install @prestonarnold/events

OR

yarn add @prestonarnold/events

Explicit API Example

import { createEventBus } from "@prestonarnold/events";

const bus = createEventBus<{
  "user:login": { userId: string };
  "user:logout": { userId: string };
}>();

const userModule = {
  async handleLogin({ userId }: { userId: string }) {
    console.log("[Explicit] User logged in:", userId);
  },
  logAll(event: string, payload: any) {
    console.log("[Explicit][ALL]", event, payload);
  }
};

bus.on("user:login", userModule.handleLogin.bind(userModule));
bus.onAll(userModule.logAll.bind(userModule));

(async () => {
  await bus.emit("user:login", { userId: "123" });
})();

Decorator API Example

import { createEventBus } from "@prestonarnold/events";
import { on, onAll } from "@prestonarnold/events/decorators";

const bus = createEventBus<{
  "user:login": { userId: string };
  "user:logout": { userId: string };
}>();

class UserModuleDecorated {
  @on(bus, "user:login")
  async handleLogin({ userId }: { userId: string }) {
    console.log("[Decorator] User logged in:", userId);
  }

  @onAll(bus)
  logAll(event: string, payload: any) {
    console.log("[Decorator][ALL]", event, payload);
  }
}

const _ = new UserModuleDecorated();

(async () => {
  await bus.emit("user:login", { userId: "456" });
})();

About

A lightweight, async event bus for TypeScript/JavaScript applications.

Resources

License

Stars

Watchers

Forks

Packages

No packages published