Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/keupoz/typed-event-dispatcher
Typed Event dispatcher for TypeScript
https://github.com/keupoz/typed-event-dispatcher
Last synced: about 1 month ago
JSON representation
Typed Event dispatcher for TypeScript
- Host: GitHub
- URL: https://github.com/keupoz/typed-event-dispatcher
- Owner: keupoz
- License: mit
- Created: 2022-02-19T01:56:29.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2022-02-19T02:14:48.000Z (over 2 years ago)
- Last Synced: 2024-10-07T23:17:28.683Z (about 1 month ago)
- Language: TypeScript
- Size: 10.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Typed EventDispatcher
Just a simple strictly typed event dispatcher
## Installation
```bash
npm install @keupoz/typed-event-dispatcher
```## Usage
```typescript
import { EventDispatcher } from "@keupoz/typed-event-dispatcher";// EventDispatcher accepts any type
// but it should be a record of data types associated with event names
export interface IFooEvents {
init: null;
bar: {
type: string;
data: any;
};
}export class Foo extends EventDispatcher {
constructor() {
super();this.dispatch("init", null);
}public bar(data: any): void {
this.dispatch("bar", {
type: "test data",
data: data,
});
}
}const foo = new Foo();
// data is null
foo.on("init", () => {
console.log("Foo is initialized");
});// data is { type: string; data: any; }
foo.on("bar", (data) => {
console.log(`Got data of type '${data.type}'`, data.data);
});foo.bar(["some", "random", "data"]);
// Remove all listeners
foo.dispose();
```