@@ -3,6 +3,9 @@
3-- @inline export note arity=2
3-- @inline export note arity=2
4-- @inline export fromAff arity=1
4-- @inline export fromAff arity=1
5-- @inline export runOm arity=2
5-- @inline export runOm arity=2
6+-- @inline export toOm arity=1
7+-- @inline export onError arity=2
8+-- @inline export mapError arity=2
6module Yoga.Om
9module Yoga.Om
8 , ParOm(..)
11 , ParOm(..)
@@ -13,6 +16,8 @@ module Yoga.Om
13 , expandErr
16 , expandErr
15 , handleErrors
18 , handleErrors
16 , handleErrors'
21 , handleErrors'
17 , launchOm
22 , launchOm
18 , launchOm_
23 , launchOm_
@@ -29,6 +34,8 @@ module Yoga.Om
30 , throwLeftAs
35 , throwLeftAs
31 , throwLeftAsM
36 , throwLeftAsM
32 , unliftAff
39 , unliftAff
33 , unliftAffFn
40 , unliftAffFn
34 , widenCtx
41 , widenCtx
@@ -52,6 +59,7 @@ import Data.Newtype (class Newtype)
52import Data.Time.Duration (class Duration, fromDuration)
59import Data.Time.Duration (class Duration, fromDuration)
53import Data.Tuple.Nested ((/\))
60import Data.Tuple.Nested ((/\))
54import Data.Variant (class VariantMatchCases, Variant, case_, match, on, onMatch)
61import Data.Variant (class VariantMatchCases, Variant, case_, match, on, onMatch)
62+import Data.Variant as Variant
55import Effect (Effect)
63import Effect (Effect)
56import Effect.AVar (AVar)
64import Effect.AVar (AVar)
57import Effect.Aff (Aff, Fiber, ParAff, forkAff, launchAff, supervise)
65import Effect.Aff (Aff, Fiber, ParAff, forkAff, launchAff, supervise)
@@ -60,7 +68,9 @@ import Effect.Aff.AVar as AVar
60import Effect.Aff.Class (class MonadAff, liftAff)
68import Effect.Aff.Class (class MonadAff, liftAff)
61import Effect.Class (class MonadEffect, liftEffect)
69import Effect.Class (class MonadEffect, liftEffect)
62import Effect.Exception (Error)
70import Effect.Exception (Error)
71+import Data.Symbol (class IsSymbol)
63−import Prim.Row (class Nub, class Union)
72+import Prim.Row (class Cons, class Nub, class Union)
73+import Type.Proxy (Proxy(..))
64import Prim.RowList (class RowToList, RowList)
74import Prim.RowList (class RowToList, RowList)
65import Record (disjointUnion)
75import Record (disjointUnion)
66import Record.Studio (class Keys, shrink)
76import Record.Studio (class Keys, shrink)
@@ -218,6 +228,30 @@ handleErrors cases (Om om) = do
218 err ← liftAff ((\(_ /\ v /\ _) → v) <$> (runRWSET ctx unit om))
228 err ← liftAff ((\(_ /\ v /\ _) → v) <$> (runRWSET ctx unit om))
219 err # either (onMatch cases throwError) pure
229 err # either (onMatch cases throwError) pure
232+ :: forall @from @to tyIn tyOut ctx errIn errMid errOut a
235+ => Cons from tyIn (exception :: Error | errMid) (exception :: Error | errIn)
236+ => Cons to tyOut (exception :: Error | errMid) (exception :: Error | errOut)
240+mapError f = handleErrors' \variant ->
241+ variant # on (Proxy :: Proxy from)
242+ (throwError <<< Variant.inj (Proxy :: Proxy to) <<< f)
243+ (throwError <<< unsafeCoerce)
246+ :: forall @label ty ctx errIn errOut a
248+ => Cons label ty (exception :: Error | errOut) (exception :: Error | errIn)
249+ => (ty -> Om ctx errOut a)
252+onError handler = handleErrors' \variant ->
253+ variant # on (Proxy :: Proxy label) handler throwError
221-- | When you have a function that can throw a subset of the errors of the ones
255-- | When you have a function that can throw a subset of the errors of the ones
222-- | you could throw in a do block, `expand` allows to make them compatible
256-- | you could throw in a do block, `expand` allows to make them compatible
@@ -347,6 +381,16 @@ runOm ctx errorHandlers (Om app) = do
347 (_ /\ Right r /\ _) → pure r
381 (_ /\ Right r /\ _) → pure r
348 (_ /\ Left err /\ _) → err # match errorHandlers
382 (_ /\ Left err /\ _) → err # match errorHandlers
384+class ToOm :: (Type -> Type) -> Constraint
386+ toOm :: forall ctx err a. f a -> Om ctx err a
388+instance ToOm Effect where
391+instance ToOm Aff where
350-- | Manually add some fields to the context of a computation.
394-- | Manually add some fields to the context of a computation.
351-- | This function is useful when already inside a `do` block of an `Om`
395-- | This function is useful when already inside a `do` block of an `Om`
352-- | and you want to call a function that requires some `ctx` that you obtained
396-- | and you want to call a function that requires some `ctx` that you obtained