dreamworkAPI
Ctrl K
GitHub
classDocs / Hook

Hook

classsharedmenusource
Object → Hook

A hook is a named, prioritized event pipeline that other code can attach handlers to and later call to run them. Handlers are grouped into four ordered stages - peek, provide, mixin, and observe - which run in that order each time the hook is called via Hook:call (or by calling the hook object itself, since it is callable).

Hook call pipeline, stages run in this order:

md
┌─────────┐
│  peek   │ --> Always called, return is optional;
└─────────┘     if any handler returns a value,
     ↓          it overrides the `provide` stage.
┌─────────┐
│ provide │ --> Called only if `peek` produced `nil`.
└─────────┘     First hook that returns a value wins,
     ↓          its return value becomes the result.
┌─────────┐
│  mixin  │ --> Called as long as a result exists.
└─────────┘     Lets handlers mutate the result,
     ↓          receiving both the old & new value.
┌─────────┐
│ observe │ --> Read-only listeners for the final
└─────────┘     result and args. Any return values
                will be ignored.

Constructor

Hook(name: string?): Hook

Fields

NameTypeDescription
namestringThe name of the hook.

Static functions

#Hook.cancel

function Hook.cancel(self: Hook<T>): boolean
Parameters
NameTypeDescription
selfHook<T>
Returns
TypeNameDescription
boolean

Methods

attachAttaches a handler function to the given stage of the hook.
awaitAsynchronously suspends the caller until the given stage of the hook is next called, then resumes with the arguments th…
callRuns the hook, calling its handlers in order across all four stages: peek (early handlers that may short-circuit the re…
clearCancels the hook if it is currently running, and removes every handler attached to every stage (peek, provide, observe,…
detachDetaches a previously attached handler from the given stage of the hook.
isRunningChecks if the hook is running currently.
onceAttaches a handler function to the hook that automatically detaches itself right before being invoked, so it only ever…

#Hook:attach

sharedmenusource
function Hook:attach(type: Hook.type, handler: Hook<T> | Mixin<T> | fun( ...: any ), priority: integer | nil)

Attaches a handler function to the given stage of the hook. If the handler is already attached to that stage, only its priority is updated (when a new one is supplied); it will not be attached twice.

If the hook is currently running, the change is deferred and applied once the hook finishes running.

Parameters
NameTypeDescription
typeHook.typeThe stage to attach the handler to.
handlerHook<T> | Mixin<T> | fun( ...: any )
priorityinteger | nilThe priority of the handler within its stage; handlers with a lower value run earlier. Defaults to 0.
Overloads
function Hook:attach(self: Hook<T>, type: "peek", handler: ( Hook<T> | Mixin<T> | fun( ...: any ): ( T | nil ) ), priority: ( integer | nil ))
function Hook:attach(self: Hook<T>, type: ( "provide" | nil ), handler: ( Hook<T> | Mixin<T> | fun( ...: any ): ( T | nil ) ), priority: ( integer | nil ))
function Hook:attach(self: Hook<T>, type: "mixin", handler: fun( old_value: ( T | nil ), new_value: T, ...: any ): ( T | nil ), priority: ( integer | nil ))
function Hook:attach(self: Hook<T>, type: "observe", handler: fun( value: T, ...: any ), priority: ( integer | nil ))

#Hook:await

sharedmenuasyncsource
async function Hook:await(type: Hook.type): any

Asynchronously suspends the caller until the given stage of the hook is next called, then resumes with the arguments that stage's handler received.

Parameters
NameTypeDescription
typeHook.typeThe stage to wait for.
Returns
TypeNameDescription
any...The arguments passed to the handler when the awaited stage fired.

#Hook:call

sharedmenusource
function Hook:call(...: any): T | nil

Runs the hook, calling its handlers in order across all four stages: peek (early handlers that may short-circuit the result), provide (produces the base result), mixin (allowed to transform the result), and observe (read-only listeners called with the final result).

Throws (and cancels the hook) if any handler errors, or if the hook is already running.

Parameters
NameTypeDescription
...anyArguments forwarded to every handler in every stage.
Returns
TypeNameDescription
T | nilresultThe final value produced by peek/provide and possibly transformed by mixin handlers; nil if no handler produced a value.

#Hook:clear

sharedmenusource
function Hook:clear()

Cancels the hook if it is currently running, and removes every handler attached to every stage (peek, provide, observe, mixin), resetting their priority tables as well.

#Hook:detach

sharedmenusource
function Hook:detach(type: Hook.type, handler: Hook<T> | Mixin<T> | fun( ...: any ): T): boolean

Detaches a previously attached handler from the given stage of the hook. If the hook is currently running, the handler is replaced with a no-op and the actual removal is deferred until the hook finishes running.

Parameters
NameTypeDescription
typeHook.typeThe stage to detach the handler from.
handlerHook<T> | Mixin<T> | fun( ...: any ): TThe exact handler reference that was previously passed to attach.
Returns
TypeNameDescription
booleanis_detachedReturns true if the handler was found and detached, otherwise false.

#Hook:isRunning

sharedmenunodiscardsource
function Hook:isRunning(): boolean

Checks if the hook is running currently.

Returns
TypeNameDescription
booleanis_runningReturns true if the hook is running currently, otherwise false.

#Hook:once

sharedmenusource
function Hook:once(type: Hook.type, handler: Hook<T> | fun( ...: any ), priority: integer | nil)

Attaches a handler function to the hook that automatically detaches itself right before being invoked, so it only ever runs once.

Parameters
NameTypeDescription
typeHook.typeThe stage to attach the handler to.
handlerHook<T> | fun( ...: any )
priorityinteger | nilThe priority of the handler within its stage; handlers with a lower value run earlier. Defaults to 0.
Overloads
function Hook:once(self: Hook<T>, type: ( "peek" | nil ), handler: ( Hook<T> | fun( ...: any ): ( T | nil ) ), priority: ( integer | nil ))
function Hook:once(self: Hook<T>, type: "provide", handler: ( Hook<T> | fun( ...: any ): T ), priority: ( integer | nil ))
function Hook:once(self: Hook<T>, type: "mixin", handler: ( Mixin<T> | fun( old_value: ( T | nil ), new_value: T, ...: any ): T ), priority: ( integer | nil ))
function Hook:once(self: Hook<T>, type: "observe", handler: fun( value: T, ...: any ), priority: ( integer | nil ))
Generated from the dreamwork meta branch.