@@ -12,14 +12,15 @@ module Network.HTTP.Affjax
12
12
, delete , delete_
13
13
) where
14
14
15
- import Control.Monad.Aff (Aff (), makeAff )
15
+ import Control.Monad.Aff (Aff (), makeAff , makeAff' , Canceler () )
16
16
import Control.Monad.Eff (Eff ())
17
17
import Control.Monad.Eff.Exception (Error (), error )
18
18
import Data.Either (Either (..))
19
19
import Data.Foreign (Foreign (..), F ())
20
- import Data.Function (Fn4 (), runFn4 )
20
+ import Data.Function (Fn5 (), runFn5 , Fn4 (), runFn4 )
21
21
import Data.Maybe (Maybe (..), maybe )
22
22
import Data.Nullable (Nullable (), toNullable )
23
+ import DOM.XHR (XMLHttpRequest ())
23
24
import Network.HTTP.Affjax.Request
24
25
import Network.HTTP.Affjax.Response
25
26
import Network.HTTP.Affjax.ResponseType
@@ -66,7 +67,7 @@ type URL = String
66
67
67
68
-- | Makes an `Affjax` request.
68
69
affjax :: forall e a b . (Requestable a , Responsable b ) => AffjaxRequest a -> Affjax e b
69
- affjax = makeAff <<< affjax'
70
+ affjax = makeAff' <<< affjax'
70
71
71
72
-- | Makes a `GET` request to the specified URL.
72
73
get :: forall e a . (Responsable a ) => URL -> Affjax e a
@@ -121,9 +122,9 @@ affjax' :: forall e a b. (Requestable a, Responsable b) =>
121
122
AffjaxRequest a ->
122
123
(Error -> Eff (ajax :: Ajax | e ) Unit ) ->
123
124
(AffjaxResponse b -> Eff (ajax :: Ajax | e ) Unit ) ->
124
- Eff (ajax :: Ajax | e ) Unit
125
+ Eff (ajax :: Ajax | e ) ( Canceler ( ajax :: Ajax | e ))
125
126
affjax' req eb cb =
126
- runFn4 unsafeAjax responseHeader req' eb cb'
127
+ runFn5 _ajax responseHeader req' cancelAjax eb cb'
127
128
where
128
129
req' :: AjaxRequest
129
130
req' = { method: methodToString req.method
@@ -149,9 +150,9 @@ type AjaxRequest =
149
150
, password :: Nullable String
150
151
}
151
152
152
- foreign import unsafeAjax
153
+ foreign import _ajax
153
154
" " "
154
- function unsafeAjax (mkHeader, options, errback, callback) {
155
+ function _ajax (mkHeader, options, canceler , errback, callback) {
155
156
return function () {
156
157
var xhr = new XMLHttpRequest();
157
158
xhr.open(options.method || " GET " , options.url || " /" , true, options.username, options.password);
@@ -179,10 +180,29 @@ foreign import unsafeAjax
179
180
};
180
181
xhr.responseType = options.responseType;
181
182
xhr.send(options.content);
183
+ return canceler(xhr);
182
184
};
183
185
}
184
- " " " :: forall e a . Fn4 (String -> String -> ResponseHeader )
186
+ " " " :: forall e a . Fn5 (String -> String -> ResponseHeader )
185
187
AjaxRequest
188
+ (XMLHttpRequest -> Canceler (ajax :: Ajax | e ))
186
189
(Error -> Eff (ajax :: Ajax | e ) Unit )
187
190
(AffjaxResponse Foreign -> Eff (ajax :: Ajax | e ) Unit )
188
- (Eff (ajax :: Ajax | e ) Unit )
191
+ (Eff (ajax :: Ajax | e ) (Canceler (ajax :: Ajax | e )))
192
+
193
+ cancelAjax :: forall e . XMLHttpRequest -> Canceler (ajax :: Ajax | e )
194
+ cancelAjax xhr err = makeAff (\eb cb -> runFn4 _cancelAjax xhr err eb cb)
195
+
196
+ foreign import _cancelAjax
197
+ " " "
198
+ function _cancelAjax (xhr, cancelError, errback, callback) {
199
+ return function () {
200
+ try { xhr.abort(); } catch (e) { return errback(e)(); }
201
+ return callback(true)();
202
+ };
203
+ };
204
+ " " " :: forall e . Fn4 XMLHttpRequest
205
+ Error
206
+ (Error -> Eff (ajax :: Ajax | e ) Unit )
207
+ (Boolean -> Eff (ajax :: Ajax | e ) Unit )
208
+ (Eff (ajax :: Ajax | e ) Unit )
0 commit comments