Created
December 7, 2009 03:29
-
-
Save arohner/250575 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (ns winston.hooks) | |
| (defn call-hooks [hooks args] | |
| (doseq [h @hooks] | |
| (apply (second h) args))) | |
| (defmacro defhook | |
| "defines an empty function, used to attach hooks to. All attached hooks must have the same signature" | |
| [name signature] | |
| `(let [hooks# (atom [])] | |
| (defn ~name {:hooks hooks#} [& args#] | |
| (call-hooks hooks# args#)))) | |
| (defn add-hook | |
| [hook-var name fn] | |
| "adds a hook to a function created with defhook. name is a symbol/keyword that can be used to refer to the fn, to remove it later using remove-hook" | |
| (let [hooks (:hooks (meta hook-var))] | |
| (swap! hooks conj [name fn]))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment