Skip to content

Commit c883eda

Browse files
committed
Merge pull request #22 from michaelficarra/GH-24
fixes #21: use setImmediate in _setTimeout when millis is zero
2 parents f7c4d83 + d1f9bd7 commit c883eda

File tree

2 files changed

+38
-17
lines changed

2 files changed

+38
-17
lines changed

MODULES.md

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
data Aff :: # ! -> * -> *
99
```
1010

11-
An asynchronous computation with effects `e`. The computation either
11+
An asynchronous computation with effects `e`. The computation either
1212
errors or produces a value of type `a`.
1313

1414
This is moral equivalent of `ErrorT (ContT Unit (Eff e)) a`.
@@ -19,7 +19,7 @@ This is moral equivalent of `ErrorT (ContT Unit (Eff e)) a`.
1919
type PureAff a = forall e. Aff e a
2020
```
2121

22-
A pure asynchronous computation, having no effects other than
22+
A pure asynchronous computation, having no effects other than
2323
asynchronous computation.
2424

2525
#### `Canceler`
@@ -29,10 +29,10 @@ newtype Canceler e
2929
= Canceler (Error -> Aff e Boolean)
3030
```
3131

32-
A canceler is asynchronous function that can be used to attempt the
32+
A canceler is asynchronous function that can be used to attempt the
3333
cancelation of a computation. Returns a boolean flag indicating whether
3434
or not the cancellation was successful. Many computations may be composite,
35-
in such cases the flag indicates whether any part of the computation was
35+
in such cases the flag indicates whether any part of the computation was
3636
successfully canceled. The flag should not be used for communication.
3737

3838
#### `cancel`
@@ -50,7 +50,7 @@ cancelWith :: forall e a. Aff e a -> Canceler e -> Aff e a
5050
```
5151

5252
This function allows you to attach a custom canceler to an asynchronous
53-
computation. If the computation is canceled, then the custom canceler
53+
computation. If the computation is canceled, then the custom canceler
5454
will be run along side the computation's own canceler.
5555

5656
#### `launchAff`
@@ -105,9 +105,17 @@ Runs the asynchronous computation off the current execution context.
105105
later' :: forall e a. Number -> Aff e a -> Aff e a
106106
```
107107

108-
Runs the specified asynchronous computation later, by the specified
108+
Runs the specified asynchronous computation later, by the specified
109109
number of milliseconds.
110110

111+
#### `finally`
112+
113+
``` purescript
114+
finally :: forall e a b. Aff e a -> Aff e b -> Aff e a
115+
```
116+
117+
Compute `aff1`, followed by `aff2` regardless of whether `aff1` terminated successfully.
118+
111119
#### `forkAff`
112120

113121
``` purescript
@@ -117,7 +125,7 @@ forkAff :: forall e a. Aff e a -> Aff e (Canceler e)
117125
Forks the specified asynchronous computation so subsequent computations
118126
will not block on the result of the computation.
119127

120-
Returns a canceler that can be used to attempt cancellation of the
128+
Returns a canceler that can be used to attempt cancellation of the
121129
forked computation.
122130

123131
#### `attempt`
@@ -262,6 +270,9 @@ instance monoidCanceler :: Monoid (Canceler e)
262270

263271
## Module Control.Monad.Aff.AVar
264272

273+
274+
A low-level primitive for building asynchronous code.
275+
265276
#### `AVAR`
266277

267278
``` purescript
@@ -365,6 +376,11 @@ from writing `liftEff $ trace x` everywhere.
365376

366377
## Module Control.Monad.Aff.Par
367378

379+
380+
A newtype over `Aff` that provides `Applicative` instances that run in
381+
parallel. This is useful, for example, if you want to run a whole bunch
382+
of AJAX requests at the same time, rather than sequentially.
383+
368384
#### `Par`
369385

370386
``` purescript

src/Control/Monad/Aff.purs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,20 @@ module Control.Monad.Aff
3333
import Control.Monad.Eff.Class
3434
import Control.Monad.Error.Class(MonadError, throwError)
3535

36-
-- | An asynchronous computation with effects `e`. The computation either
36+
-- | An asynchronous computation with effects `e`. The computation either
3737
-- | errors or produces a value of type `a`.
3838
-- |
3939
-- | This is moral equivalent of `ErrorT (ContT Unit (Eff e)) a`.
4040
foreign import data Aff :: # ! -> * -> *
4141

42-
-- | A pure asynchronous computation, having no effects other than
42+
-- | A pure asynchronous computation, having no effects other than
4343
-- | asynchronous computation.
4444
type PureAff a = forall e. Aff e a
4545

46-
-- | A canceler is asynchronous function that can be used to attempt the
46+
-- | A canceler is asynchronous function that can be used to attempt the
4747
-- | cancelation of a computation. Returns a boolean flag indicating whether
4848
-- | or not the cancellation was successful. Many computations may be composite,
49-
-- | in such cases the flag indicates whether any part of the computation was
49+
-- | in such cases the flag indicates whether any part of the computation was
5050
-- | successfully canceled. The flag should not be used for communication.
5151
newtype Canceler e = Canceler (Error -> Aff e Boolean)
5252

@@ -55,7 +55,7 @@ module Control.Monad.Aff
5555
cancel (Canceler f) = f
5656

5757
-- | This function allows you to attach a custom canceler to an asynchronous
58-
-- | computation. If the computation is canceled, then the custom canceler
58+
-- | computation. If the computation is canceled, then the custom canceler
5959
-- | will be run along side the computation's own canceler.
6060
cancelWith :: forall e a. Aff e a -> Canceler e -> Aff e a
6161
cancelWith aff c = runFn3 _cancelWith nonCanceler aff c
@@ -86,7 +86,7 @@ module Control.Monad.Aff
8686
later :: forall e a. Aff e a -> Aff e a
8787
later = later' 0
8888

89-
-- | Runs the specified asynchronous computation later, by the specified
89+
-- | Runs the specified asynchronous computation later, by the specified
9090
-- | number of milliseconds.
9191
later' :: forall e a. Number -> Aff e a -> Aff e a
9292
later' n aff = runFn3 _setTimeout nonCanceler n aff
@@ -101,7 +101,7 @@ module Control.Monad.Aff
101101
-- | Forks the specified asynchronous computation so subsequent computations
102102
-- | will not block on the result of the computation.
103103
-- |
104-
-- | Returns a canceler that can be used to attempt cancellation of the
104+
-- | Returns a canceler that can be used to attempt cancellation of the
105105
-- | forked computation.
106106
forkAff :: forall e a. Aff e a -> Aff e (Canceler e)
107107
forkAff aff = runFn2 _forkAff nonCanceler aff
@@ -205,7 +205,7 @@ module Control.Monad.Aff
205205
};
206206
207207
canceler2(e)(s, f);
208-
canceler1(e)(s, f);
208+
canceler1(e)(s, f);
209209
210210
return nonCanceler;
211211
};
@@ -216,10 +216,15 @@ module Control.Monad.Aff
216216

217217
foreign import _setTimeout """
218218
function _setTimeout(nonCanceler, millis, aff) {
219+
var set = setTimeout, clear = clearTimeout;
220+
if (millis <= 0 && typeof setImmediate === "function") {
221+
set = setImmediate;
222+
clear = clearImmediate;
223+
}
219224
return function(success, error) {
220225
var canceler;
221226
222-
var timeout = setTimeout(function() {
227+
var timeout = set(function() {
223228
canceler = aff(success, error);
224229
}, millis);
225230
@@ -228,7 +233,7 @@ module Control.Monad.Aff
228233
if (canceler !== undefined) {
229234
return canceler(e)(s, f);
230235
} else {
231-
clearTimeout(timeout);
236+
clear(timeout);
232237
233238
try {
234239
s(true);

0 commit comments

Comments
 (0)