-
Notifications
You must be signed in to change notification settings - Fork 128
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
Conversation
…e the ability to append. Also, make usre the hook.apply works when the hook is an array
@@ -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](...) |
There was a problem hiding this comment.
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...).
I have created a branch at github called PR696 which started as your patch. I have made some key changes.
Please test the PR696 branch to see if it works for you. Also the LMOD_RC is only for properties and will not work for hooks. |
The PR696 branch also has updated documentation. Please review files: docs/source/025_new.rst and docs/source/170_hooks.rst |
Thanks for polishing this and implementing the tests! I think they cover all scenarios well.
I've tested it (actually using the
Yep, message well received in the Lmod meeting :) I actually also ran into this when I created this PR: indeed I got into a scenario where the
Looks good to me.
You could consider to add the expected output for even more clarity:
Also, I'd replace
with the more straighforward
The way you handle the return values of the hooks also makes sense to me Finally,
makes total sense to me and I think this is the most intuitive behavior. I guess for those types of hooks, it doesn't really make sense to us Also, in the current |
I have updated the documentation based on your comments. In addition I have remarked on which hooks return something. Some of these hooks require a strong understanding of how Lmod uses these hook functions. |
Looks good to me! What's the process: we close this PR and you make a new PR from your feature branch? |
I think the question is when I close this pull request does the PR696 branch disappear. The answer is no. You can use and test this branch. Sometime in the near future the PR696 branch will be merged into the main branch. Does this answer your question or have I misunderstood your question. |
The PR696 branch has been merged on to the main branch and is now Lmod 8.7.36. Thanks for the issue and the PR's |
Note: there is an alternative approach in another PR. However, I consider the approach in this (#696) PR cleaner, because it is more explicit.
This PR stores hooks in
Hook.lua
internally as tables. It also adds the additional possibility of appending hooks to this table: a third (boolean) argument can be passed tohook.register
that defines if the function should be appended (true
) to the exist function, or whether it should overwrite it (false
). The default value if this argument is not specified isfalse
, to maintain backwards compatibility.It can be tested with this simple
SitePackage.lua
:I consider the fact that you have to be explicit (by passing the
append
boolean argument) a plus compared to the other PR, where the appending behaviour is implicit based on the datatype of thefunc
argument. I'm sure that implicit behaviour will surprise some people, whereas this explicit behaviour shouldn't surprise anyone. It also makes for slightly cleaner code internally, since we don't have to handle an extra potential datatype.Anyway, those are my 2 cents, curious to hear what you think.