def IO.within

Base · base.bend:295

import Base
295# Some{x} if act answers x within ms milliseconds, else None{}. The loser
296# is not cancelled: it runs to its end before the program exits.
297def IO.within(-A: Type, ms: U32, act: IO(A)) -> IO(Maybe<&1, A>):
298  won = IO.bind(A, Maybe<&1, A>, act, x => IO.pure(Maybe<&1, A>, Some{x}))
299  late = IO.bind(Unit, Maybe<&1, A>, IO.sleep(ms), u => IO.pure(Maybe<&1, A>, None{}))
300  both = IO.bind(Chan(Maybe<&1, A>), Chan(Maybe<&1, A>), IO.fork(Maybe<&1, A>, won),
301    IO.fork.go(Maybe<&1, A>, late))
302  IO.bind(Chan(Maybe<&1, A>), Maybe<&1, A>, both, c => IO.join(Maybe<&1, A>, c))

Uses (11)

Used by (1)