https://github.com/wvbe/simple-event
A simple event class that I keep writing. One instance represents one event, and you can call it with typed parameters.
https://github.com/wvbe/simple-event
Last synced: about 1 month ago
JSON representation
A simple event class that I keep writing. One instance represents one event, and you can call it with typed parameters.
- Host: GitHub
- URL: https://github.com/wvbe/simple-event
- Owner: wvbe
- Created: 2025-01-29T09:23:51.000Z (3 months ago)
- Default Branch: main
- Last Pushed: 2025-01-29T09:42:52.000Z (3 months ago)
- Last Synced: 2025-03-28T01:51:12.352Z (about 1 month ago)
- Language: TypeScript
- Size: 36.1 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# `@wvbe/simple-event`
A pure Javascript library for the world's simplest event. Works in the browser, NodeJS, Deno, etc.
Comes with types included.### Examples
The world's simplest example"
```ts
const e = new SimpleEvent();
void e.on(() => {
console.log('Something happened');
});
void e.emit();
```Cancel a specific listener:
```ts
const destroyer = e.on(() => {
console.log('Something happened');
});
destroyer();
```Cancel all listeners:
```ts
const destroyer = e.on(() => {
console.log('Something happened');
});
e.clear();
```Wait for all event handlers to finish:
```ts
void e.on(async () => {
await fetch(CMS_URL, { method: 'post' });
});
await e.emit();
```Type the parameters expected for emit(), and received by listener:
```ts
const e = new SimpleEvent<[UserObject, PermissionsObject]>();
void e.on((user, permissions) => {
// `user` and `permissions` argument are typed as
// `UserObject` and `PermissionsObject` respectively
console.log(user.name, permissions);
});
void e.emit(currentUser, defaultPermissions);
```