hooks icon indicating copy to clipboard operation
hooks copied to clipboard

Removing hooks?

Open cmtonkinson opened this issue 8 years ago • 5 comments

I don't see any (documented or not) method to remove a hook. Is this possible, even if not fully supported? How would I do this?

If I wanted to remove all Hooks by name, it looks like I might be able to run self._hooks[:hook_name] = nil but it's not clear that

  1. This is "safe"
  2. This wouldn't result in memory leak for a long-running process.

I know this is more a pub/sub workflow than hooking, strictly speaking, but it would be awfully helpful (to my current challenge). Lacking a proper solution or viable hack, I'll have to rip out this gem and replace it with a pub/sub alternative, which I don't look forward to.

cmtonkinson avatar Sep 08 '17 16:09 cmtonkinson

Hi Chris,

the _hooks object uses inheritable_attr which is "safe" to manipulate in subclasses. We do it all the time in many gems. What makes you think it could lead to memory leaks?

https://github.com/apotonick/hooks/blob/master/lib/hooks.rb#L25

apotonick avatar Sep 16 '17 10:09 apotonick

Thanks for pointing to the use of inheritable_attr (I'd missed that).

With respect to safety - I shouldn't have said "memory leak." Poor question asking on my part, I apologize. I should have instead asked whether you create any other references to the callable objects stored in _hooks, such that if I remove a hook and relinquish the last variable in my code which references to said callable, the runtime will be able to GC it.

cmtonkinson avatar Sep 18 '17 19:09 cmtonkinson

It's a valid, justified question! I had many memory leaks!..... with ActiveRecord haha! :grimacing:

If you change the _hooks "content" in B, its superclass A will still reference all the objects, so GC won't pick up anything here. Are you planning to change hooks dynamically at runtime? What will happen to the superclasses?

apotonick avatar Sep 18 '17 19:09 apotonick

In my use case, I'm wanting to dynamically add-and-remove instance hooks. Like I said, I may be pushing the design boundaries of the gem to the breaking point.

cmtonkinson avatar Sep 18 '17 19:09 cmtonkinson

Uhm, no, no. I haven't worked with this gem for a long time, but I can't see why this wouldn't work.

Have you considered using Trailblazer's Operations for your use case?

apotonick avatar Sep 18 '17 19:09 apotonick