|
1 | 1 | (ns rx.lang.clojure.blocking
|
2 | 2 | "Blocking operators and functions. These should never be used in
|
3 | 3 | production code except at the end of an async chain to convert from
|
4 |
| - rx land back to sync land. For example, to produce a servlet response." |
5 |
| - (:refer-clojure :exclude [first into]) |
6 |
| - (:require [rx.lang.clojure.core :as rx]) |
| 4 | + rx land back to sync land. For example, to produce a servlet response. |
| 5 | +
|
| 6 | + If you use these, you're a bad person. |
| 7 | + " |
| 8 | + (:refer-clojure :exclude [first into doseq last]) |
| 9 | + (:require [rx.lang.clojure.interop :as iop] [rx.lang.clojure.core :as rx]) |
7 | 10 | (:import [rx Observable]
|
8 | 11 | [rx.observables BlockingObservable]))
|
9 | 12 |
|
10 | 13 | (def ^:private -ns- *ns*)
|
11 | 14 | (set! *warn-on-reflection* true)
|
12 | 15 |
|
| 16 | +(defmacro ^:private with-ex-unwrap |
| 17 | + [& body] |
| 18 | + `(try |
| 19 | + ~@body |
| 20 | + (catch RuntimeException e# |
| 21 | + (throw (or |
| 22 | + (and (identical? RuntimeException (class e#)) |
| 23 | + (.getCause e#)) |
| 24 | + e#))))) |
| 25 | + |
13 | 26 | (defn ^BlockingObservable ->blocking
|
14 |
| - "Convert an Observable to a BlockingObservable" |
15 |
| - [^Observable o] |
16 |
| - (.toBlockingObservable o)) |
| 27 | + "Convert an Observable to a BlockingObservable. |
| 28 | +
|
| 29 | + If o is already a BlockingObservable it's returned unchanged. |
| 30 | + " |
| 31 | + [o] |
| 32 | + (if (instance? BlockingObservable o) |
| 33 | + o |
| 34 | + (.toBlockingObservable ^Observable o))) |
| 35 | + |
| 36 | +(defn o->seq |
| 37 | + "Returns a lazy sequence of the items emitted by o |
| 38 | +
|
| 39 | + See: |
| 40 | + rx.observables.BlockingObservable/getIterator |
| 41 | + rx.lang.clojure.core/seq->o |
| 42 | + " |
| 43 | + [o] |
| 44 | + (-> (->blocking o) |
| 45 | + (.getIterator) |
| 46 | + (iterator-seq))) |
17 | 47 |
|
18 | 48 | (defn first
|
19 | 49 | "*Blocks* and waits for the first value emitted by the given observable.
|
20 | 50 |
|
| 51 | + If the Observable is empty, returns nil |
| 52 | +
|
21 | 53 | If an error is produced it is thrown.
|
22 | 54 |
|
23 | 55 | See:
|
24 | 56 | clojure.core/first
|
25 | 57 | rx/first
|
| 58 | + rx.observables.BlockingObservable/first |
| 59 | + " |
| 60 | + [observable] |
| 61 | + (with-ex-unwrap |
| 62 | + (.firstOrDefault (->blocking observable) nil))) |
| 63 | + |
| 64 | +(defn last |
| 65 | + "*Blocks* and waits for the last value emitted by the given observable. |
| 66 | +
|
| 67 | + If the Observable is empty, returns nil |
| 68 | +
|
| 69 | + If an error is produced it is thrown. |
| 70 | +
|
| 71 | + See: |
| 72 | + clojure.core/last |
| 73 | + rx/last |
| 74 | + rx.observable.BlockingObservable/last |
26 | 75 | "
|
27 | 76 | [observable]
|
28 |
| - (let [result (clojure.core/promise)] |
29 |
| - (rx/subscribe (->> observable (rx/take 1)) |
30 |
| - #(clojure.core/deliver result [:value %]) |
31 |
| - #(clojure.core/deliver result [:error %]) |
32 |
| - #(clojure.core/deliver result nil)) |
33 |
| - (if-let [[type v] @result] |
34 |
| - (case type |
35 |
| - :value v |
36 |
| - :error (throw v))))) |
| 77 | + (with-ex-unwrap |
| 78 | + (.lastOrDefault (->blocking observable) nil))) |
37 | 79 |
|
38 | 80 | (defn single
|
39 | 81 | "*Blocks* and waits for the first value emitted by the given observable.
|
40 | 82 |
|
41 | 83 | An error is thrown if more then one value is produced.
|
42 | 84 | "
|
43 | 85 | [observable]
|
44 |
| - (.single (->blocking observable))) |
| 86 | + (with-ex-unwrap |
| 87 | + (.single (->blocking observable)))) |
45 | 88 |
|
46 | 89 | (defn into
|
47 | 90 | "*Blocks* and pours the elements emitted by the given observables into
|
|
55 | 98 | "
|
56 | 99 | [to from-observable]
|
57 | 100 | (first (rx/into to from-observable)))
|
| 101 | + |
| 102 | +(defn doseq* |
| 103 | + "*Blocks* and executes (f x) for each x emitted by xs |
| 104 | +
|
| 105 | + Returns nil. |
| 106 | +
|
| 107 | + See: |
| 108 | + doseq |
| 109 | + clojure.core/doseq |
| 110 | + " |
| 111 | + [xs f] |
| 112 | + (with-ex-unwrap |
| 113 | + (-> (->blocking xs) |
| 114 | + (.forEach (rx.lang.clojure.interop/action* f))))) |
| 115 | + |
| 116 | +(defmacro doseq |
| 117 | + "Like clojure.core/doseq except iterates over an observable in a blocking manner. |
| 118 | +
|
| 119 | + Unlike clojure.core/doseq, only supports a single binding |
| 120 | +
|
| 121 | + Returns nil. |
| 122 | +
|
| 123 | + Example: |
| 124 | +
|
| 125 | + (rx-blocking/doseq [{:keys [name]} users-observable] |
| 126 | + (println \"User:\" name)) |
| 127 | +
|
| 128 | + See: |
| 129 | + doseq* |
| 130 | + clojure.core/doseq |
| 131 | + " |
| 132 | + [bindings & body] |
| 133 | + (when (not= (count bindings) 2) |
| 134 | + (throw (IllegalArgumentException. (str "sorry, rx/doseq only supports one binding")))) |
| 135 | + (let [[k v] bindings] |
| 136 | + `(doseq* ~v (fn [~k] ~@body)))) |
| 137 | + |
0 commit comments