git.iko.soy/rowtype-yoga/purescript-yoga-react-om
on main help sign in
2 files changed+43−9
since splitsnapshots
unifiedsplit
README.md+1 src/Yoga/React/Om.purs+42−9
README.mdnew file+1
@@ -0,0 +1 @@
1+
src/Yoga/React/Om.purs+42−9
@@ -3,8 +3,10 @@ module Yoga.React.Om
33 , omComponent
44 , useCtx
55 , useOm
6+ , useEffectOm
67 , UseCtx
78 , UseOm
9+ , UseEffectOm
810 , liftRender
911 , bind
1012 , discard
@@ -35,8 +37,12 @@ import Control.Bind.Indexed (class IxBind, ibind)
3537import Control.Monad.Indexed (class IxMonad)
3638import Data.Functor.Indexed (class IxFunctor)
3739import Data.Newtype (class Newtype, un)
40+import Data.Either (Either(..))
41+import Data.Maybe (Maybe(..))
3842import Data.Tuple.Nested ((/\), type (/\))
3943import Effect (Effect)
44+import Effect.Aff (error, killFiber, launchAff, launchAff_, try)
45+import Effect.Aff as Aff
4046import Effect.Class (liftEffect)
4147import Effect.Ref as Ref
4248import Effect.Unsafe (unsafePerformEffect)
@@ -123,15 +129,42 @@ data UseOm :: Row Type -> Type -> Type -> Type
123129data UseOm ctx a hooks
124130
125131useOm
126− :: forall @ctx hooks a
127− . ({ | ctx } -> Effect a)
128− -> OmRender ctx hooks (UseOm ctx a hooks) a
129−useOm f = OmRender (unsafeHook go)
130− where
131− go :: Effect a
132− go = Prelude.do
133− ctx :: { | ctx } <- unsafeCoerce (Ref.read ctxStash)
134− f ctx
132+ :: forall ctx deps a hooks
133+ . Eq deps
134+ => deps
135+ -> Om { | ctx } () a
136+ -> OmRender ctx hooks (UseOm ctx a hooks) (Maybe a)
137+useOm deps om = fromEffect Prelude.do
138+ ctx :: { | ctx } <- unsafeCoerce (Ref.read ctxStash)
139+ result /\ setResult <- toEffect (useState Nothing)
140+ toEffect $ useEffect deps Prelude.do
141+ setResult (const Nothing)
142+ fiber <- launchAff Prelude.do
143+ r <- try (Om.runOm ctx { exception: \e -> Aff.throwError e } om)
144+ liftEffect (setResult \_ -> Just r)
145+ Prelude.pure Prelude.do
146+ launchAff_ (killFiber (error "Stale request cancelled") fiber)
147+ toEffect $ liftRender $ unsafeRenderEffect case result of
148+ Just (Left err) -> Aff.throwError err
149+ Just (Right a) -> Prelude.pure (Just a)
150+ Nothing -> Prelude.pure Nothing
151+
152+data UseEffectOm :: Row Type -> Type -> Type
153+data UseEffectOm ctx hooks
154+
155+useEffectOm
156+ :: forall ctx deps hooks
157+ . Eq deps
158+ => deps
159+ -> Om { | ctx } () Unit
160+ -> OmRender ctx hooks (UseEffectOm ctx hooks) Unit
161+useEffectOm deps om = fromEffect Prelude.do
162+ ctx :: { | ctx } <- unsafeCoerce (Ref.read ctxStash)
163+ toEffect $ useEffect deps Prelude.do
164+ fiber <- launchAff Prelude.do
165+ Om.runOm ctx { exception: \_ -> Prelude.pure unit } om
166+ Prelude.pure Prelude.do
167+ launchAff_ (killFiber (error "Stale request cancelled") fiber)
135168
136169useState
137170 :: forall ctx state hooks