|
9 | 9 | (:require [rx.lang.clojure.interop :as iop]
|
10 | 10 | [rx.lang.clojure.graph :as graph]
|
11 | 11 | [rx.lang.clojure.realized :as realized])
|
12 |
| - (:import [rx Observable Observer Subscriber Subscription Observable$Operator Observable$OnSubscribe] |
| 12 | + (:import [rx |
| 13 | + Observable |
| 14 | + Observer Observable$Operator Observable$OnSubscribe |
| 15 | + Subscriber Subscription] |
13 | 16 | [rx.observables BlockingObservable]
|
14 | 17 | [rx.subscriptions Subscriptions]
|
15 | 18 | [rx.util.functions Action0 Action1 Func0 Func1 Func2]))
|
|
47 | 50 | [^Observer o e]
|
48 | 51 | (.onError o e))
|
49 | 52 |
|
50 |
| -(defn on-error-return |
51 |
| - [^Observable o f] |
52 |
| - (.onErrorReturn o f)) |
53 |
| - |
54 | 53 | ;################################################################################
|
55 | 54 |
|
56 | 55 | (defn ^Subscription subscribe
|
| 56 | + |
57 | 57 | ([^Observable o on-next-action]
|
58 |
| - (.subscribe o ^Action1 (iop/action* on-next-action))) |
| 58 | + (.subscribe o |
| 59 | + ^Action1 (iop/action* on-next-action))) |
| 60 | + |
59 | 61 | ([^Observable o on-next-action on-error-action]
|
60 |
| - (.subscribe o ^Action1 (iop/action* on-next-action) ^Action1 (iop/action* on-error-action))) |
| 62 | + (.subscribe o |
| 63 | + ^Action1 (iop/action* on-next-action) |
| 64 | + ^Action1 (iop/action* on-error-action))) |
| 65 | + |
61 | 66 | ([^Observable o on-next-action on-error-action on-completed-action]
|
62 |
| - (.subscribe o ^Action1 (iop/action* on-next-action) ^Action1 (iop/action* on-error-action) ^Action0 (iop/action* on-completed-action)))) |
| 67 | + (.subscribe o |
| 68 | + ^Action1 (iop/action* on-next-action) |
| 69 | + ^Action1 (iop/action* on-error-action) |
| 70 | + ^Action0 (iop/action* on-completed-action)))) |
63 | 71 |
|
64 | 72 | (defn ^Subscriber ->subscriber
|
65 | 73 | ""
|
66 | 74 | ([o on-next-action] (->subscriber o on-next-action nil nil))
|
67 |
| - ([o on-next-action on-error-action] (->subscriber o on-next-action on-error-action)) |
| 75 | + ([o on-next-action on-error-action] (->subscriber o on-next-action on-error-action nil)) |
68 | 76 | ([^Subscriber o on-next-action on-error-action on-completed-action]
|
69 | 77 | (proxy [Subscriber] [o]
|
70 | 78 | (onCompleted []
|
|
80 | 88 | (on-next-action o t)
|
81 | 89 | (on-next o t))))))
|
82 | 90 |
|
83 |
| -(defn ^Observable$Operator ->operator |
84 |
| - "Create a basic Operator with the given handler fns. If a handler is omitted or nil |
| 91 | +(defn ^Observable$Operator fn->operator |
| 92 | + "Create a basic Operator with f. If a handler is omitted or nil |
85 | 93 | it's treated as a pass-through.
|
86 | 94 |
|
87 | 95 | on-next-action Passed Subscriber and value
|
|
92 | 100 | lift
|
93 | 101 | rx.Observable$Operator
|
94 | 102 | "
|
95 |
| - [input] |
96 |
| - {:pre [(fn? input)]} |
| 103 | + [f] |
| 104 | + {:pre [(fn? f)]} |
97 | 105 | (reify Observable$Operator
|
98 | 106 | (call [this o]
|
99 |
| - (input o)))) |
| 107 | + (f o)))) |
100 | 108 |
|
101 | 109 | (defn lift
|
102 | 110 | "Lift the Operator op over the given Observable xs
|
103 | 111 |
|
104 | 112 | Example:
|
105 | 113 |
|
106 | 114 | (->> my-observable
|
107 |
| - (rx/lift (rx/->operator ...)) |
| 115 | + (rx/lift (rx/fn->operator ...)) |
108 | 116 | ...)
|
109 | 117 |
|
110 | 118 | See:
|
111 | 119 | rx.Observable/lift
|
112 |
| - ->operator |
| 120 | + fn->operator |
113 | 121 | "
|
114 | 122 | [^Observable$Operator op ^Observable xs]
|
115 | 123 | (.lift xs op))
|
|
160 | 168 | [handler]
|
161 | 169 | (fn [^Observer observer]
|
162 | 170 | (handler observer)
|
163 |
| - (.onCompleted observer))) |
| 171 | + (when-not (unsubscribed? observer) |
| 172 | + (.onCompleted observer)))) |
164 | 173 |
|
165 | 174 | (defn wrap-on-error
|
166 | 175 | "Wrap handler with code that automaticaly calls (on-error) if an exception is thrown"
|
|
169 | 178 | (try
|
170 | 179 | (handler observer)
|
171 | 180 | (catch Throwable e
|
172 |
| - (.onError observer e))))) |
| 181 | + (when-not (unsubscribed? observer) |
| 182 | + (.onError observer e)))))) |
173 | 183 |
|
174 | 184 | (defn ^Observable merge
|
175 | 185 | "Observable.merge, renamed because merge means something else in Clojure
|
|
194 | 204 | (throw (IllegalArgumentException. (str "Don't know how to merge " (type os))))))
|
195 | 205 |
|
196 | 206 | (defn ^Observable merge-delay-error
|
197 |
| - "Observable.mergeDelayError, renamed because merge means something else in Clojure" |
| 207 | + "Observable.mergeDelayError" |
198 | 208 | [os]
|
199 | 209 | (cond
|
200 | 210 | (instance? java.util.List os)
|
|
205 | 215 | (throw (IllegalArgumentException. (str "Don't know how to merge " (type os))))))
|
206 | 216 |
|
207 | 217 | (defn ^Observable zip
|
208 |
| - "Observable.zip. You want map." |
| 218 | + "rx.Observable.zip. You want map." |
209 | 219 | ([f ^Observable a ^Observable b] (Observable/zip a b (iop/fn* f)))
|
210 | 220 | ([f ^Observable a ^Observable b ^Observable c] (Observable/zip a b c (iop/fn* f)))
|
211 | 221 | ([f ^Observable a ^Observable b ^Observable c ^Observable d] (Observable/zip a b c d (iop/fn* f)))
|
|
224 | 234 | names (clojure.core/mapv clojure.core/first pairs)
|
225 | 235 | values (clojure.core/map second pairs)]
|
226 | 236 | `(zip (fn ~names ~@body) ~@values)))
|
227 |
| -;################################################################################ |
228 | 237 |
|
| 238 | +;################################################################################ |
229 | 239 |
|
| 240 | +(defn ^Observable never |
| 241 | + "Returns an Observable that never emits any values and never completes. |
230 | 242 |
|
| 243 | + See: |
| 244 | + rx.Observable/never |
| 245 | + " |
| 246 | + [] |
| 247 | + (Observable/never)) |
231 | 248 |
|
| 249 | +(defn ^Observable empty |
| 250 | + "Returns an Observable that completes immediately without emitting any values. |
232 | 251 |
|
233 |
| -(defn ^Observable never [] (Observable/never)) |
234 |
| -(defn ^Observable empty [] (Observable/empty)) |
| 252 | + See: |
| 253 | + rx.Observable/empty |
| 254 | + " |
| 255 | + [] |
| 256 | + (Observable/empty)) |
235 | 257 |
|
236 | 258 | (defn ^Observable return
|
237 | 259 | "Returns an observable that emits a single value.
|
238 | 260 |
|
239 | 261 | See:
|
240 |
| - Observable/just |
| 262 | + rx.Observable/just |
241 | 263 | "
|
242 | 264 | [value]
|
243 | 265 | (Observable/just value))
|
|
329 | 351 |
|
330 | 352 | (defn interpose
|
331 | 353 | [sep xs]
|
332 |
| - (let [op (->operator (fn [o] |
| 354 | + (let [op (fn->operator (fn [o] |
333 | 355 | (let [first? (atom true)]
|
334 | 356 | (->subscriber o (fn [o v]
|
335 | 357 | (if-not (compare-and-set! first? true false)
|
|
385 | 407 | clojure.core/map-indexed
|
386 | 408 | "
|
387 | 409 | [f xs]
|
388 |
| - (let [op (->operator (fn [o] |
| 410 | + (let [op (fn->operator (fn [o] |
389 | 411 | (let [n (atom -1)]
|
390 | 412 | (->subscriber o
|
391 | 413 | (fn [o v] (on-next o (f (swap! n inc) v)))))))]
|
|
589 | 611 | ;################################################################################;
|
590 | 612 |
|
591 | 613 | (defn generator*
|
592 |
| - "Creates an observable that calls (f observable & args) which should emit a sequence. |
| 614 | + "Creates an observable that calls (f observable & args) which should emit values |
| 615 | + with (rx/on-next observable value). |
593 | 616 |
|
594 | 617 | Automatically calls on-completed on return, or on-error if any exception is thrown.
|
595 | 618 |
|
596 |
| - Subscribers will block. |
| 619 | + f should exit early if (rx/unsubscribed? observable) returns true |
597 | 620 |
|
598 | 621 | Examples:
|
599 | 622 |
|
600 | 623 | ; An observable that emits just 99
|
601 |
| - (generator* on-next 99) |
| 624 | + (rx/generator* on-next 99) |
602 | 625 | "
|
603 | 626 | [f & args]
|
604 | 627 | (fn->o (-> #(apply f % args)
|
|
611 | 634 |
|
612 | 635 | Automatically calls on-completed on return, or on-error if any exception is thrown.
|
613 | 636 |
|
614 |
| - Subscribe will block. |
| 637 | + The body should exit early if (rx/unsubscribed? observable) returns true |
615 | 638 |
|
616 | 639 | Examples:
|
617 | 640 |
|
|
0 commit comments