xref: /freebsd/stand/lua/hook.lua (revision 3c4ba5f55438f7afd4f4b0b56f88f2bb505fd6a6)
1--
2-- SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3--
4-- Copyright (c) 2018 Kyle Evans <kevans@FreeBSD.org>
5--
6-- Redistribution and use in source and binary forms, with or without
7-- modification, are permitted provided that the following conditions
8-- are met:
9-- 1. Redistributions of source code must retain the above copyright
10--    notice, this list of conditions and the following disclaimer.
11-- 2. Redistributions in binary form must reproduce the above copyright
12--    notice, this list of conditions and the following disclaimer in the
13--    documentation and/or other materials provided with the distribution.
14--
15-- THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16-- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17-- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18-- ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19-- FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20-- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21-- OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22-- HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23-- LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24-- OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25-- SUCH DAMAGE.
26--
27-- $FreeBSD$
28--
29
30local hook = {}
31
32local registered_hooks = {}
33
34-- Module exports
35-- Register a hook type; these are the names that hooks may be registered for.
36-- It is expected that modules will register all of their hook types upon
37-- initial load. Other modules may then, at any time, register a hook for these
38-- types.
39--
40-- Naming convention: hook types should be sensible named, preferably prefixed
41-- with the name of the module that registered them. They would also ideally
42-- describe an action that may or may not match a function name.
43-- e.g. config.reloaded which takes place after config has been reloaded,
44-- possibly from a different source.
45function hook.registerType(hooktype)
46	registered_hooks[hooktype] = {}
47end
48
49function hook.register(hooktype, hookfunc)
50	local selected_hooks = registered_hooks[hooktype]
51	if selected_hooks == nil then
52		print("Tried registering a hook for an unknown hook type: " ..
53		    hooktype)
54		return
55	end
56	selected_hooks[#selected_hooks + 1] = hookfunc
57	registered_hooks[hooktype] = selected_hooks
58end
59
60-- Takes a hooktype and runs all functions associated with that specific hook
61-- type in the order that they were registered in. This ordering should likely
62-- not be relied upon.
63function hook.runAll(hooktype)
64	local selected_hooks = registered_hooks[hooktype]
65	if selected_hooks == nil then
66		-- This message, and the print() above, should only be seen by
67		-- developers. Hook type registration should have happened at
68		-- module load, so if this hasn't happened then we have messed
69		-- up the order in which we've loaded modules and we need to
70		-- catch that as soon as possible.
71		print("Tried to run hooks for an unknown hook type: " ..
72		    hooktype)
73		return 0
74	end
75	if #selected_hooks > 0 then
76		for _, func in ipairs(selected_hooks) do
77			func()
78		end
79	end
80	return #selected_hooks
81end
82
83return hook
84