-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
17 changed files
with
421 additions
and
49 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
0.0.29 | ||
0.0.30 |
This file contains 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 file contains 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 file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
;TODO move to async utils library (to avoid core.async and full.async deps) | ||
(ns io.jesi.backpack.async | ||
#?(:cljs (:require-macros | ||
[full.async] | ||
[full.async.env :refer [if-cljs]] | ||
[io.jesi.backpack.async :refer [when-open <? go-try]])) | ||
(:require | ||
[clojure.core.async :as async] | ||
[clojure.core.async.impl.protocols :as proto] | ||
[full.async :as fa] | ||
#?(:clj [full.async.env :refer [if-cljs]]))) | ||
|
||
(defn closed? | ||
"returns true if the channel is nil or closed" | ||
[chan] | ||
(or (nil? chan) | ||
(proto/closed? chan))) | ||
|
||
(def | ||
^{:arglists '([chan])} | ||
open? | ||
"returns true if the channel is open. The complement of `closed?`" | ||
(complement closed?)) | ||
|
||
(defmacro when-open [chan & body] | ||
`(when-not (closed? ~chan) | ||
~@body)) | ||
|
||
(def | ||
^{:arglists '([] [buf-or-n] [buf-or-n xform] [buf-or-n xform ex-handler])} | ||
chan | ||
"Creates a channel with an optional buffer, an optional transducer | ||
(like (map f), (filter p) etc or a composition thereof), and an | ||
optional exception-handler. If buf-or-n is a number, will create | ||
and use a fixed buffer of that size. If a transducer is supplied a | ||
buffer must be specified. ex-handler must be a fn of one argument - | ||
if an exception occurs during transformation it will be called with | ||
the Throwable as an argument, and any non-nil return value will be | ||
placed in the channel." | ||
async/chan) | ||
|
||
(defmacro >! | ||
"puts a val into port. nil values are ignored. Must be called inside a (go ...) block. Will park if no buffer space is available. | ||
Returns true unless port is already closed." | ||
[port val] | ||
`(when (some? ~val) | ||
(if-cljs | ||
(cljs.core.async/>! ~port ~val) | ||
(async/>! ~port ~val)))) | ||
|
||
(defmacro go-try | ||
"Asynchronously executes the body in a go block. Returns a channel which | ||
will receive the result of the body when completed or an exception if one | ||
is thrown." | ||
[& body] | ||
`(fa/go-try ~@body)) | ||
|
||
(defmacro go-retry | ||
"Attempts to evaluate a go block and retries it if `should-retry-fn` | ||
which is invoked with block's evaluation result evaluates to false. | ||
`should-retry-fn` is optional and by default it will simply check if | ||
result is of type Throwable (clj) / js/Error (cljs). If the evaluation | ||
still fails after given retries, the last failed result will be | ||
returned in channel. | ||
Parameters: | ||
* retries - how many times to retry (default 5 times) | ||
* delay - how long to wait in seconds between retries (default 1s) | ||
* should-retry-fn - function that is invoked with result of block's | ||
evaluation and should indicate whether to retry | ||
(if it returns true) or not (returns false)" | ||
[config & body] | ||
`(fa/go-retry ~config ~@body)) | ||
|
||
(defmacro <? | ||
"Same as core.async <! but throws an exception if the channel returns a | ||
throwable object. Also will not crash if channel is nil." | ||
[ch] | ||
`(fa/<? ~ch)) | ||
|
||
(defmacro go | ||
"Asynchronously executes the body, returning immediately to the | ||
calling thread. Additionally, any visible calls to <!, >! and alt!/alts! | ||
channel operations within the body will block (if necessary) by | ||
'parking' the calling thread rather than tying up an OS thread (or | ||
the only JS thread when in ClojureScript). Upon completion of the | ||
operation, the body will be resumed. | ||
Returns a channel which will receive the result of the body when | ||
completed" | ||
[& body] | ||
`(if-cljs | ||
(cljs.core.async.macros/go ~@body) | ||
(async/go ~@body))) | ||
|
||
(defn close! | ||
"Closes a channel. The channel will no longer accept any puts (they | ||
will be ignored). Data in the channel remains available for taking, until | ||
exhausted, after which takes will return nil. If there are any | ||
pending takes, they will be dispatched with nil. Closing a closed | ||
channel is a no-op. Returns the channel. | ||
Logically closing happens after all puts have been delivered. Therefore, any | ||
blocked or parked puts will remain blocked/parked until a taker releases them." | ||
[chan] | ||
(when chan | ||
(async/close! chan) | ||
chan)) | ||
|
||
(defn put! | ||
"Asynchronously puts a val into a channel if it's open. `nil` values are | ||
ignored. Returns the channel." | ||
[chan val] | ||
(when (and (open? chan) (some? val)) | ||
(async/put! chan val) | ||
chan)) |
This file contains 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 file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,3 +56,9 @@ | |
(if (pred v) | ||
(then v) | ||
(else v))))) | ||
|
||
(def | ||
^{:arglists '([& x])} | ||
p= | ||
"Partial =" | ||
#(apply partial = %&)) |
This file contains 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 file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
(ns io.jesi.backpack.test.macros | ||
#?(:cljs (:require-macros | ||
[full.async.env :refer [if-cljs]] | ||
[io.jesi.backpack.test.macros])) | ||
(:require | ||
[clojure.test :refer [is]] | ||
[io.jesi.backpack.async :as async] | ||
#?(:clj [full.async.env :refer [if-cljs]]) | ||
#?(:cljs [cljs.test]))) | ||
|
||
(defmacro async-go [& body] | ||
`(if-cljs | ||
(cljs.test/async ~'done | ||
(async/go | ||
(try | ||
~@body | ||
(finally | ||
(~'done))))) | ||
(clojure.core.async/<!! (async/go | ||
~@body)))) | ||
|
||
(defmacro is-nil<? [body] | ||
`(is (nil? (async/<? ~body)))) | ||
|
||
(defmacro is= [& body] | ||
`(is (= ~@body))) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
(ns io.jesi.backpack.test.reporter | ||
(:require | ||
[cljs-test-display.core :as ctd] | ||
[cljs.test :as ct] | ||
[io.jesi.backpack.async :as async])) | ||
|
||
(defonce done-chan (async/chan)) | ||
|
||
(def ^:private ^:const dispatch-val [::ctd/default :summary]) | ||
|
||
(defonce ^:private report-summary (get-method ct/report dispatch-val)) | ||
|
||
(defmethod ct/report dispatch-val [m] | ||
(async/put! done-chan true) | ||
(report-summary m)) |
This file contains 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
Oops, something went wrong.