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.