Skip to content

Commit 46749af

Browse files
authored
Merge pull request #119 from natefaubion/avar-updates
AVar updates
2 parents 9715ea7 + 104129b commit 46749af

File tree

4 files changed

+33
-17
lines changed

4 files changed

+33
-17
lines changed

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ The `Control.Monad.Aff.AVar` module contains asynchronous variables, which
212212
are very similar to Haskell's `MVar`.
213213

214214
`AVar`s represent a value that is either full or empty. Calling `takeVar` on
215-
an empty `AVar` will queue until it is filled by a matching `putVar`.
215+
an empty `AVar` will queue until it is filled by a `putVar`.
216216

217217
```purescript
218218
example = do
@@ -230,22 +230,23 @@ example = do
230230
> Got a value: hello
231231
```
232232

233-
Likewise, calling `putVar` will queue until it is taken:
233+
Likewise, calling `putVar` on a filled `AVar` will queue until it is emptied by
234+
a `takeVar`.
234235

235236
```purescript
236237
example = do
237-
var <- makeEmptyVar
238+
var <- makeVar "hello"
238239
_ <- forkAff do
239240
delay (Milliseconds 100.0)
240241
value <- takeVar var
241242
log $ "Got a value: " <> value
242-
putVar var "hello"
243-
log "Value taken"
243+
putVar var "next"
244+
log "Value put"
244245
```
245246
```
246247
(Waits 100ms)
247-
> Value taken
248248
> Got a value: hello
249+
> Value put
249250
```
250251

251252
These combinators (and a few more) can be used as the building blocks for

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"purescript-free": "^4.0.1",
2828
"purescript-st": "^3.0.0",
2929
"purescript-type-equality": "^2.1.0",
30-
"purescript-avar": "^1.0.1"
30+
"purescript-avar": "^2.0.0"
3131
},
3232
"devDependencies": {
3333
"purescript-partial": "^1.2.0",

src/Control/Monad/Aff/AVar.purs

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ module Control.Monad.Aff.AVar
22
( module Control.Monad.Eff.AVar
33
, makeVar
44
, makeEmptyVar
5+
, status
56
, isEmptyVar
7+
, isFilledVar
8+
, isKilledVar
69
, takeVar
710
, tryTakeVar
811
, putVar
@@ -15,7 +18,7 @@ module Control.Monad.Aff.AVar
1518
import Prelude
1619
import Control.Monad.Aff (Aff, Canceler(..), makeAff)
1720
import Control.Monad.Eff (Eff)
18-
import Control.Monad.Eff.AVar (AVar, AVAR)
21+
import Control.Monad.Eff.AVar (AVar, AVAR, AVarStatus(..), isEmpty, isFilled, isKilled)
1922
import Control.Monad.Eff.AVar as AVar
2023
import Control.Monad.Eff.Class (liftEff)
2124
import Control.Monad.Eff.Exception (Error)
@@ -32,10 +35,22 @@ makeVar = liftEff <<< AVar.makeVar
3235
makeEmptyVar eff a. Aff (avar AVAR | eff) (AVar a)
3336
makeEmptyVar = liftEff AVar.makeEmptyVar
3437

35-
-- | Synchronously checks whether an AVar currently has a value.
38+
-- | Synchronously checks the status of an AVar.
39+
status eff a. AVar a Aff (avar AVAR | eff) (AVar.AVarStatus a)
40+
status = liftEff <<< AVar.status
41+
42+
-- | Synchronously checks whether an AVar currently is empty.
3643
isEmptyVar eff a. AVar a Aff (avar AVAR | eff) Boolean
3744
isEmptyVar = liftEff <<< AVar.isEmptyVar
3845

46+
-- | Synchronously checks whether an AVar currently has a value.
47+
isFilledVar eff a. AVar a Aff (avar AVAR | eff) Boolean
48+
isFilledVar = liftEff <<< AVar.isFilledVar
49+
50+
-- | Synchronously checks whether an AVar has been killed.
51+
isKilledVar eff a. AVar a Aff (avar AVAR | eff) Boolean
52+
isKilledVar = liftEff <<< AVar.isKilledVar
53+
3954
-- | Takes the AVar value, leaving it empty. If the AVar is already empty,
4055
-- | the callback will be queued until the AVar is filled. Multiple takes will
4156
-- | resolve in order as the AVar fills.
@@ -52,15 +67,15 @@ tryTakeVar = liftEff <<< AVar.tryTakeVar
5267
-- | Sets the value of the AVar. If the AVar is already filled, it will be
5368
-- | queued until the value is emptied. Multiple puts will resolve in order as
5469
-- | the AVar becomes available.
55-
putVar eff a. AVar a a Aff (avar AVAR | eff) Unit
56-
putVar avar value = makeAff \k → do
57-
c ← AVar.putVar avar value k
70+
putVar eff a. a AVar a Aff (avar AVAR | eff) Unit
71+
putVar value avar = makeAff \k → do
72+
c ← AVar.putVar value avar k
5873
pure (toCanceler c)
5974

6075
-- | Attempts to synchronously fill an AVar. If the AVar is already filled,
6176
-- | this will do nothing. Returns true or false depending on if it succeeded.
62-
tryPutVar eff a. AVar a a Aff (avar AVAR | eff) Boolean
63-
tryPutVar avar = liftEff <<< AVar.tryPutVar avar
77+
tryPutVar eff a. a AVar a Aff (avar AVAR | eff) Boolean
78+
tryPutVar value = liftEff <<< AVar.tryPutVar value
6479

6580
-- | Reads the AVar value. Unlike `takeVar`, this will not leave the AVar empty.
6681
-- | If the AVar is empty, this will queue until it is filled. Multiple reads
@@ -77,5 +92,5 @@ tryReadVar = liftEff <<< AVar.tryReadVar
7792

7893
-- | Kills the AVar with an exception. All pending and future actions will
7994
-- | resolve immediately with the provided exception.
80-
killVar eff a. AVar a Error Aff (avar AVAR | eff) Unit
81-
killVar avar = liftEff <<< AVar.killVar avar
95+
killVar eff a. Error AVar a Aff (avar AVAR | eff) Unit
96+
killVar error = liftEff <<< AVar.killVar error

test/Test/Main.purs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ test_avar_order = assert "avar/order" do
556556
delay (Milliseconds 10.0)
557557
value ← takeVar var
558558
modifyRef ref (_ <> value)
559-
putVar var "foo"
559+
putVar "foo" var
560560
modifyRef ref (_ <> "taken")
561561
joinFiber f1
562562
eq "takenfoo" <$> readRef ref

0 commit comments

Comments
 (0)