-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from daveray/static-core-clj
Static core clj
- Loading branch information
Showing
9 changed files
with
344 additions
and
133 deletions.
There are no files selected for viewing
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
58 changes: 58 additions & 0 deletions
58
...e-adaptors/rxjava-clojure/src/examples/clojure/rx/lang/clojure/examples/http_examples.txt
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,58 @@ | ||
(ns rx.lang.clojure.examples.http-examples | ||
(:require [rx.lang.clojure.interop :as rx] | ||
[clj-http.client :as http]) | ||
(:import rx.Observable rx.subscriptions.Subscriptions)) | ||
|
||
; NOTE on naming conventions. I'm using camelCase names (against clojure convention) | ||
; in this file as I'm purposefully keeping functions and methods across | ||
; different language implementations in-sync for easy comparison. | ||
|
||
(defn fetchWikipediaArticleAsynchronously [wikipediaArticleNames] | ||
"Fetch a list of Wikipedia articles asynchronously. | ||
|
||
return Observable<String> of HTML" | ||
(Observable/create | ||
(rx/fn [observer] | ||
(let [f (future | ||
(doseq [articleName wikipediaArticleNames] | ||
(-> observer (.onNext (http/get (str "http://en.wikipedia.org/wiki/" articleName))))) | ||
; after sending response to onnext we complete the sequence | ||
(-> observer .onCompleted))] | ||
; a subscription that cancels the future if unsubscribed | ||
(Subscriptions/create (rx/action [] (future-cancel f))))))) | ||
|
||
; To see output | ||
(comment | ||
(-> (fetchWikipediaArticleAsynchronously ["Tiger" "Elephant"]) | ||
(.subscribe (rx/action [v] (println "--- Article ---\n" (subs (:body v) 0 125) "..."))))) | ||
|
||
|
||
; -------------------------------------------------- | ||
; Error Handling | ||
; -------------------------------------------------- | ||
|
||
(defn fetchWikipediaArticleAsynchronouslyWithErrorHandling [wikipediaArticleNames] | ||
"Fetch a list of Wikipedia articles asynchronously | ||
with proper error handling. | ||
|
||
return Observable<String> of HTML" | ||
(Observable/create | ||
(rx/fn [observer] | ||
(let [f (future | ||
(try | ||
(doseq [articleName wikipediaArticleNames] | ||
(-> observer (.onNext (http/get (str "http://en.wikipedia.org/wiki/" articleName))))) | ||
;(catch Exception e (prn "exception"))) | ||
(catch Exception e (-> observer (.onError e)))) | ||
; after sending response to onNext we complete the sequence | ||
(-> observer .onCompleted))] | ||
; a subscription that cancels the future if unsubscribed | ||
(Subscriptions/create (rx/action [] (future-cancel f))))))) | ||
|
||
; To see output | ||
(comment | ||
(-> (fetchWikipediaArticleAsynchronouslyWithErrorHandling ["Tiger" "NonExistentTitle" "Elephant"]) | ||
(.subscribe (rx/action [v] (println "--- Article ---\n" (subs (:body v) 0 125) "...")) | ||
(rx/action [e] (println "--- Error ---\n" (.getMessage e)))))) | ||
|
||
|
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.