Skip to content

A TypeScript library for managing a list of listeners and broadcasting events to them with priority support.

License

Notifications You must be signed in to change notification settings

Leawind/delegate-ts

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@leawind/delegate

GitHub License JSR Version deno score GitHub Actions Workflow Status

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:

import { Delegate } from '@leawind/delegate';

// Create a new delegate
// The type of the event data is `string`
const delegate = new Delegate<string>('Example');

let str = '';

delegate.addListener((event) => {
	str += event.data;
});

delegate.broadcast('Hello');
delegate.broadcast(', World!');

assertStrictEquals(str, 'Hello, World!');

About

A TypeScript library for managing a list of listeners and broadcasting events to them with priority support.

Topics

Resources

License

Stars

Watchers

Forks