From 27cb8550ba5aad45f4486f9893facf16b3923289 Mon Sep 17 00:00:00 2001 From: Orion Kindel Date: Wed, 22 Nov 2023 10:05:01 -0600 Subject: [PATCH] feat: isLocked --- src/Data.Async.Class.purs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/Data.Async.Class.purs b/src/Data.Async.Class.purs index 2acb98d..9a8fea3 100644 --- a/src/Data.Async.Class.purs +++ b/src/Data.Async.Class.purs @@ -2,7 +2,8 @@ module Data.Async.Class where import Prelude -import Data.Maybe (Maybe) +import Data.Foldable (for_) +import Data.Maybe (Maybe, isNothing) import Effect.Aff.Class (class MonadAff) -- | Acquire a lock that guarantees access to mutable state @@ -32,6 +33,13 @@ class AsyncStateLock m lock | lock -> m where -- | `unlock` has been invoked. unlock :: forall async a. MonadAff async => m a -> lock a -> async Unit +-- | Returns whether a lock is currently held +isLocked :: forall async a l g. MonadAff async => AsyncStateLock l g => l a -> async Boolean +isLocked l = do + guard :: Maybe (g a) <- tryLock l + for_ guard (unlock l) + pure $ isNothing guard + -- | A lock type that supports writing data to the mutable state. -- | -- | Writable locks must also be Readable.