Hook defines a generic concurrent safe structure for managing event hooks.

When using custom event it must embed the base [hook.Event].

Example:

  type CustomEvent struct {
hook.Event
SomeField int
}

h := Hook[*CustomEvent]{}

h.BindFunc(func(e *CustomEvent) error {
println(e.SomeField)

return e.Next()
})

h.Trigger(&CustomEvent{ SomeField: 123 })

Type Parameters

  • T

Hierarchy

  • Hook

Methods

  • Bind registers the provided handler to the current hooks queue.

    If handler.Id is empty it is updated with autogenerated value.

    If a handler from the current hook list has Id matching handler.Id then the old handler is replaced with the new one.

    Parameters

    Returns string

  • BindFunc is similar to Bind but registers a new handler from just the provided function.

    The registered handler is added with a default 0 priority and the id will be autogenerated.

    If you want to register a handler with custom priority or id use the [Hook.Bind] method.

    Parameters

    • fn: ((e) => void)
        • (e): void
        • Parameters

          • e: T

          Returns void

    Returns string

  • Length returns to total number of registered hook handlers.

    Returns number

  • Trigger executes all registered hook handlers one by one with the specified event as an argument.

    Optionally, this method allows also to register additional one off handler funcs that will be temporary appended to the handlers queue.

    NB! Each hook handler must call event.Next() in order the hook chain to proceed.

    Parameters

    • event: T
    • Rest ...oneOffHandlerFuncs: ((_arg0) => void)[]

    Returns void

  • Unbind removes one or many hook handler by their id.

    Parameters

    • Rest ...idsToRemove: string[]

    Returns void

  • UnbindAll removes all registered handlers.

    Returns void

Generated using TypeDoc