technomancy / robert-hooke

Hooke your Clojure functions!
Other
358 stars 27 forks source link

Add scopes for hooks #10

Closed hugoduncan closed 11 years ago

hugoduncan commented 11 years ago

Adds a hook-scope macro that provides a scope which records any change to hooks during the dynamic scope of its body, and restores hooks to their original state on exit of the scope.

This has only been lightly tested, as I wanted to get feedback as to whether this was a welcome addition or not. The motivation for this is to allow lein sub to apply plugins properly to sub-projects, without having their hooks spill over between projects.

joegallo commented 11 years ago

Doesn't the use of alter-var-root here mean that all hook-scopes are shared, and that two threads both using hook-scope simultaneously would clobber each other? (If not, then I need to read it much closer, maybe it's more subtle than my initial read of things revealed.)

hugoduncan commented 11 years ago

Threads can indeed interfere with each other, as they share any scope.

This is essentially the minimal change to allow scopes in line with the global nature of the current implementation. Supporting thread bindings would be a wider reaching change, and requiring a change in the implementation details of how hooks are added, removed and used, and the current semantics of hooks.

My driving use case for this was single threaded, but I hoped this issue would be brought up and discussed.

technomancy commented 11 years ago

Great idea; let's get some docs in for it and I'll cut a release. Thanks.