Skip to content

Commit d5569a1

Browse files
committed
more tests, rename Var to Queue
1 parent 85858a0 commit d5569a1

File tree

8 files changed

+397
-326
lines changed

8 files changed

+397
-326
lines changed

MODULES.md

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ success callbacks.
6969
later :: forall e a. Aff e a -> Aff e a
7070
```
7171

72-
Runs the asynchronous computation later.
72+
Runs the asynchronous computation later (off the current execution context).
7373

7474
#### `forkAff`
7575

@@ -222,14 +222,14 @@ instance monadAffAff :: MonadAff e (Aff e)
222222

223223
``` purescript
224224
newtype Par e a
225-
= Par (AffVar e a)
225+
= Par (AffQueue e a)
226226
```
227227

228228

229229
#### `runPar`
230230

231231
``` purescript
232-
runPar :: forall e a. Par e a -> AffVar e a
232+
runPar :: forall e a. Par e a -> AffQueue e a
233233
```
234234

235235
Extracts the `Aff` from the `Par`.
@@ -292,91 +292,90 @@ instance alternativePar :: Alternative (Par e)
292292

293293

294294

295-
## Module Control.Monad.Aff.Unsafe
295+
## Module Control.Monad.Aff.Queue
296296

297-
#### `unsafeTrace`
297+
#### `QueueFx`
298298

299299
``` purescript
300-
unsafeTrace :: forall e a. a -> Aff e Unit
300+
data QueueFx :: !
301301
```
302302

303303

304-
#### `unsafeInterleaveAff`
304+
#### `Queue`
305305

306306
``` purescript
307-
unsafeInterleaveAff :: forall e1 e2 a. Aff e1 a -> Aff e2 a
307+
data Queue :: * -> *
308308
```
309309

310310

311-
312-
## Module Control.Monad.Aff.Var
313-
314-
#### `VarFx`
311+
#### `AffQueue`
315312

316313
``` purescript
317-
data VarFx :: !
314+
type AffQueue e a = Aff (queue :: QueueFx | e) a
318315
```
319316

320317

321-
#### `Var`
318+
#### `makeQueue`
322319

323320
``` purescript
324-
data Var :: * -> *
321+
makeQueue :: forall e a. AffQueue e (Queue a)
325322
```
326323

324+
Makes a new asynchronous queue.
327325

328-
#### `AffVar`
326+
#### `makeQueue'`
329327

330328
``` purescript
331-
type AffVar e a = Aff (var :: VarFx | e) a
329+
makeQueue' :: forall e a. a -> AffQueue e (Queue a)
332330
```
333331

332+
Makes a queue and sets it to some value.
334333

335-
#### `makeVar`
334+
#### `takeQueue`
336335

337336
``` purescript
338-
makeVar :: forall e a. AffVar e (Var a)
337+
takeQueue :: forall e a. Queue a -> AffQueue e a
339338
```
340339

341-
Makes a new asynchronous variable.
340+
Takes the next value from the asynchronous queue.
342341

343-
#### `makeVar'`
342+
#### `putQueue`
344343

345344
``` purescript
346-
makeVar' :: forall e a. a -> AffVar e (Var a)
345+
putQueue :: forall e a. Queue a -> a -> AffQueue e Unit
347346
```
348347

349-
Makes a variable and sets it to some value.
348+
Puts a new value into the asynchronous queue. If the queue has
349+
been killed, this will result in an error.
350350

351-
#### `takeVar`
351+
#### `modifyQueue`
352352

353353
``` purescript
354-
takeVar :: forall e a. Var a -> AffVar e a
354+
modifyQueue :: forall e a. (a -> a) -> Queue a -> AffQueue e Unit
355355
```
356356

357-
Takes the next value from the asynchronous variable.
357+
Modifies the value at the head of the queue (will suspend until one is available).
358358

359-
#### `putVar`
359+
#### `killQueue`
360360

361361
``` purescript
362-
putVar :: forall e a. Var a -> a -> AffVar e Unit
362+
killQueue :: forall e a. Queue a -> Error -> AffQueue e Unit
363363
```
364364

365-
Puts a new value into the asynchronous variable. If the variable has
366-
been killed, this will result in an error.
365+
Kills an asynchronous queue.
367366

368-
#### `modifyVar`
367+
368+
## Module Control.Monad.Aff.Unsafe
369+
370+
#### `unsafeTrace`
369371

370372
``` purescript
371-
modifyVar :: forall e a. (a -> a) -> Var a -> AffVar e Unit
373+
unsafeTrace :: forall e a. a -> Aff e Unit
372374
```
373375

374-
Modifies an asynchronous variable.
375376

376-
#### `killVar`
377+
#### `unsafeInterleaveAff`
377378

378379
``` purescript
379-
killVar :: forall e a. Var a -> Error -> AffVar e Unit
380-
```
381-
382-
Kills an asynchronous variable.
380+
unsafeInterleaveAff :: forall e1 e2 a. Aff e1 a -> Aff e2 a
381+
```

README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -162,18 +162,21 @@ Because Javascript is single-threaded, forking does not actually cause the
162162
computation to be run in a separate thread. Forking just allows the subsequent
163163
actions to execute without waiting for the forked computation to complete.
164164

165-
## Variables
165+
## Queues
166166

167-
The `Control.Monad.Aff.Var` module contains asynchronous variables. These can
167+
The `Control.Monad.Aff.Queue` module contains asynchronous queues. These can
168168
be used as low-level building blocks for asynchronous programs.
169169

170170
```purescript
171-
do v <- makeVar
172-
forkAff (later $ putVar v 1.0)
173-
a <- takeVar v
171+
do v <- makeQueue
172+
forkAff (later $ putQueue v 1.0)
173+
a <- takeQueue v
174174
liftEff $ trace ("Succeeded with " ++ show a)
175175
```
176176

177+
You can use these as queues which suspend (if necessary) on take operations, or
178+
as asynchronous variables (similar to Haskell's `MVar` construct).
179+
177180
## Parallel Execution
178181

179182
If you only need the power of `Applicative`, then instead of using the monadic `Aff`, you can use the `Par` newtype wrapper defined in `Control.Monad.Aff.Par`.

examples/src/Examples.purs

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module Examples where
44
import Data.Either(either)
55

66
import Control.Monad.Aff
7-
import Control.Monad.Aff.Var
7+
import Control.Monad.Aff.Queue
88
import Control.Monad.Aff.Par
99
import Control.Apply((*>))
1010
import Control.Alt(Alt, (<|>))
@@ -33,14 +33,14 @@ module Examples where
3333
""" :: forall e. Number -> Aff (time :: Time | e) Unit
3434

3535
type Test = forall e. Aff (trace :: Trace | e) Unit
36-
type TestVar = forall e. Aff (trace :: Trace, var :: VarFx | e) Unit
37-
type TestVarTime = forall e. Aff (trace :: Trace, var :: VarFx, time :: Time | e) Unit
36+
type TestQueue = forall e. Aff (trace :: Trace, queue :: QueueFx | e) Unit
37+
type TestQueueTime = forall e. Aff (trace :: Trace, queue :: QueueFx, time :: Time | e) Unit
3838

3939
test_sequencing :: forall e. Number -> Aff (trace :: Trace, time :: Time | e) Unit
4040
test_sequencing 0 = liftEff $ trace "Done"
4141
test_sequencing n = do
42-
timeout 1000
43-
liftEff $ trace (show n ++ " seconds left")
42+
timeout 100
43+
liftEff $ trace (show (n / 10) ++ " seconds left")
4444
test_sequencing (n - 1)
4545

4646
test_pure :: Test
@@ -60,26 +60,38 @@ module Examples where
6060
apathize $ throwError (error "Oh noes!")
6161
liftEff $ trace "Success: Exceptions don't stop the apathetic"
6262

63-
test_putTakeVar :: TestVar
64-
test_putTakeVar = do
65-
v <- makeVar
66-
forkAff (later $ putVar v 1.0)
67-
a <- takeVar v
63+
test_putTakeQueue :: TestQueue
64+
test_putTakeQueue = do
65+
v <- makeQueue
66+
forkAff (later $ putQueue v 1.0)
67+
a <- takeQueue v
6868
liftEff $ trace ("Success: Value " ++ show a)
6969

70-
test_killVar :: TestVar
71-
test_killVar = do
72-
v <- makeVar
73-
killVar v (error "DOA")
74-
e <- attempt $ takeVar v
75-
liftEff $ either (const $ trace "Success: Killed var dead") (const $ trace "Failure: Oh noes, Var survived!") e
70+
test_killQueue :: TestQueue
71+
test_killQueue = do
72+
v <- makeQueue
73+
killQueue v (error "DOA")
74+
e <- attempt $ takeQueue v
75+
liftEff $ either (const $ trace "Success: Killed queue dead") (const $ trace "Failure: Oh noes, queue survived!") e
7676

77-
test_parRace :: TestVarTime
77+
test_parRace :: TestQueueTime
7878
test_parRace = do
79-
s <- runPar $ (Par (timeout 100 *> pure "Success: Early bird got the worm") <|>
80-
Par (timeout 1000 *> pure "Failure: Late bird got the worm"))
79+
s <- runPar (Par (timeout 100 *> pure "Success: Early bird got the worm") <|>
80+
Par (timeout 200 *> pure "Failure: Late bird got the worm"))
8181
liftEff $ trace s
8282

83+
test_parRaceKill1 :: TestQueueTime
84+
test_parRaceKill1 = do
85+
s <- runPar (Par (timeout 100 *> throwError (error ("Oh noes!"))) <|>
86+
Par (timeout 200 *> pure "Success: Early error was ignored in favor of late success"))
87+
liftEff $ trace s
88+
89+
test_parRaceKill2 :: TestQueueTime
90+
test_parRaceKill2 = do
91+
e <- attempt $ runPar (Par (timeout 100 *> throwError (error ("Oh noes!"))) <|>
92+
Par (timeout 200 *> throwError (error ("Oh noes!"))))
93+
liftEff $ either (const $ trace "Success: Killing both kills it dead") (const $ trace "Failure: It's alive!!!") e
94+
8395
main = launchAff $ do
8496
liftEff $ trace "Testing sequencing"
8597
test_sequencing 3
@@ -96,11 +108,17 @@ module Examples where
96108
liftEff $ trace "Testing apathize"
97109
test_apathize
98110

99-
liftEff $ trace "Testing Var - putVar, takeVar"
100-
test_putTakeVar
111+
liftEff $ trace "Testing Queue - putQueue, takeQueue"
112+
test_putTakeQueue
101113

102-
liftEff $ trace "Testing killVar"
103-
test_killVar
114+
liftEff $ trace "Testing killQueue"
115+
test_killQueue
104116

105117
liftEff $ trace "Testing Par (<|>)"
106118
test_parRace
119+
120+
liftEff $ trace "Testing Par (<|>) - kill one"
121+
test_parRaceKill1
122+
123+
liftEff $ trace "Testing Par (<|>) - kill two"
124+
test_parRaceKill2

0 commit comments

Comments
 (0)