@@ -35,8 +35,12 @@ import Control.Bind.Indexed (class IxBind, ibind)
3535import Control.Monad.Indexed (class IxMonad)
3636import Data.Functor.Indexed (class IxFunctor)
3737import Data.Newtype (class Newtype, un)
38+import Data.Either (Either(..))
39+import Data.Maybe (Maybe(..))
3840import Data.Tuple.Nested ((/\), type (/\))
3941import Effect (Effect)
42+import Effect.Aff (error, killFiber, launchAff, launchAff_, try)
43+import Effect.Aff as Aff
4044import Effect.Class (liftEffect)
4145import Effect.Ref as Ref
4246import Effect.Unsafe (unsafePerformEffect)
@@ -123,15 +127,25 @@ data UseOm :: Row Type -> Type -> Type -> Type
123127data UseOm ctx a hooks
124128
125129useOm
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
130+ :: forall ctx deps a hooks
131+ . Eq deps
132+ => deps
133+ -> Om { | ctx } () a
134+ -> OmRender ctx hooks (UseOm ctx a hooks) (Maybe a)
135+useOm deps om = fromEffect Prelude.do
136+ ctx :: { | ctx } <- unsafeCoerce (Ref.read ctxStash)
137+ result /\ setResult <- toEffect (useState Nothing)
138+ toEffect $ useEffect deps Prelude.do
139+ setResult (const Nothing)
140+ fiber <- launchAff Prelude.do
141+ r <- try (Om.runOm ctx { exception: \e -> Aff.throwError e } om)
142+ liftEffect (setResult \_ -> Just r)
143+ Prelude.pure Prelude.do
144+ launchAff_ (killFiber (error "Stale request cancelled") fiber)
145+ toEffect $ liftRender $ unsafeRenderEffect case result of
146+ Just (Left err) -> Aff.throwError err
147+ Just (Right a) -> Prelude.pure (Just a)
148+ Nothing -> Prelude.pure Nothing
135149
136150useState
137151 :: forall ctx state hooks