@@ -5,7 +5,6 @@ module Network.HTTP.Affjax
5
5
, AffjaxResponse
6
6
, URL
7
7
, affjax
8
- , affjax'
9
8
, get
10
9
, post , post_ , post' , post_'
11
10
, put , put_ , put' , put_'
@@ -19,21 +18,22 @@ module Network.HTTP.Affjax
19
18
20
19
import Prelude hiding (max )
21
20
22
- import Control.Monad.Aff (Aff , makeAff , makeAff' , Canceler (..), attempt , delay , forkAff , cancel )
23
- import Control.Monad.Aff.AVar ( AVAR , makeVar , takeVar , putVar )
24
- import Control.Monad.Eff (kind Effect , Eff )
21
+ import Control.Monad.Aff (Aff , try , delay )
22
+ import Control.Monad.Aff.Compat as AC
23
+ import Control.Monad.Eff (kind Effect )
25
24
import Control.Monad.Eff.Class (liftEff )
26
25
import Control.Monad.Eff.Exception (Error , error )
27
26
import Control.Monad.Eff.Ref (REF , newRef , readRef , writeRef )
28
27
import Control.Monad.Except (runExcept , throwError )
28
+ import Control.Parallel (parOneOf )
29
29
30
30
import Data.Argonaut.Parser (jsonParser )
31
31
import Data.Array as Arr
32
32
import Data.Either (Either (..), either )
33
33
import Data.Foldable (any )
34
34
import Data.Foreign (F , Foreign , ForeignError (JSONError), fail , readString , toForeign )
35
35
import Data.Function (on )
36
- import Data.Function.Uncurried (Fn5 , runFn5 , Fn4 , runFn4 )
36
+ import Data.Function.Uncurried (Fn2 , runFn2 )
37
37
import Data.HTTP.Method (Method (..), CustomMethod )
38
38
import Data.HTTP.Method as Method
39
39
import Data.Int (toNumber )
@@ -45,8 +45,6 @@ import Data.Tuple (Tuple(..), fst, snd)
45
45
46
46
import Math (max , pow )
47
47
48
- import DOM.XHR.Types (XMLHttpRequest )
49
-
50
48
import Network.HTTP.Affjax.Request (class Requestable , RequestContent , toRequest )
51
49
import Network.HTTP.Affjax.Response (class Respondable , ResponseContent , ResponseType (..), fromResponse , responseType , responseTypeToString )
52
50
import Network.HTTP.RequestHeader (RequestHeader (..), requestHeaderName , requestHeaderValue )
@@ -90,10 +88,6 @@ type AffjaxResponse a =
90
88
-- | Type alias for URL strings to aid readability of types.
91
89
type URL = String
92
90
93
- -- | Makes an `Affjax` request.
94
- affjax :: forall e a b . Requestable a => Respondable b => AffjaxRequest a -> Affjax e b
95
- affjax = makeAff' <<< affjax'
96
-
97
91
-- | Makes a `GET` request to the specified URL.
98
92
get :: forall e a . Respondable a => URL -> Affjax e a
99
93
get u = affjax $ defaultRequest { url = u }
@@ -186,23 +180,16 @@ retry
186
180
:: forall e a b
187
181
. Requestable a
188
182
=> RetryPolicy
189
- -> (AffjaxRequest a -> Affjax (avar :: AVAR , ref :: REF | e ) b )
190
- -> (AffjaxRequest a -> Affjax (avar :: AVAR , ref :: REF | e ) b )
183
+ -> (AffjaxRequest a -> Affjax (ref :: REF | e ) b )
184
+ -> (AffjaxRequest a -> Affjax (ref :: REF | e ) b )
191
185
retry policy run req = do
192
186
-- failureRef is either an exception or a failed request
193
187
failureRef <- liftEff $ newRef Nothing
194
188
let loop = go failureRef
195
189
case policy.timeout of
196
190
Nothing -> loop 1
197
191
Just timeout -> do
198
- respVar <- makeVar
199
- loopHandle <- forkAff $ loop 1 >>= putVar respVar <<< Just
200
- timeoutHandle <-
201
- forkAff $ do
202
- delay timeout
203
- putVar respVar Nothing
204
- loopHandle `cancel` error " Cancel"
205
- result <- takeVar respVar
192
+ result <- parOneOf [ Just <$> loop 1 , Nothing <$ delay timeout ]
206
193
case result of
207
194
Nothing -> do
208
195
failure <- liftEff $ readRef failureRef
@@ -225,25 +212,26 @@ retry policy run req = do
225
212
Right resp
226
213
227
214
go failureRef n = do
228
- result <- retryState <$> attempt (run req)
215
+ result <- retryState <$> try (run req)
229
216
case result of
230
217
Left err -> do
231
218
liftEff $ writeRef failureRef $ Just err
232
219
delay (policy.delayCurve n)
233
220
go failureRef (n + 1 )
234
221
Right resp -> pure resp
235
222
236
- -- | Run a request directly without using `Aff` .
237
- affjax'
223
+ -- | Makes an `Affjax` request .
224
+ affjax
238
225
:: forall e a b
239
226
. Requestable a
240
227
=> Respondable b
241
228
=> AffjaxRequest a
242
- -> (Error -> Eff (ajax :: AJAX | e ) Unit )
243
- -> (AffjaxResponse b -> Eff (ajax :: AJAX | e ) Unit )
244
- -> Eff (ajax :: AJAX | e ) (Canceler (ajax :: AJAX | e ))
245
- affjax' req eb cb =
246
- runFn5 _ajax responseHeader req' cancelAjax eb cb'
229
+ -> Affjax e b
230
+ affjax req = do
231
+ res <- AC .fromEffFnAff $ runFn2 _ajax responseHeader req'
232
+ case res { response = _ } <$> runExcept (fromResponse' res.response) of
233
+ Left err -> throwError $ error (show err)
234
+ Right res' -> pure res'
247
235
where
248
236
249
237
req' :: AjaxRequest
@@ -277,11 +265,6 @@ affjax' req eb cb =
277
265
Just h | not $ any (on eq requestHeaderName h) hs -> hs `Arr.snoc` h
278
266
_ -> hs
279
267
280
- cb' :: AffjaxResponse ResponseContent -> Eff (ajax :: AJAX | e ) Unit
281
- cb' res = case res { response = _ } <$> runExcept (fromResponse' res .response ) of
282
- Left err -> eb $ error (show err )
283
- Right res' -> cb res'
284
-
285
268
parseJSON :: String -> F Foreign
286
269
parseJSON = either (fail <<< JSONError ) (pure <<< toForeign) <<< jsonParser
287
270
@@ -301,20 +284,4 @@ type AjaxRequest =
301
284
, withCredentials :: Boolean
302
285
}
303
286
304
- foreign import _ajax
305
- :: forall e . Fn5 (String -> String -> ResponseHeader )
306
- AjaxRequest
307
- (XMLHttpRequest -> Canceler (ajax :: AJAX | e ))
308
- (Error -> Eff (ajax :: AJAX | e ) Unit )
309
- (AffjaxResponse Foreign -> Eff (ajax :: AJAX | e ) Unit )
310
- (Eff (ajax :: AJAX | e ) (Canceler (ajax :: AJAX | e )))
311
-
312
- cancelAjax :: forall e . XMLHttpRequest -> Canceler (ajax :: AJAX | e )
313
- cancelAjax xhr = Canceler \err -> makeAff (\eb cb -> runFn4 _cancelAjax xhr err eb cb)
314
-
315
- foreign import _cancelAjax
316
- :: forall e . Fn4 XMLHttpRequest
317
- Error
318
- (Error -> Eff (ajax :: AJAX | e ) Unit )
319
- (Boolean -> Eff (ajax :: AJAX | e ) Unit )
320
- (Eff (ajax :: AJAX | e ) Unit )
287
+ foreign import _ajax :: forall e . Fn2 (String -> String -> ResponseHeader ) AjaxRequest (AC.EffFnAff (ajax :: AJAX | e ) (AffjaxResponse Foreign ))
0 commit comments