Skip to content

Instantly share code, notes, and snippets.

@jneira
Created February 8, 2015 20:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jneira/4135e0c99cce29cdee2a to your computer and use it in GitHub Desktop.
Save jneira/4135e0c99cce29cdee2a to your computer and use it in GitHub Desktop.
foreign import data Timeout :: !
type Milliseconds = Number
type WithTimeout eff = Eff (t :: Timeout | eff)
type ContTimeout eff = ContT Unit (WithTimeout eff)
setTimeoutCont :: forall eff. Milliseconds -> ContTimeout eff Unit
setTimeoutCont = ContT <<< setTimeout
type WithRef eff = Eff (ref :: Ref | eff)
type ContRef eff = ContT Unit (WithRef eff)
race :: forall a eff. ContRef eff a -> ContRef eff a -> ContRef eff a
race ca cb = ContT $ \k -> do
rflag <- newRef false
let run c = runContT c $ \a -> do
flag <- readRef rflag
unless flag $ writeRef rflag true *> k a
run ca *> run cb
timeout :: forall a eff. Milliseconds -> ContRef eff a -> ContRef eff (Maybe a)
timeout t c = race (Just <$> c)
(setTimeoutCont t *> return Nothing)
{-
Error in declaration timeout
Cannot unify (t :: Network.HTTP.Client.Timeout | u13977) with (ref :: Control.Monad.Eff.Ref.Ref | eff13964)
Use --force to continue.
The type inferred by compiler for timeout is:
> :t Network.HTTP.Client.timeout
Compiling Network.HTTP.Client
forall t184 t198. Prim.Number -> Control.Monad.Cont.Trans.ContT Prelude.Unit
(Control.Monad.Eff.Eff (t :: Network.HTTP.Client.Timeout,
ref :: Control.Monad.Eff.Ref.Ref | t198)) t184 ->
Network.HTTP.Client.ContRef (t :: Network.HTTP.Client.Timeout | t198) (Data.Maybe.Maybe t184)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment