Yoga.Om
- Package
- purescript-yoga-om
- Repository
- rowtype-yoga/purescript-yoga-om
#Om Source
newtype Om :: Type -> Row Type -> Type -> Type
newtype Om ctx err a
A generic type for handling computations
It combines asynchronous side-effecting computations with Aff
,
"dependency injection" via ReaderT
which tracks a context, and
checked exceptions and early return with ExceptV
.
Constructors
Instances
Newtype (Om ctx err a) _
Functor (Om ctx err)
Applicative (Om ctx err)
Apply (Om ctx err)
Bind (Om ctx err)
Monad (Om ctx err)
MonadEffect (Om ctx err)
MonadAff (Om ctx err)
MonadAsk ctx (Om ctx err)
MonadReader ctx (Om ctx err)
MonadThrow (Variant (Exception err)) (Om ctx err)
MonadError (Variant (Exception err)) (Om ctx err)
MonadRec (Om ctx err)
(Semigroup a) => Semigroup (Om ctx err a)
Alt (Om ctx err)
Plus (Om ctx err)
Parallel (ParOm ctx err) (Om ctx err)
#ParOm Source
#error Source
error :: forall err errors. SingletonVariantRecord err errors => Record err -> OneOfTheseErrors errors
#expandCtx Source
expandCtx :: forall lt more gt err. Union lt more gt => Keys lt => (Om (Record lt) err) ~> (Om (Record gt) err)
When you have a function that requires a closed subset of the context of
the one you're operating in, expandCtx
allows to call this function in a
do block, e.g.:
let
otherComp :: Om { a :: A } _ _
otherComp = ...
myComp :: Om { a :: A, b :: B } _ _
myComp = do
...
otherComp # expandCtx
#handleErrors Source
handleErrors :: forall ctx errIn errOut a (handlersRL :: RowList Type) (handlers :: Row Type) (handled :: Row Type). RowToList handlers handlersRL => VariantMatchCases handlersRL handled (Om ctx errOut a) => Union handled (exception :: Error | errOut) (exception :: Error | errIn) => Record handlers -> Om ctx errIn a -> Om ctx errOut a
#note Source
note :: forall m err errors a. SingletonVariantRecord err errors => MonadThrow (OneOfTheseErrors errors) m => Record err -> Maybe a -> m a
Turns a Nothing
into an exception which reduces nesting and encourages
tracking what actually went wrong
e.g.
do
let maybeCached = Object.lookup key cache :: Maybe a
cached :: a <- maybeCached # Om.note (unexpectedCacheMiss key)
#noteM Source
noteM :: forall err m a. MonadThrow err m => m err -> Maybe a -> m a
#inParallel Source
inParallel :: forall ctx e a. Array (Om ctx e a) -> Om ctx e (Array a)
Run multiple Om
s in parallel, and collect all results
To only take the fastest result, use race
#runOm Source
runOm :: forall ctx r rl err_ err a. RowToList (exception :: Error -> Aff a | r) rl => VariantMatchCases rl err_ (Aff a) => Union err_ () (Exception err) => ctx -> { exception :: Error -> Aff a | r } -> Om ctx err a -> Aff a
Invoke this function in the end and handle all possible errors, e.g.
runOm { token: "123" } { someError: \e -> handleTheError e } someApp
#throw Source
throw :: forall m err errors a. SingletonVariantRecord err errors => MonadThrow (OneOfTheseErrors errors) m => Record err -> m a
#throwLeftAs Source
throwLeftAs :: forall err m left right. MonadThrow err m => (left -> err) -> Either left right -> m right
Turns a Left a
into an exception which reduces nesting
#throwLeftAsM Source
throwLeftAsM :: forall err m left right. MonadThrow err m => (left -> m err) -> Either left right -> m right
Turns a Left a
into an exception which reduces nesting
#unliftAff Source
unliftAff :: forall ctx err otherErrors a. Om ctx err a -> Om ctx otherErrors (Aff (Either (Variant (Exception err)) a))
Useful when constructing a parameterless callback of type Aff
or
Effect
within the context of an Om computation
recordAndUploadDOM ∷ Aff _ <- do
Om.unliftAff $ serialiseDOM >>= uploadDOMSnapshot
#unliftAffFn Source
unliftAffFn :: forall ctx arg errors a. (arg -> Om ctx () a) -> Om ctx errors (arg -> Aff a)
Useful when constructing a callback of type Aff
or Effect
within the
context of an Om computation
uploadDOM ∷ (SerialisedDOM -> Aff _) <- do
Om.unliftAffFn $ uploadDOMSnapshot
#widenCtx Source
widenCtx :: forall err additionalCtx ctx widerCtx. Union additionalCtx ctx widerCtx => Nub widerCtx widerCtx => Record additionalCtx -> (Om (Record widerCtx) err) ~> (Om (Record ctx) err)
Manually add some fields to the context of a computation.
This function is useful when already inside a do
block of an Om
and you want to call a function that requires some ctx
that you obtained
within this do
block. For example:
do
moreCtx <- getSomeData :: Om {} _
getMoreData :: Om { moreCtx :: _ } _ # widenCtx { moreCtx }
Re-exports from Control.Monad.Reader.Trans
- Modules
- Yoga.
Om - Yoga.
Om. Error