Created
December 9, 2013 22:30
-
-
Save tobias/7882167 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
| (defn ^:internal ^:no-doc delayed | |
| "Creates an timeout-derefable delay around any function taking a timeout" | |
| [f] | |
| (let [val (atom ::unrealized) | |
| realized? #(not= ::unrealized @val) | |
| rcv (fn [timeout] (reset! val (f timeout)))] | |
| (proxy [clojure.lang.Delay clojure.lang.IBlockingDeref] [nil] | |
| (deref | |
| ([] | |
| (if (realized?) @val (rcv 0))) | |
| ([timeout-ms timeout-val] | |
| (if (realized?) | |
| @val | |
| (let [r (rcv timeout-ms)] | |
| (if (nil? r) timeout-val r))))) | |
| (isRealized [] | |
| (or (realized?) | |
| (not (nil? (rcv -1)))))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment