xref: /freebsd/stand/lua/hook.lua (revision 2e3f49888ec8851bafb22011533217487764fdb0)
1--
2-- SPDX-License-Identifier: BSD-2-Clause
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
28local hook = {}
29
30local registered_hooks = {}
31
32-- Module exports
33-- Register a hook type; these are the names that hooks may be registered for.
34-- It is expected that modules will register all of their hook types upon
35-- initial load. Other modules may then, at any time, register a hook for these
36-- types.
37--
38-- Naming convention: hook types should be sensible named, preferably prefixed
39-- with the name of the module that registered them. They would also ideally
40-- describe an action that may or may not match a function name.
41-- e.g. config.reloaded which takes place after config has been reloaded,
42-- possibly from a different source.
43function hook.registerType(hooktype)
44	registered_hooks[hooktype] = {}
45end
46
47function hook.register(hooktype, hookfunc)
48	local selected_hooks = registered_hooks[hooktype]
49	if selected_hooks == nil then
50		print("Tried registering a hook for an unknown hook type: " ..
51		    hooktype)
52		return
53	end
54	selected_hooks[#selected_hooks + 1] = hookfunc
55	registered_hooks[hooktype] = selected_hooks
56end
57
58-- Takes a hooktype and runs all functions associated with that specific hook
59-- type in the order that they were registered in. This ordering should likely
60-- not be relied upon.
61function hook.runAll(hooktype, data)
62	local selected_hooks = registered_hooks[hooktype]
63	if selected_hooks == nil then
64		-- This message, and the print() above, should only be seen by
65		-- developers. Hook type registration should have happened at
66		-- module load, so if this hasn't happened then we have messed
67		-- up the order in which we've loaded modules and we need to
68		-- catch that as soon as possible.
69		print("Tried to run hooks for an unknown hook type: " ..
70		    hooktype)
71		return 0
72	end
73	if #selected_hooks > 0 then
74		for _, func in ipairs(selected_hooks) do
75			func(data)
76		end
77	end
78	return #selected_hooks
79end
80
81return hook
82