Created
October 26, 2012 22:56
-
-
Save ohpauleez/3962043 to your computer and use it in GitHub Desktop.
Blocking Deref Blog example
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
(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 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
;; 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)) |
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
(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))) |
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
(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