Skip to content

Commit 877321c

Browse files
committed
Add AVar.modify helper function
1 parent 2f3a4ee commit 877321c

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/Internal/Lib/AVar.purs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module HydraSdk.Internal.Lib.AVar
2+
( modify
3+
) where
4+
5+
import Prelude
6+
7+
import Control.Monad.Error.Class (class MonadError, catchError, throwError)
8+
import Effect.AVar (AVar)
9+
import Effect.Aff.AVar (put, take) as AVar
10+
import Effect.Aff.Class (class MonadAff, liftAff)
11+
12+
modify
13+
:: forall (m :: Type -> Type) (e :: Type) (a :: Type)
14+
. MonadAff m
15+
=> MonadError e m
16+
=> (a -> m a)
17+
-> AVar a
18+
-> m a
19+
modify f avar = do
20+
prev <- liftAff $ AVar.take avar
21+
new <- catchError (f prev) \err -> liftAff (AVar.put prev avar) *> throwError err
22+
liftAff $ AVar.put new avar
23+
pure new

src/Lib.purs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
module HydraSdk.Lib
2-
( module ExportCodec
2+
( module ExportAVar
3+
, module ExportCodec
34
) where
45

6+
import HydraSdk.Internal.Lib.AVar (modify) as ExportAVar
7+
58
import HydraSdk.Internal.Lib.Codec
69
( class FromVariantGeneric
710
, class ToVariantGeneric

0 commit comments

Comments
 (0)