Skip to content

Instantly share code, notes, and snippets.

@arohner
Created December 7, 2009 03:29
Show Gist options
  • Select an option

  • Save arohner/250575 to your computer and use it in GitHub Desktop.

Select an option

Save arohner/250575 to your computer and use it in GitHub Desktop.
(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