Skip to content

Commit 65f64f0

Browse files
committed
Comments
1 parent 828ed99 commit 65f64f0

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

src/Control/Monad/Eff/AVar.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,11 +260,9 @@ function drainVar (left, right, avar) {
260260
if (p = takeHead(ps)) {
261261
runEff(p.cb(value));
262262
}
263-
264263
while (r = takeHead(rs)) {
265264
runEff(r(value));
266265
}
267-
268266
if (t = takeHead(ts)) {
269267
runEff(t(value));
270268
}

src/Control/Monad/Eff/AVar.purs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,30 +30,52 @@ foreign import data AVar ∷ Type → Type
3030

3131
foreign import data AVAREffect
3232

33+
-- | Creates a fresh AVar.
3334
foreign import makeEmptyVar eff a. AVarEff eff (AVar a)
3435

36+
-- | Creates a fresh AVar with an initial value.
3537
foreign import makeVar eff a. a AVarEff eff (AVar a)
3638

39+
-- | Synchronously checks whether an AVar currently has a value.
3740
foreign import isEmptyVar eff a. AVar a AVarEff eff Boolean
3841

42+
-- | Kills the AVar with an exception. All pending and future actions will
43+
-- | resolve immediately with the provided exception.
3944
killVar eff a. AVar a Error AVarEff eff Unit
4045
killVar avar err = Fn.runFn4 _killVar Left Right avar err
4146

47+
-- | Sets the value of the AVar. If the AVar is already filled, it will be
48+
-- | queued until the value is emptied. Multiple puts will resolve in order as
49+
-- | the AVar becomes available. Returns an effect which will remove the
50+
-- | callback from the pending queue.
4251
putVar eff a. AVar a a AVarCallback eff Unit AVarEff eff (AVarEff eff Unit)
4352
putVar avar value cb = Fn.runFn5 _putVar Left Right avar value cb
4453

54+
-- | Attempts to synchronously fill an AVar. If the AVar is already filled,
55+
-- | this will do nothing. Returns true or false depending on if it succeeded.
4556
tryPutVar eff a. AVar a a AVarEff eff Boolean
4657
tryPutVar avar value = Fn.runFn4 _tryPutVar Left Right avar value
4758

59+
-- | Takes the AVar value, leaving it empty. If the AVar is already empty,
60+
-- | the callback will be queued until the AVar is filled. Multiple takes will
61+
-- | resolve in order as the AVar fills. Returns an effect which will remove
62+
-- | the callback from the pending queue.
4863
takeVar eff a. AVar a AVarCallback eff a AVarEff eff (AVarEff eff Unit)
4964
takeVar avar cb = Fn.runFn4 _takeVar Left Right avar cb
5065

66+
-- | Attempts to synchronously take an AVar value, leaving it empty. If the
67+
-- | AVar is empty, this will return `Nothing`.
5168
tryTakeVar eff a. AVar a AVarEff eff (Maybe a)
5269
tryTakeVar avar = Fn.runFn5 _tryTakeVar Left Right Nothing Just avar
5370

71+
-- | Reads the AVar value. Unlike `takeVar`, this will not leave the AVar empty.
72+
-- | If the AVar is empty, this will queue until it is filled. Multiple reads
73+
-- | will resolve at the same time, as soon as possible.
5474
readVar eff a. AVar a AVarCallback eff a AVarEff eff (AVarEff eff Unit)
5575
readVar avar cb = Fn.runFn4 _readVar Left Right avar cb
5676

77+
-- | Attempts to synchronously read an AVar. If the AVar is empty, this will
78+
-- | return `Nothing`.
5779
tryReadVar eff a. AVar a AVarEff eff (Maybe a)
5880
tryReadVar avar = Fn.runFn3 _tryReadVar Nothing Just avar
5981

test/Main.purs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ test_cancel = test "cancel" do
169169
c1 ← putVar v1 "a" $ traverse_ \_ → modifyRef ref (_ <> "a")
170170
c2 ← putVar v1 "b" $ traverse_ \_ → modifyRef ref (_ <> "b")
171171
c3 ← putVar v1 "c" $ traverse_ \_ → modifyRef ref (_ <> "c")
172+
c1
172173
c2
173174
_ ← tryTakeVar v1
174175
_ ← tryTakeVar v1
@@ -186,8 +187,9 @@ test_cancel = test "cancel" do
186187
c8 ← readVar v3 $ traverse_ \_ → modifyRef ref (_ <> "h")
187188
c9 ← readVar v3 $ traverse_ \_ → modifyRef ref (_ <> "i")
188189
c8
190+
c9
189191
_ ← tryPutVar v3 "a"
190-
eq "acdfgi" <$> readRef ref
192+
eq "cdfg" <$> readRef ref
191193

192194
main TestEff Unit
193195
main = do

0 commit comments

Comments
 (0)