|
54 | 54 | (.onError o e))
|
55 | 55 |
|
56 | 56 | ;################################################################################
|
| 57 | +; Tools for creating new operators and observables |
57 | 58 |
|
58 |
| -(defn ^Subscription subscribe |
59 |
| - |
60 |
| - ([^Observable o on-next-action] |
61 |
| - (.subscribe o |
62 |
| - ^Action1 (iop/action* on-next-action))) |
| 59 | +(declare unsubscribed?) |
63 | 60 |
|
64 |
| - ([^Observable o on-next-action on-error-action] |
65 |
| - (.subscribe o |
66 |
| - ^Action1 (iop/action* on-next-action) |
67 |
| - ^Action1 (iop/action* on-error-action))) |
68 |
| - |
69 |
| - ([^Observable o on-next-action on-error-action on-completed-action] |
70 |
| - (.subscribe o |
71 |
| - ^Action1 (iop/action* on-next-action) |
72 |
| - ^Action1 (iop/action* on-error-action) |
73 |
| - ^Action0 (iop/action* on-completed-action)))) |
74 |
| - |
75 |
| -(defn ^Subscriber ->subscriber |
| 61 | +(defn ^Subscriber subscriber |
76 | 62 | ""
|
77 |
| - ([o on-next-action] (->subscriber o on-next-action nil nil)) |
78 |
| - ([o on-next-action on-error-action] (->subscriber o on-next-action on-error-action nil)) |
| 63 | + ([o on-next-action] (subscriber o on-next-action nil nil)) |
| 64 | + ([o on-next-action on-error-action] (subscriber o on-next-action on-error-action nil)) |
79 | 65 | ([^Subscriber o on-next-action on-error-action on-completed-action]
|
80 | 66 | (proxy [Subscriber] [o]
|
81 | 67 | (onCompleted []
|
|
91 | 77 | (on-next-action o t)
|
92 | 78 | (on-next o t))))))
|
93 | 79 |
|
94 |
| -(defn ^Observable$Operator fn->operator |
95 |
| - "Create a basic Operator with f. If a handler is omitted or nil |
96 |
| - it's treated as a pass-through. |
| 80 | +(defn ^Subscription subscription |
| 81 | + "Create a new subscription that calls the given no-arg handler function when |
| 82 | + unsubscribe is called |
| 83 | +
|
| 84 | + See: |
| 85 | + rx.subscriptions.Subscriptions/create |
| 86 | + " |
| 87 | + [handler] |
| 88 | + (Subscriptions/create ^Action0 (iop/action* handler))) |
97 | 89 |
|
98 |
| - on-next-action Passed Subscriber and value |
99 |
| - on-error-action Passed Throwable |
100 |
| - on-completed-action No-args |
| 90 | +(defn ^Observable$Operator operator* |
| 91 | + "Returns a new implementation of rx.Observable$Operator that calls the given |
| 92 | + function with a rx.Subscriber. The function should return a rx.Subscriber. |
101 | 93 |
|
102 | 94 | See:
|
103 |
| - lift |
104 |
| - rx.Observable$Operator |
| 95 | + lift |
| 96 | + rx.Observable$Operator |
105 | 97 | "
|
106 | 98 | [f]
|
107 | 99 | {:pre [(fn? f)]}
|
108 | 100 | (reify Observable$Operator
|
109 | 101 | (call [this o]
|
110 | 102 | (f o))))
|
111 | 103 |
|
| 104 | +(defn ^Observable observable* |
| 105 | + "Create an Observable from the given function. |
| 106 | +
|
| 107 | + When subscribed to, (f subscriber) is called at which point, f can start emitting values, etc. |
| 108 | + The passed subscriber is of type rx.Subscriber. |
| 109 | +
|
| 110 | + See: |
| 111 | + rx.Subscriber |
| 112 | + rx.Observable/create |
| 113 | + " |
| 114 | + [f] |
| 115 | + (Observable/create ^Observable$OnSubscribe (iop/action* f))) |
| 116 | + |
| 117 | +(defn wrap-on-completed |
| 118 | + "Wrap handler with code that automaticaly calls rx.Observable.onCompleted." |
| 119 | + [handler] |
| 120 | + (fn [^Observer observer] |
| 121 | + (handler observer) |
| 122 | + (when-not (unsubscribed? observer) |
| 123 | + (.onCompleted observer)))) |
| 124 | + |
| 125 | +(defn wrap-on-error |
| 126 | + "Wrap handler with code that automaticaly calls (on-error) if an exception is thrown" |
| 127 | + [handler] |
| 128 | + (fn [^Observer observer] |
| 129 | + (try |
| 130 | + (handler observer) |
| 131 | + (catch Throwable e |
| 132 | + (when-not (unsubscribed? observer) |
| 133 | + (.onError observer e)))))) |
| 134 | + |
112 | 135 | (defn lift
|
113 | 136 | "Lift the Operator op over the given Observable xs
|
114 | 137 |
|
115 | 138 | Example:
|
116 | 139 |
|
117 | 140 | (->> my-observable
|
118 |
| - (rx/lift (rx/fn->operator ...)) |
| 141 | + (rx/lift (rx/operator ...)) |
119 | 142 | ...)
|
120 | 143 |
|
121 | 144 | See:
|
122 | 145 | rx.Observable/lift
|
123 |
| - fn->operator |
| 146 | + operator |
124 | 147 | "
|
125 | 148 | [^Observable$Operator op ^Observable xs]
|
126 | 149 | (.lift xs op))
|
127 | 150 |
|
| 151 | +;################################################################################ |
| 152 | + |
| 153 | +(defn ^Subscription subscribe |
| 154 | + |
| 155 | + ([^Observable o on-next-action] |
| 156 | + (.subscribe o |
| 157 | + ^Action1 (iop/action* on-next-action))) |
| 158 | + |
| 159 | + ([^Observable o on-next-action on-error-action] |
| 160 | + (.subscribe o |
| 161 | + ^Action1 (iop/action* on-next-action) |
| 162 | + ^Action1 (iop/action* on-error-action))) |
| 163 | + |
| 164 | + ([^Observable o on-next-action on-error-action on-completed-action] |
| 165 | + (.subscribe o |
| 166 | + ^Action1 (iop/action* on-next-action) |
| 167 | + ^Action1 (iop/action* on-error-action) |
| 168 | + ^Action0 (iop/action* on-completed-action)))) |
| 169 | + |
128 | 170 | (defn unsubscribe
|
129 | 171 | "Unsubscribe from Subscription s and return it."
|
130 | 172 | [^Subscription s]
|
|
158 | 200 |
|
159 | 201 | See:
|
160 | 202 | rx.Observable/create
|
161 |
| - fn->o |
| 203 | + observable* |
162 | 204 | "
|
163 | 205 | [^Subscription s]
|
164 | 206 | (.isUnsubscribed s))
|
165 | 207 |
|
166 |
| -(defn ^Subscription fn->subscription |
167 |
| - "Create a new subscription that calls the given no-arg handler function when |
168 |
| - unsubscribe is called |
| 208 | +;################################################################################ |
| 209 | +; Functions for creating Observables |
| 210 | + |
| 211 | +(defn ^Observable never |
| 212 | + "Returns an Observable that never emits any values and never completes. |
169 | 213 |
|
170 | 214 | See:
|
171 |
| - rx.subscriptions.Subscriptions/create |
| 215 | + rx.Observable/never |
172 | 216 | "
|
173 |
| - [handler] |
174 |
| - (Subscriptions/create ^Action0 (iop/action* handler))) |
175 |
| - |
176 |
| -(defn ^Observable fn->o |
177 |
| - "Create an Observable from the given function. |
| 217 | + [] |
| 218 | + (Observable/never)) |
178 | 219 |
|
179 |
| - When subscribed to, (f subscriber) is called at which point, f can start emitting values, etc. |
180 |
| - The passed subscriber is of type rx.Subscriber. |
| 220 | +(defn ^Observable empty |
| 221 | + "Returns an Observable that completes immediately without emitting any values. |
181 | 222 |
|
182 | 223 | See:
|
183 |
| - rx.Subscriber |
184 |
| - rx.Observable/create |
| 224 | + rx.Observable/empty |
185 | 225 | "
|
186 |
| - [f] |
187 |
| - (Observable/create ^Observable$OnSubscribe (iop/action* f))) |
| 226 | + [] |
| 227 | + (Observable/empty)) |
188 | 228 |
|
189 |
| -(defn wrap-on-completed |
190 |
| - "Wrap handler with code that automaticaly calls rx.Observable.onCompleted." |
191 |
| - [handler] |
192 |
| - (fn [^Observer observer] |
193 |
| - (handler observer) |
194 |
| - (when-not (unsubscribed? observer) |
195 |
| - (.onCompleted observer)))) |
| 229 | +(defn ^Observable return |
| 230 | + "Returns an observable that emits a single value. |
196 | 231 |
|
197 |
| -(defn wrap-on-error |
198 |
| - "Wrap handler with code that automaticaly calls (on-error) if an exception is thrown" |
199 |
| - [handler] |
200 |
| - (fn [^Observer observer] |
201 |
| - (try |
202 |
| - (handler observer) |
203 |
| - (catch Throwable e |
204 |
| - (when-not (unsubscribed? observer) |
205 |
| - (.onError observer e)))))) |
| 232 | + See: |
| 233 | + rx.Observable/just |
| 234 | + " |
| 235 | + [value] |
| 236 | + (Observable/just value)) |
| 237 | + |
| 238 | +(defn ^Observable seq->o |
| 239 | + "Make an observable out of some seq-able thing. The rx equivalent of clojure.core/seq." |
| 240 | + [xs] |
| 241 | + (if xs |
| 242 | + (Observable/from ^Iterable xs) |
| 243 | + (empty))) |
206 | 244 |
|
207 | 245 | ;################################################################################
|
| 246 | +; Operators |
208 | 247 |
|
209 | 248 | (defn synchronize
|
210 | 249 | ([^Observable xs]
|
|
246 | 285 | (throw (IllegalArgumentException. (str "Don't know how to merge " (type os))))))
|
247 | 286 |
|
248 | 287 |
|
249 |
| -;################################################################################ |
250 |
| - |
251 |
| -(defn ^Observable never |
252 |
| - "Returns an Observable that never emits any values and never completes. |
253 |
| -
|
254 |
| - See: |
255 |
| - rx.Observable/never |
256 |
| - " |
257 |
| - [] |
258 |
| - (Observable/never)) |
259 |
| - |
260 |
| -(defn ^Observable empty |
261 |
| - "Returns an Observable that completes immediately without emitting any values. |
262 |
| -
|
263 |
| - See: |
264 |
| - rx.Observable/empty |
265 |
| - " |
266 |
| - [] |
267 |
| - (Observable/empty)) |
268 |
| - |
269 |
| -(defn ^Observable return |
270 |
| - "Returns an observable that emits a single value. |
271 |
| -
|
272 |
| - See: |
273 |
| - rx.Observable/just |
274 |
| - " |
275 |
| - [value] |
276 |
| - (Observable/just value)) |
277 |
| - |
278 |
| -(defn ^Observable seq->o |
279 |
| - "Make an observable out of some seq-able thing. The rx equivalent of clojure.core/seq." |
280 |
| - [xs] |
281 |
| - (if xs |
282 |
| - (Observable/from ^Iterable xs) |
283 |
| - (empty))) |
284 |
| - |
285 |
| -;################################################################################ |
286 |
| - |
287 | 288 | (defn cache
|
288 | 289 | "caches the observable value so that multiple subscribers don't re-evaluate it.
|
289 | 290 |
|
|
354 | 355 | "
|
355 | 356 | ([xs] (distinct identity xs))
|
356 | 357 | ([key-fn ^Observable xs]
|
357 |
| - (let [op (fn->operator (fn [o] |
358 |
| - (let [seen (atom #{})] |
359 |
| - (->subscriber o |
360 |
| - (fn [o v] |
361 |
| - (let [key (key-fn v)] |
362 |
| - (when-not (contains? @seen key) |
363 |
| - (swap! seen conj key) |
364 |
| - (on-next o v))))))))] |
| 358 | + (let [op (operator* (fn [o] |
| 359 | + (let [seen (atom #{})] |
| 360 | + (subscriber o |
| 361 | + (fn [o v] |
| 362 | + (let [key (key-fn v)] |
| 363 | + (when-not (contains? @seen key) |
| 364 | + (swap! seen conj key) |
| 365 | + (on-next o v))))))))] |
365 | 366 | (lift op xs))))
|
366 | 367 |
|
367 | 368 | (defn ^Observable do
|
|
422 | 423 |
|
423 | 424 | (defn interpose
|
424 | 425 | [sep xs]
|
425 |
| - (let [op (fn->operator (fn [o] |
426 |
| - (let [first? (atom true)] |
427 |
| - (->subscriber o (fn [o v] |
428 |
| - (if-not (compare-and-set! first? true false) |
429 |
| - (on-next o sep)) |
430 |
| - (on-next o v))))))] |
| 426 | + (let [op (operator* (fn [o] |
| 427 | + (let [first? (atom true)] |
| 428 | + (subscriber o (fn [o v] |
| 429 | + (if-not (compare-and-set! first? true false) |
| 430 | + (on-next o sep)) |
| 431 | + (on-next o v))))))] |
431 | 432 | (lift op xs)))
|
432 | 433 |
|
433 | 434 | (defn into
|
|
499 | 500 | clojure.core/map-indexed
|
500 | 501 | "
|
501 | 502 | [f xs]
|
502 |
| - (let [op (fn->operator (fn [o] |
503 |
| - (let [n (atom -1)] |
504 |
| - (->subscriber o |
505 |
| - (fn [o v] (on-next o (f (swap! n inc) v)))))))] |
| 503 | + (let [op (operator* (fn [o] |
| 504 | + (let [n (atom -1)] |
| 505 | + (subscriber o |
| 506 | + (fn [o v] |
| 507 | + (on-next o (f (swap! n inc) v)))))))] |
506 | 508 | (lift op xs)))
|
507 | 509 |
|
508 | 510 | (def next
|
|
768 | 770 | (rx/generator* on-next 99)
|
769 | 771 | "
|
770 | 772 | [f & args]
|
771 |
| - (fn->o (-> #(apply f % args) |
772 |
| - wrap-on-completed |
773 |
| - wrap-on-error))) |
| 773 | + (observable* (-> #(apply f % args) |
| 774 | + wrap-on-completed |
| 775 | + wrap-on-error))) |
774 | 776 |
|
775 | 777 | (defmacro generator
|
776 | 778 | "Create an observable that executes body which should emit a sequence. bindings
|
|
0 commit comments