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