Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow registering multiple hooks of the same type - alternate approach #696

Merged
merged 5 commits into from
Mar 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions src/Hook.lua
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,19 @@ local validT =
-- Checks for a valid hook name and stores it if valid.
-- @param name The name of the hook.
-- @param func The function to store with it.
function M.register(name, func)
if (validT[name] ~= nil) then
validT[name] = func
function M.register(name, func, append)
if (validT[name] ~= nil) then
-- set default for append to be backwards compatible
append = append or false
-- if append and validT[name] was set before, append. Otherwise, overwrite.
if append and validT[name] then
validT[name][#validT[name]+1] = func
else
validT[name] = {func}
end
else
LmodWarning{msg="w_Unknown_Hook",name = tostring(name)}
end

end

--------------------------------------------------------------------------
Expand All @@ -97,7 +103,9 @@ end
-- @return the results of the hook if it exists.
function M.apply(name, ...)
if (validT[name]) then
return validT[name](...)
for i=1,#validT[name] do
validT[name][i](...)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NB, I don't return anything anymore, as it is not clear what should be returned. I considered a table of return values

      local returnT = {}
      for i=1,#validT[name] do
         local return_val = validT[name][i](...)
         if return_val ~= nil then
            table.insert(returnT, return_val)
         end
      end
      return returnT

but the awkward part is that tables cant contain nil values, so the returning table could have less elements than the original number of functions in the hook table. That makes it impossible to do reasonable handling of return code (which is pretty hard anyway since the hooks are arbitrary functions being executed, so you have no idea what different return values mean in any case...).

end
end
end

Expand Down
Loading