feat: isLocked

This commit is contained in:
orion 2023-11-22 10:05:01 -06:00
parent fe21a2e7ab
commit 27cb8550ba
Signed by: orion
GPG Key ID: 6D4165AE4C928719

View File

@ -2,7 +2,8 @@ module Data.Async.Class where
import Prelude import Prelude
import Data.Maybe (Maybe) import Data.Foldable (for_)
import Data.Maybe (Maybe, isNothing)
import Effect.Aff.Class (class MonadAff) import Effect.Aff.Class (class MonadAff)
-- | Acquire a lock that guarantees access to mutable state -- | 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` has been invoked.
unlock :: forall async a. MonadAff async => m a -> lock a -> async Unit 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. -- | A lock type that supports writing data to the mutable state.
-- | -- |
-- | Writable locks must also be Readable. -- | Writable locks must also be Readable.