Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Abstract some boilerplate out into its own function. #3

Open
wants to merge 1 commit into
base: kitchen-sink
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 18 additions & 19 deletions src/shoreleave/remotes/http_rpc.cljs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(ns shoreleave.remotes.http-rpc
"Remote procedure calls over HTTP"
(:require [shoreleave.remotes.xhr :as xhr]
;[goog.structs.PriorityPool :as priority]
;[goog.structs.PriorityPool :as priority]
[cljs.reader :as reader]))

;; HTTP-RPC
Expand All @@ -24,6 +24,14 @@

(def ^:dynamic *remote-uri* "/_shoreleave")

(defn read-callback
[f]
(when f
(fn [data]
(let [data (if (= data "") "nil" data)]
(f (reader/read-string data))))))


(defn remote-callback
"Call a remote-callback on the server.
Arguments:
Expand All @@ -37,24 +45,15 @@
(let [{:keys [on-success on-error]} callback] ;;TODO make xhr take *ANY* of the event triggers
(xhr/xhr [:post *remote-uri*]
:content (merge
{:remote remote
:params (pr-str params)}
(apply hash-map extra-content))
:on-success (when on-success
(fn [data]
(let [data (if (= data "") "nil" data)]
(on-success (reader/read-string data)))))
:on-error (when on-error
(fn [data]
(let [data (if (= data "") "nil" data)]
(on-error (reader/read-string data)))))))
{:remote remote
:params (pr-str params)}
(apply hash-map extra-content))
:on-success (read-callback on-success)
:on-error (read-callback on-error)))
(xhr/xhr [:post *remote-uri*]
:content (merge
{:remote remote
:params (pr-str params)}
(apply hash-map extra-content))
:on-success (when callback
(fn [data]
(let [data (if (= data "") "nil" data)]
(callback (reader/read-string data))))))))
{:remote remote
:params (pr-str params)}
(apply hash-map extra-content))
:on-success (read-callback callback))))