Skip to content

Commit

Permalink
Extracted remote logic to github file
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagofm committed Mar 21, 2016
1 parent 74ff6a5 commit 3e4887b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 33 deletions.
37 changes: 6 additions & 31 deletions src/cljs/haxlife/core.cljs
Original file line number Diff line number Diff line change
@@ -1,49 +1,24 @@
(ns haxlife.core
(:require-macros [cljs.core.async.macros :refer [go]])
(:require [om.next :as om :include-macros true]
[haxlife.components.window :as window]
[goog.dom :as gdom]
[clojure.string :as string]
[haxlife.query :as query]
[cljs.core.async :refer [<! >! put! chan]])
(:import [goog Uri]
[goog.net Jsonp]))
[haxlife.github :as github]
[cljs.core.async :refer [chan]]))

(enable-console-print!)

(def base-url
"http://en.wikipedia.org/w/api.php?action=opensearch&format=json&search=")

(defn jsonp
([uri] (jsonp (chan) uri))
([c uri]
(let [gjsonp (Jsonp. (Uri. uri))]
(.send gjsonp nil #(put! c %))
c)))

(def app-state (atom))
(def send-chan (chan))

(defn search-loop [c]
(go
(loop [[query cb] (<! c)]
(let [[_ results] (<! (jsonp (str base-url query)))]
(cb {:code results}))
(recur (<! c)))))

(defn send-to-chan [c]
(fn [{:keys [search]} cb]
(when search
(let [{[search] :children} (om/query->ast search)
query (get-in search [:params :query])]
(put! c [query cb])))))
(github/code-loop send-chan)

(def reconciler
(om/reconciler {:state app-state
:parser (om/parser {:read query/read :mutate query/mutate})
:send (send-to-chan send-chan)
:remotes [:remote :search]}))
:send (github/send-to-chan send-chan)
:remotes [:remote :code]}))


(search-loop send-chan)

(om/add-root! reconciler window/Window (gdom/getElement "app"))
32 changes: 31 additions & 1 deletion src/cljs/haxlife/github.cljs
Original file line number Diff line number Diff line change
@@ -1,11 +1,41 @@
(ns haxlife.github
(:require-macros [cljs.core.async.macros :refer [go]])
(:require [cljs-http.client :as http]
[cljs.core.async :refer [<! >!]]))
[om.next :as om :include-macros true]
[clojure.string :as string]
[cljs.core.async :refer [<! >! put! chan]])
(:import [goog Uri]
[goog.net Jsonp]))


(def github-repositories-url "https://api.github.com/search/repositories")

(def base-url
"http://en.wikipedia.org/w/api.php?action=opensearch&format=json&search=")


(defn jsonp
([uri] (jsonp (chan) uri))
([c uri]
(let [gjsonp (Jsonp. (Uri. uri))]
(.send gjsonp nil #(put! c %))
c)))

(defn send-to-chan [c]
(fn [{:keys [code]} cb]
(.log js/console (pr-str cb))
(when code
(let [{[search] :children} (om/query->ast code)
query (get-in search [:params :query])]
(put! c [query cb])))))

(defn code-loop [c]
(go
(loop [[query cb] (<! c)]
(let [[_ results] (<! (jsonp (str base-url query)))]
(cb {:code results}))
(recur (<! c)))))

(defn repositories [language repository-response cb]
(go
(if (nil? repository-response)
Expand Down
2 changes: 1 addition & 1 deletion src/cljs/haxlife/query.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
{:value (get @state k [])}
(when-not (or (string/blank? query)
(< (count query) 3))
{:search ast})))
{:code ast})))

(defmulti mutate om/dispatch)

0 comments on commit 3e4887b

Please sign in to comment.