Module

Yoga.Om

Package
purescript-yoga-om
Repository
rowtype-yoga/purescript-yoga-om

#Om Source

newtype Om :: Type -> Row Type -> Type -> Typenewtype 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

#ParOm Source

newtype ParOm :: Type -> Row Type -> Type -> Typenewtype ParOm ctx err a

Constructors

Instances

#delay Source

delay :: forall m d. MonadAff m => Duration d => d -> m Unit

#error Source

error :: forall err errors. SingletonVariantRecord err errors => Record err -> OneOfTheseErrors errors

#expand Source

expand :: forall eSmall e_ eLarge a cSmall c_ cLarge. Union eSmall e_ eLarge => Union cSmall c_ cLarge => Keys cSmall => Om (Record cSmall) eSmall a -> Om (Record cLarge) eLarge a

#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

#expandErr Source

expandErr :: forall ctx lt more gt. Union lt more gt => (Om ctx lt) ~> (Om ctx gt)

When you have a function that can throw a subset of the errors of the ones you could throw in a do block, expand allows to make them compatible

#fromAff Source

fromAff :: forall ctx a err. Aff a -> Om ctx err a

Turn any Aff into an Om. Because Aff can throw exceptions of type Error, a possible such exception is captured in the exception branch of the error variant

#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

#launchOm Source

launchOm :: 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 -> Effect (Fiber a)

Launches an error free Om

#launchOm_ Source

launchOm_ :: forall ctx r rl err_ err. RowToList (exception :: Error -> Aff Unit | r) rl => VariantMatchCases rl err_ (Aff Unit) => Union err_ () (Exception err) => ctx -> { exception :: Error -> Aff Unit | r } -> Om ctx err Unit -> Effect Unit

Launches an error free Om

#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

#parOmToAff Source

parOmToAff :: forall ctx err a. ctx -> ParOm ctx err a -> Aff (Either (Variant (exception :: Error | err)) a)

#race Source

race :: forall ctx e a. Array (Om ctx e a) -> Om ctx e a

Run multiple Om computations in parallel, and return the fastest To collect all results, use inParallel instead

#inParallel Source

inParallel :: forall ctx e a. Array (Om ctx e a) -> Om ctx e (Array a)

Run multiple Oms in parallel, and collect all results To only take the fastest result, use race

#readerT Source

readerT :: forall m err r a. Functor m => (r -> m (Either (Variant err) a)) -> RWSET r Unit Unit (Variant err) m a

#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

#runReader Source

runReader :: forall ctx err a. ctx -> Om ctx err a -> Aff (Either (Variant (Exception err)) a)

Invoke this function in the end after handling all possible errors, e.g.

runOm_ { token: "123" } someApp

Removes the ReaderT part and makes the errors explicit

runReader { token: "123" } 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

#ask Source

ask :: forall m r. MonadAsk r m => m r

#asks Source

asks :: forall r m a. MonadAsk r m => (r -> a) -> m a

Projects a value from the global context in a MonadAsk.