Skip to content

Instantly share code, notes, and snippets.

@ohpauleez
Created October 26, 2012 22:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ohpauleez/3962043 to your computer and use it in GitHub Desktop.
Save ohpauleez/3962043 to your computer and use it in GitHub Desktop.
Blocking Deref Blog example
(defn blocking-deref
"Given an atom and a Result object, attempt to cycle the process tick on derefing until `pred-fn` isn't true
By default, `pred-fn` is nil?
Once the condition doesn't hold, the Result will be set to @a, and @a is returned"
([a r]
(blocking-deref a r nil?))
([a r pred-fn]
(if (pred-fn @a)
(node/process.nextTick #(blocking-deref a r pred-fn))
(do (.setValue r @a)
@a))))
;; This is the direct Result object you can use to access
;; a git config map
(def git-config-res
(let [res (goog.result.SimpleResult.)]
(->>
(git-config-atom "user.email")
(git-config-atom "user.name")
((fn [a] (blocking-deref a res #(< (count %) 2)))))
res))
(defn git-config-atom
"Call the `git config` command to grab the value of a given key.
Return an atom that will hold the return/output string in a map {k output-value}.
Optionally pass in an atom (allowing you to build up many returns)"
([k]
(git-config-atom k (atom {})))
([k ret]
(do (sys-exec (str "git config --get " (name k))
(fn [_ out _]
(swap! ret assoc (keyword k) (cstr/trim out))))
ret)))
(defn -main [& args]
(let [git-root core/git-root-res
git-conf core/git-config-res]
(result/wait git-root
#(binding [core/*repo-root* (.getValue git-root)]
(result/wait git-conf
(fn [] (main-control git-conf args)))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment