A simple event framework used throughout Foundry Virtual Tabletop. When key actions or events occur, a "hook" is defined where user-defined callback functions can execute. This class manages the registration and execution of hooked callback functions.

Properties

Accessors

Methods

Properties

#ids: Map<number, HookedFunction> = ...

A mapping of hooked functions by their assigned ID

#id: number = 1

An incrementing counter for assigned hooked function IDs

Accessors

  • get events(): Record<string, HookedFunction[]>
  • A mapping of hook events which have functions registered to them.

    Returns Record<string, HookedFunction[]>

Methods

  • Register a callback handler which should be triggered when a hook is triggered.

    Parameters

    • hook: string

      The unique name of the hooked event

    • fn: Function

      The callback function which should be triggered when the hook event occurs

    • options: {
          once: boolean;
      } = {}

      Options which customize hook registration

      • once: boolean

        Only trigger the hooked function once

    Returns number

    An ID number of the hooked function which can be used to turn off the hook later

  • Register a callback handler for an event which is only triggered once the first time the event occurs. An alias for Hooks.on with {once: true}

    Parameters

    • hook: string

      The unique name of the hooked event

    • fn: Function

      The callback function which should be triggered when the hook event occurs

    Returns number

    An ID number of the hooked function which can be used to turn off the hook later

  • Unregister a callback handler for a particular hook event

    Parameters

    • hook: string

      The unique name of the hooked event

    • fn: number | Function

      The function, or ID number for the function, that should be turned off

    Returns void

  • Call all hook listeners in the order in which they were registered Hooks called this way can not be handled by returning false and will always trigger every hook callback.

    Parameters

    • hook: string

      The hook being triggered

    • Rest ...args: any[]

      Arguments passed to the hook callback functions

    Returns boolean

    Were all hooks called without execution being prevented?

  • Call hook listeners in the order in which they were registered. Continue calling hooks until either all have been called or one returns false.

    Hook listeners which return false denote that the original event has been adequately handled and no further hooks should be called.

    Parameters

    • hook: string

      The hook being triggered

    • Rest ...args: any[]

      Arguments passed to the hook callback functions

    Returns boolean

    Were all hooks called without execution being prevented?

  • Notify subscribers that an error has occurred within foundry.

    Parameters

    • location: string

      The method where the error was caught.

    • error: Error

      The error.

    • Optional options: {
          msg: string;
          log: string;
          notify: string;
          data: object;
      } = {}

      Additional options to configure behaviour.

      • msg: string

        A message which should prefix the resulting error or notification.

      • log: string

        The level at which to log the error to console (if at all).

      • notify: string

        The level at which to spawn a notification in the UI (if at all).

      • data: object

        Additional data to pass to the hook subscribers.

    Returns void

  • Private

    Call a hooked function using provided arguments and perhaps unregister it.

    Parameters

    • entry: HookedFunction

      The hooked function entry

    • args: any[]

      Arguments to be passed

    Returns any