@@ -35,8 +35,12 @@ import Control.Bind.Indexed (class IxBind, ibind)
35import Control.Monad.Indexed (class IxMonad)
35import Control.Monad.Indexed (class IxMonad)
36import Data.Functor.Indexed (class IxFunctor)
36import Data.Functor.Indexed (class IxFunctor)
37import Data.Newtype (class Newtype, un)
37import Data.Newtype (class Newtype, un)
38+import Data.Either (Either(..))
39+import Data.Maybe (Maybe(..))
38import Data.Tuple.Nested ((/\), type (/\))
40import Data.Tuple.Nested ((/\), type (/\))
39import Effect (Effect)
41import Effect (Effect)
42+import Effect.Aff (error, killFiber, launchAff, launchAff_, try)
43+import Effect.Aff as Aff
40import Effect.Class (liftEffect)
44import Effect.Class (liftEffect)
41import Effect.Ref as Ref
45import Effect.Ref as Ref
42import Effect.Unsafe (unsafePerformEffect)
46import Effect.Unsafe (unsafePerformEffect)
@@ -123,15 +127,25 @@ data UseOm :: Row Type -> Type -> Type -> Type
123data UseOm ctx a hooks
127data UseOm ctx a hooks
126− :: forall @ctx hooks a
130+ :: forall ctx deps a hooks
127− . ({ | ctx } -> Effect a)
133+ -> Om { | ctx } () a
128− -> OmRender ctx hooks (UseOm ctx a hooks) a
134+ -> OmRender ctx hooks (UseOm ctx a hooks) (Maybe a)
129−useOm f = OmRender (unsafeHook go)
132− go = Prelude.do
135+useOm deps om = fromEffect Prelude.do
133− ctx :: { | ctx } <- unsafeCoerce (Ref.read ctxStash)
136+ ctx :: { | ctx } <- unsafeCoerce (Ref.read ctxStash)
134− f ctx
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
137 :: forall ctx state hooks
151 :: forall ctx state hooks