--1-- SPDX-License-Identifier: BSD-2-Clause2--3-- Copyright (c) 2018 Kyle Evans <[email protected]>4--5-- Redistribution and use in source and binary forms, with or without6-- modification, are permitted provided that the following conditions7-- are met:8-- 1. Redistributions of source code must retain the above copyright9-- notice, this list of conditions and the following disclaimer.10-- 2. Redistributions in binary form must reproduce the above copyright11-- notice, this list of conditions and the following disclaimer in the12-- documentation and/or other materials provided with the distribution.13--14-- THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND15-- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE16-- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE17-- ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE18-- FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL19-- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS20-- OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)21-- HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT22-- LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY23-- OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF24-- SUCH DAMAGE.25--2627local hook = {}2829local registered_hooks = {}3031-- Module exports32-- Register a hook type; these are the names that hooks may be registered for.33-- It is expected that modules will register all of their hook types upon34-- initial load. Other modules may then, at any time, register a hook for these35-- types.36--37-- Naming convention: hook types should be sensible named, preferably prefixed38-- with the name of the module that registered them. They would also ideally39-- describe an action that may or may not match a function name.40-- e.g. config.reloaded which takes place after config has been reloaded,41-- possibly from a different source.42function hook.registerType(hooktype)43registered_hooks[hooktype] = {}44end4546function hook.register(hooktype, hookfunc)47local selected_hooks = registered_hooks[hooktype]48if selected_hooks == nil then49print("Tried registering a hook for an unknown hook type: " ..50hooktype)51return52end53selected_hooks[#selected_hooks + 1] = hookfunc54registered_hooks[hooktype] = selected_hooks55end5657-- Takes a hooktype and runs all functions associated with that specific hook58-- type in the order that they were registered in. This ordering should likely59-- not be relied upon.60function hook.runAll(hooktype, data)61local selected_hooks = registered_hooks[hooktype]62if selected_hooks == nil then63-- This message, and the print() above, should only be seen by64-- developers. Hook type registration should have happened at65-- module load, so if this hasn't happened then we have messed66-- up the order in which we've loaded modules and we need to67-- catch that as soon as possible.68print("Tried to run hooks for an unknown hook type: " ..69hooktype)70return 071end72if #selected_hooks > 0 then73for _, func in ipairs(selected_hooks) do74func(data)75end76end77return #selected_hooks78end7980return hook818283