Wiki

Clone wiki

PureWork / How hooks are working

PureWork's module system is based on the concept of "hooks". A hook is a PureBasic "runtime procedure" inside a module that was registered as module to the PureWork framework. Hooks allow modules to interact with the PureWork core, with custom modules and modules of the PureWork library.

To extend PureWork or a PureWork module, a module need simply implement a hook. Whenever PureWork or a module wishes to allow intervention from other modules, it determines which modules implement a hook and calls that hook in all included and registered modules that implement it.

In example, the Configuration core module allows to alter configuration values in the module method "setConfig". This method calls the PureWork::invokeHook for "onConfigurationChanged" hooks before saving the value. This allows other modules which have implemented "onConfigurationChanged" to alter the config value before it has been saved.

Example of invoking hooks:

#!purebasic
 ...

  ; /**
  ;  * Sets a configuration value. 
  ;  * 
  ;  * @param   string key          The key of the config value.
  ;  * @param   string value        The config value.
  ;  */
  Procedure setConfig(key.s, value.s)

    Protected.tHookConfigurationChanged argument

    argument\key    = key
    argument\value  = value

    PureWork::invokeHook("onConfigurationChanged", argument) <--- invoking the hook.

    configurations(key) = argument\value

    ProcedureReturn
  EndProcedure
EndModule

Example of hook onConfigurationChanged implementation:

#!purebasic

  ...

  Runtime Procedure onConfigurationChanged(*argument.tHookConfigurationChanged)

    ; Here I can change or notice config changes in my custom modules.

  EndProcedure
EndModule

Hooks

Updated