Hook
Bajo has a hook system that allows you to run custom code before or after certain events in the framework. You can use hooks to modify the behavior of the framework or to add new functionality.
Your hook should be put in:
- your plugin's hook folder (also valid for app's
mainplugin), which is located in{pluginRoot}/extend/bajo/hook. In this case:- the file should be named as
{hookName}.jsfor hook that listens for one name or{hookName1}${hookName2}${hookName3}.jsfor hook that listens for many names. It should be exported as default export - you can use the exact hook name or follow our convention to use kebab case for the file name, e.g.
bajo.override:after-read-config.jsforbajo.override:afterReadConfighook - content should be a single module:Hook.THook object. If
nameproperty is there, it will be used as the hook name, otherwise it will be reconstructed from the file name. Hence the file name is important - or simply a function that will be used as the hook handler. In this case, all missing properties will be set to their default values
- the file should be named as
- or as array of hook object in
{pluginRoot}/extend/bajo/hook.jsfile that follows the module:Hook.THook structure
Note: Hook handlers can have
.dot symbol in their name, but in documentation, the dot symbol will be replaced with·symbol because of JSDoc limitation. For example, a hook handler namedbajo.override:afterReadConfigwill be documented asbajo·override:afterReadConfig.
Warning: Even though hooks is a powerfull and convenient feature, it should be used with caution. Overusing hooks can lead to code that is difficult to understand and maintain. Use hooks only when necessary and avoid using them for simple tasks that can be accomplished with regular code.
- Source
Methods
(async, static) bajo:afterAllInit()
Hook handler that runs after all plugins are initialized. You can use this hook to do some post-initialization process.
- Source
- See
- module:Helper.run
(async, static) bajo:afterAllStart()
Hook handler that runs after all plugins are started. You can use this hook to do some post-start process.
- Source
- See
- module:Helper.run
(async, static) bajo:afterBoot()
Hook handler that runs after boot process. You can use this hook to do some post-boot process.
- Source
(async, static) bajo:afterCollectHooks(hooks)
Hook handler that runs after hooks are collected. You can use this hook to modify the collected hooks before they are recognized as application hooks.
| Name | Type | Description |
|---|---|---|
hooks | Array.<module:Hook~THook> | Array of hook objects |
- Source
(async, static) bajo:afterReadConfig(file, result, options)
Hook handler that runs after all read processes of a configuration file are completed.
| Name | Type | Description |
|---|---|---|
file | string | Config file path |
result | object | Resulting config object after parsing |
options | object | readConfig options |
- Source
(async, static) bajo:beforeAllInit()
Hook handler that runs before all plugins are initialized. You can use this hook to do some pre-initialization process.
- Source
- See
- module:Helper.run
(async, static) bajo:beforeAllStart()
Hook handler that runs before all plugins are started. You can use this hook to do some pre-start process.
- Source
- See
- module:Helper.run
(async, static) bajo:beforeBoot()
Hook handler that runs before boot process. You can use this hook to do some pre-boot process.
- Source
(async, static) bajo:beforeReadConfig(file, options)
Hook handler that runs before a configuration file is read.
| Name | Type | Description |
|---|---|---|
file | string | Config file path |
options | object | readConfig options |
- Source
(async, static) bajo·default:afterReadConfig(file, orgObj, options)
Hook handler that runs after a non override/extended configuration file is read.
| Name | Type | Description |
|---|---|---|
file | string | Config file path |
orgObj | string | Original config object before parsing |
options | object | readConfig options |
- Source
(async, static) bajo·override:afterReadConfig(fileExt, result, options)
Hook handler that runs after a configuration file override is read.
| Name | Type | Description |
|---|---|---|
fileExt | string | Config file extension |
result | object | Resulting config object after parsing |
options | object | readConfig options |
- Source
(async, static) bajo·override:beforeReadConfig(fileExt, options)
Hook handler that runs before a configuration file override is read.
| Name | Type | Description |
|---|---|---|
fileExt | string | Config file extension |
options | object | readConfig options |
- Source
(async, static) {ns}:afterAppletRun(…args)
Hook handler that runs after applet is run. {ns} is the applet's namespace
| Name | Type | Attributes | Description |
|---|---|---|---|
args | any | <repeatable> | Arguments passed to the applet |
- Source
(async, static) {ns}:afterBuildCollection(container, items)
Hook handler that runs after a collection is built. {ns} is the collection's namespace. This hook is useful to modify the collection items after they are built.
| Name | Type | Description |
|---|---|---|
container | string | Collection container name |
items | array.<object> | Collection items |
- Source
(async, static) {ns}:afterInit()
Hook handler that runs after {ns} plugins are initialized. You can use this hook to do some post-initialization process.
- Source
- See
- module:Helper.run
(async, static) {ns}:afterStart()
Hook handler that runs after {ns} plugins are started. You can use this hook to do some post-start process.
- Source
- See
- module:Helper.run
(async, static) {ns}:beforeAppletRun(…args)
Hook handler that runs before applet is run. {ns} is the applet's namespace
| Name | Type | Attributes | Description |
|---|---|---|---|
args | any | <repeatable> | Arguments passed to the applet |
- Source
(async, static) {ns}:beforeBuildCollection(container, items)
Hook handler that runs before a collection is built. {ns} is the collection's namespace. This hook is useful to modify the collection items before they are built.
| Name | Type | Description |
|---|---|---|
container | string | Collection container name |
items | array.<object> | Collection items |
- Source
(async, static) {ns}:beforeInit()
Hook handler that runs before {ns} plugins are initialized. You can use this hook to do some pre-initialization process.
- Source
- See
- module:Helper.run
(async, static) {ns}:beforeStart()
Hook handler that runs before {ns} plugins are started. You can use this hook to do some pre-start process.
- Source
- See
- module:Helper.run
Type Definitions
THook
Hook structure definition. Your hook listener should be an object that follows this structure.
- Object
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
name | string | | Hook name or array of hook names | ||
handler | module:Hook. | Hook handler function | ||
level | number | <optional> | 999 | Hook level (lower number means higher priority) |
src | string | Hook source (origin plugin name). Bajo will set this automatically, any value you set will be overriden. | ||
noWait | boolean | <optional> | false | If |
- Source
(async) hookHandler(…args) → {Promise.<void>}
This is the hook handler function that will be called when the hook is triggered. This handler is scoped to the owning plugin, so you can use this to access the plugin instance and its properties.
| Name | Type | Attributes | Description |
|---|---|---|---|
args | any | <repeatable> | Arguments passed to the hook handler |
- Source
The return value of the hook handler
- Type:
- Promise.<void>