Simplifies adding event listeners to your code.
import { newDispatcher } from "./src/blocks/dispatcher";const dispatcher = newDispatcher();let count = 0;dispatcher.addListener(() => { count += 2; });dispatcher.emit();console.log(count); // 2dispatcher.addListener(() => { count += 4; });dispatcher.emit();console.log(count); // 8 Copy
import { newDispatcher } from "./src/blocks/dispatcher";const dispatcher = newDispatcher();let count = 0;dispatcher.addListener(() => { count += 2; });dispatcher.emit();console.log(count); // 2dispatcher.addListener(() => { count += 4; });dispatcher.emit();console.log(count); // 8
import { newDispatcher } from "./src/blocks/dispatcher";const dispatcher = newDispatcher<string>();dispatcher.addListener((name) => { console.log(`Hello ${name}!`) });dispatcher.emit("John"); // 'Hello John!' Copy
import { newDispatcher } from "./src/blocks/dispatcher";const dispatcher = newDispatcher<string>();dispatcher.addListener((name) => { console.log(`Hello ${name}!`) });dispatcher.emit("John"); // 'Hello John!'
Adds an event listener
const listenerId = dispatcher.addListener(() => { ... }); Copy
const listenerId = dispatcher.addListener(() => { ... });
Emits an event to all listeners with the provided options.
Options to be passed to the listener
void
dispatcher.emit(); Copy
dispatcher.emit();
Removes all event listeners from the dispatcher.
dispatcher.removeAllListeners(); Copy
dispatcher.removeAllListeners();
Removes an event listener
dispatcher.removeListener(listenerId); Copy
dispatcher.removeListener(listenerId);
Simplifies adding event listeners to your code.
Examples
Typed Options