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 main plugin), which is located in {pluginRoot}/extend/bajo/hook. In this case:
    • the file should be named as {hookName}.js for hook that listens for one name or {hookName1}${hookName2}${hookName3}.js for 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.js for bajo.override:afterReadConfig hook
    • content should be a single module:Hook.THook object. If name property 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
  • or as array of hook object in {pluginRoot}/extend/bajo/hook.js file 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 named bajo.override:afterReadConfig will be documented as bajo·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.

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.

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.

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.

(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.

Parameters:
NameTypeDescription
hooksArray.<module:Hook~THook>

Array of hook objects

(async, static) bajo:afterReadConfig(file, result, options)

Hook handler that runs after all read processes of a configuration file are completed.

Parameters:
NameTypeDescription
filestring

Config file path

resultobject

Resulting config object after parsing

optionsobject

readConfig options

(async, static) bajo:beforeAllInit()

Hook handler that runs before all plugins are initialized. You can use this hook to do some pre-initialization process.

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.

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.

(async, static) bajo:beforeReadConfig(file, options)

Hook handler that runs before a configuration file is read.

Parameters:
NameTypeDescription
filestring

Config file path

optionsobject

readConfig options

(async, static) bajo·default:afterReadConfig(file, orgObj, options)

Hook handler that runs after a non override/extended configuration file is read.

Parameters:
NameTypeDescription
filestring

Config file path

orgObjstring

Original config object before parsing

optionsobject

readConfig options

(async, static) bajo·override:afterReadConfig(fileExt, result, options)

Hook handler that runs after a configuration file override is read.

Parameters:
NameTypeDescription
fileExtstring

Config file extension

resultobject

Resulting config object after parsing

optionsobject

readConfig options

(async, static) bajo·override:beforeReadConfig(fileExt, options)

Hook handler that runs before a configuration file override is read.

Parameters:
NameTypeDescription
fileExtstring

Config file extension

optionsobject

readConfig options

(async, static) {ns}:afterAppletRun(…args)

Hook handler that runs after applet is run. {ns} is the applet's namespace

Parameters:
NameTypeAttributesDescription
argsany<repeatable>

Arguments passed to the applet

(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.

Parameters:
NameTypeDescription
containerstring

Collection container name

itemsarray.<object>

Collection items

(async, static) {ns}:afterInit()

Hook handler that runs after {ns} plugins are initialized. You can use this hook to do some post-initialization process.

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.

See
  • module:Helper.run

(async, static) {ns}:beforeAppletRun(…args)

Hook handler that runs before applet is run. {ns} is the applet's namespace

Parameters:
NameTypeAttributesDescription
argsany<repeatable>

Arguments passed to the applet

(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.

Parameters:
NameTypeDescription
containerstring

Collection container name

itemsarray.<object>

Collection items

(async, static) {ns}:beforeInit()

Hook handler that runs before {ns} plugins are initialized. You can use this hook to do some pre-initialization process.

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.

See
  • module:Helper.run

Type Definitions

THook

Hook structure definition. Your hook listener should be an object that follows this structure.

Type:
  • Object
Properties
NameTypeAttributesDefaultDescription
namestring | Array.<string>

Hook name or array of hook names

handlermodule:Hook.hookHandler

Hook handler function

levelnumber<optional>
999

Hook level (lower number means higher priority)

srcstring

Hook source (origin plugin name). Bajo will set this automatically, any value you set will be overriden.

noWaitboolean<optional>
false

If true, Bajo will not wait for this hook to complete before proceeding to the next hook. Default is false.

(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.

Parameters:
NameTypeAttributesDescription
argsany<repeatable>

Arguments passed to the hook handler

Returns:

The return value of the hook handler

Type: 
Promise.<void>