Skip to content

Hooks

@tsed/hooks

Usage

typescript
import { Hooks } from "@tsed/hooks";

See /packages/hooks/src/types/Hooks.ts.

Overview

ts
class Hooks {
    has(event: string): boolean;
    on(event: string, ref: HookRef, cb: HookListener): this;
    on(event: string, cb: HookListener): this;
    once(event: string, ref: HookRef, cb: HookListener): this;
    once(event: string, cb: HookListener): this;
    off(ref: HookRef): this;
    off(event: string, cb: HookListener): this;
    emit(event: string): void;
    emit(event: string, args: unknown[]): void;
    emit(event: string, ref: HookRef, args?: unknown[]): void;
    asyncEmit(event: string): Promise<void>;
    asyncEmit(event: string, args: unknown[]): Promise<void>;
    asyncEmit(event: string, ref: HookRef, args?: unknown[]): Promise<void>;
    alter<Arg = unknown, AlteredArg = Arg>(event: string, value: Arg, args?: unknown[], callThis?: unknown): AlteredArg;
    asyncAlter<Arg = unknown, AlteredArg = Arg>(event: string, value: Arg, args?: unknown[], callThis?: unknown): Promise<AlteredArg>;
    destroy(): void;
}

has

ts
has(event: string): boolean;

Check if an event has listeners

on

ts
on(event: string, ref: HookRef, cb: HookListener): this;
  • event (string): The event name

  • ref (HookRef): The reference of the listener

  • cb (HookListener): The callback

Listen a hook event

on

ts
on(event: string, cb: HookListener): this;
  • event (string): The event name

  • cb (HookListener): The callback

Listen a hook event

once

ts
once(event: string, ref: HookRef, cb: HookListener): this;
  • event (string): The event name

  • ref (HookRef): The reference of the listener

  • cb (HookListener): The callback

Listen a hook event once

once

ts
once(event: string, cb: HookListener): this;

Listen a hook event once

off

ts
off(ref: HookRef): this;

Remove a listener attached to an event

off

ts
off(event: string, cb: HookListener): this;

Remove a listener attached to an event

emit

ts
emit(event: string): void;
  • event (string): The event name

Trigger an event without arguments.

emit

ts
emit(event: string, args: unknown[]): void;

Trigger an event and call listener.

emit

ts
emit(event: string, ref: HookRef, args?: unknown[]): void;
  • event (string): The event name

  • ref (HookRef): The reference of the listener

  • args (unknown[]): Optional. The arguments

Trigger an event with arguments and call only listener attached to the given ref.

asyncEmit

ts
asyncEmit(event: string): Promise<void>;
  • event (string): The event name

Trigger an event and call async listener.

asyncEmit

ts
asyncEmit(event: string, args: unknown[]): Promise<void>;

asyncEmit

ts
asyncEmit(event: string, ref: HookRef, args?: unknown[]): Promise<void>;

alter

ts
alter<Arg = unknown, AlteredArg = Arg>(event: string, value: Arg, args?: unknown[], callThis?: unknown): AlteredArg;

Trigger an event, listener alter given value and return it.

asyncAlter

ts
asyncAlter<Arg = unknown, AlteredArg = Arg>(event: string, value: Arg, args?: unknown[], callThis?: unknown): Promise<AlteredArg>;

Trigger an event, async listener alter given value and return it.

destroy

ts
destroy(): void;

Released under the MIT License.