Skip to content

Commit a698242

Browse files
Merge pull request #344 from daveray/covariant-support-clj
Update Clojure interop to support new OnSubscribeFunc with rx/fn.
2 parents 74cd3d4 + 05bbf38 commit a698242

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

language-adaptors/rxjava-clojure/src/main/clojure/rx/lang/clojure/interop.clj

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@
1717
(let [f-name (gensym "rc")]
1818
`(let [~f-name ~f]
1919
(reify
20+
; If they want Func1, give them onSubscribe as well so Observable/create can be
21+
; used seemlessly with rx/fn.
22+
~@(if (and (= prefix "rx.util.functions.Func")
23+
(some #{1} arities))
24+
`(rx.Observable$OnSubscribeFunc
25+
(~'onSubscribe [~'this observer#]
26+
(~f-name observer#))))
27+
2028
~@(mapcat (clojure.core/fn [n]
2129
(let [ifc-sym (symbol (str prefix n))
2230
arg-syms (map #(symbol (str "v" %)) (range n))]
@@ -31,6 +39,10 @@
3139
3240
If the f has the wrong arity, an ArityException will be thrown at runtime.
3341
42+
This will also implement rx.Observable$OnSubscribeFunc.onSubscribe for use with
43+
Observable/create. In this case, the function must take an Observable as its single
44+
argument and return a subscription object.
45+
3446
Example:
3547
3648
(.reduce my-numbers (rx/fn* +))
@@ -64,6 +76,15 @@
6476
6577
(.map my-observable (rx/fn [a] (* 2 a)))
6678
79+
or, to create an Observable:
80+
81+
(Observable/create (rx/fn [observer]
82+
(.onNext observer 10)
83+
(.onCompleted observer)
84+
(Subscriptions/empty)))
85+
86+
See:
87+
rx.lang.clojure.interop/fn*
6788
"
6889
[& fn-form]
6990
; preserve metadata so type hints work

language-adaptors/rxjava-clojure/src/test/clojure/rx/lang/clojure/interop_test.clj

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
(deftest test-fn*
99
(testing "implements Func0-9"
1010
(let [f (rx/fn* vector)]
11+
(is (instance? rx.Observable$OnSubscribeFunc f))
1112
(is (instance? rx.util.functions.Func0 f))
1213
(is (instance? rx.util.functions.Func1 f))
1314
(is (instance? rx.util.functions.Func2 f))
@@ -113,6 +114,14 @@
113114

114115
(deftest test-basic-usage
115116

117+
(testing "can create an observable"
118+
(is (= 99
119+
(-> (Observable/create (rx/fn [^rx.Observer o]
120+
(.onNext o 99)
121+
(.onCompleted o)
122+
(rx.subscriptions.Subscriptions/empty)))
123+
(BlockingObservable/single)))))
124+
116125
(testing "can pass rx/fn to map and friends"
117126
(is (= (+ 1 4 9)
118127
(-> (Observable/from [1 2 3])

0 commit comments

Comments
 (0)