https://github.com/leawind/delegate-ts
A TypeScript library for managing a list of listeners and broadcasting events to them with priority support.
https://github.com/leawind/delegate-ts
deno typescript typescript-library
Last synced: about 2 months ago
JSON representation
A TypeScript library for managing a list of listeners and broadcasting events to them with priority support.
- Host: GitHub
- URL: https://github.com/leawind/delegate-ts
- Owner: Leawind
- License: gpl-3.0
- Created: 2025-03-23T02:32:38.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-03-24T15:54:56.000Z (over 1 year ago)
- Last Synced: 2025-03-24T16:39:31.879Z (over 1 year ago)
- Topics: deno, typescript, typescript-library
- Language: TypeScript
- Homepage: https://jsr.io/@leawind/delegate
- Size: 19.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# `@leawind/delegate`
[](https://github.com/Leawind/delegate-ts)
[](https://jsr.io/@leawind/delegate)
[](https://jsr.io/@leawind/delegate/doc)
[](https://github.com/Leawind/delegate-ts/actions/workflows/deno-test.yaml)
`Delegate` handles event listeners with features such as priority-based execution, one-time listeners, listener keys, and event control mechanisms. Ideal for scenarios requiring fine-grained control over event propagation and handler lifecycle management.
## Features
- **Priority Control** Define execution order using priorities. Higher-priority listeners run first.
- **Listener Keys** Associate listeners with unique keys to override or manage them independently.
- **Event Object API**
- `event.data` Get the event data which being broadcasted.
- `event.stop()`: Stop the event from propagating to other listeners.
- `event.removeSelf()`: Remove the current listener from the delegate.
## Installation
| | |
| -------- | ------------------------------------ |
| **deno** | `deno add jsr:@leawind/delegate` |
| **npm** | `npx jsr add @leawind/delegate` |
| **yarn** | `yarn dlx jsr add @leawind/delegate` |
| **pnpm** | `pnpm dlx jsr add @leawind/delegate` |
| **bun** | `bunx jsr add @leawind/delegate` |
## Example
Here is a simple example of how to use `Delegate` in TypeScript:
```typescript
import { Delegate } from '@leawind/delegate';
// Create a new delegate
// The type of the event data is `string`
const delegate = new Delegate('Example');
let str = '';
delegate.addListener((event) => {
str += event.data;
});
delegate.broadcast('Hello');
delegate.broadcast(', World!');
assertStrictEquals(str, 'Hello, World!');
```