A hook is a specific location in the code called the interception point identified as a string
. Plugins registered one or multiple hooks which names must match the targeted interceptions points. When the interception point is reached, all the registered hooks are executed.
The hooks
property of a plugin is an object whose keys are the hook name and the value are the hook object definitions. As such, plugins can be seen as a collection of related hooks. They are defined as object literals. You can register one or multiple hooks by providing a hook object or an array of those.
Defining the hook as a function is also accepted. It is converted to an object with the handler property associated to the function.
The handler
property is the function where the plugin author can listen, intercept and modify the application code. Promises are supported. Handlers can be synchronous and asynchronous JavaScript functions.
The after
and before
properties are used to sort the order in which the hook handlers are called. They must refers to a valid plugins name which must have a hook of the same name registered.
A hook is an object literal with the properties:
after
([string])
List of plugin names with hooks of the same name are to be executed before, a string is coerced to an array.handler
function User defined function used to listen, intercept and modify the application code.name
(string)
Name to indentify the hook.before
([string])
List of plugin names with hooks of the same name are to be executed after, a string is coerced to an array.
Note, when referencing plugins with after
and before
, the plugins do not need to exists. If they do, they are required to export a hook of the same name.
When the handler defines 1 argument, it can only access and modify the interception point arguments.
When the handler defines 2 arguments, it get access to the original handler or the handler returned by the previous hook. It can then decide to return it, execute it or even return an alternative handler. It can also prevent other hooks from being executed by returning null
.