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: 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 (2 months ago)
- Default Branch: main
- Last Pushed: 2025-03-24T15:54:56.000Z (2 months ago)
- Last Synced: 2025-03-24T16:39:31.879Z (2 months 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)`@leawind/delegate` is a TypeScript library that manages a list of listeners and allows broadcasting events to them. Listeners can be added with a priority, and they will be called in order of priority. If a listener returns `false`, subsequent listeners will not be executed.
## 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` |## Usage
Here is an example of how to use Delegate in TypeScript:
```typescript
import { Delegate } from '@leawind/delegate';// Create a new delegate
const delegate = new Delegate();// Add listeners
delegate.addListener((event) => {
console.log(`Listener 1: ${event}`);
});
delegate.addListener((event) => {
console.log(`Listener 2: ${event.toUpperCase()}`);
});// Broadcast an event
delegate.broadcast('hello');
// Output:
// Listener 1: hello
// Listener 2: HELLO
```