-
Notifications
You must be signed in to change notification settings - Fork 137
/
Copy pathxhrio.cljs
56 lines (54 loc) · 1.89 KB
/
xhrio.cljs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
(ns ajax.xhrio
(:require goog.net.EventType
goog.net.ErrorCode
[goog.net.XhrIo :as xhr]
[goog.net.XhrManager :as xhrm]
[goog.Uri :as uri]
[goog.json :as goog-json]
[goog.events :as events]
[ajax.protocols :refer [AjaxImpl AjaxRequest
AjaxResponse Interceptor]]))
(extend-type goog.net.XhrIo
AjaxImpl
(-js-ajax-request
[this
{:keys [uri method body headers timeout with-credentials
response-format progress-handler]
:or {with-credentials false
timeout 0}}
handler]
(when-let [response-type (:type response-format)]
(.setResponseType this (name response-type)))
(when (fn? progress-handler)
(doto this
(.setProgressEventsEnabled true)
(events/listen goog.net.EventType.UPLOAD_PROGRESS progress-handler)))
(doto this
(events/listen goog.net.EventType/COMPLETE
#(handler (.-target %)))
(.setTimeoutInterval timeout)
(.setWithCredentials with-credentials)
(.send uri method body (clj->js headers))))
AjaxRequest
(-abort [this] (.abort this goog.net.ErrorCode/ABORT))
AjaxResponse
(-body [this] (.getResponse this))
(-status [this] (.getStatus this))
(-status-text [this] (.getStatusText this))
(-get-all-headers [this]
(js->clj (.getResponseHeaders this)))
(-get-response-header [this header]
(.getResponseHeader this header))
(-was-aborted [this]
(= (.getLastErrorCode this) goog.net.ErrorCode/ABORT)))
(extend-type goog.net.XhrManager
AjaxImpl
(-js-ajax-request
[this {:keys [uri method body headers
id timeout priority max-retries]
:or {timeout 0}}
handler]
(doto this
(.setTimeoutInterval timeout)
(.send id uri method body (clj->js headers)
priority handler max-retries))))