Type Alias Dispatcher<T>

Dispatcher<T>: {
    addListener: ((callback: ListenerCallback<T>) => number);
    emit: T extends undefined
        ? (() => void)
        : ((opts: T) => void);
    removeAllListeners: (() => void);
    removeListener: ((id: number) => void);
}

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); // 2

dispatcher.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!'

Type Parameters

  • T = undefined

Type declaration

  • addListener: ((callback: ListenerCallback<T>) => number)

    Adds an event listener

      • (callback): number
      • Parameters

        Returns number

        const listenerId = dispatcher.addListener(() => { ... });
        
  • emit: T extends undefined
        ? (() => void)
        : ((opts: T) => void)

    Emits an event to all listeners with the provided options.

    Options to be passed to the listener

    void

    dispatcher.emit();
    
  • removeAllListeners: (() => void)

    Removes all event listeners from the dispatcher.

      • (): void
      • Returns void

        dispatcher.removeAllListeners();
        
  • removeListener: ((id: number) => void)

    Removes an event listener

      • (id): void
      • Parameters

        • id: number

        Returns void

        dispatcher.removeListener(listenerId);