std/async/interleave▲toc

Asynchronous interleavings.

Various forms of interleaving for asynchronous operations.

fun list/firstof-any( x : () -> <asyncstd/async/async/async: E,ndetstd/core/types/ndet: X|e> a, xs : liststd/core/types/list: V -> V<() -> <asyncstd/async/async/async: E,ndetstd/core/types/ndet: X|e> a> ) : <asyncstd/async/async/async: E,ndetstd/core/types/ndet: X|e> a

Interleave a list of actions [x]++xs and return the result of the first action that completes (and cancel all others). todo: we need a list1 type.

fun list/interleaved( xs : liststd/core/types/list: V -> V<() -> <asyncstd/async/async/async: E|e> a> ) : <asyncstd/async/async/async: E|e> liststd/core/types/list: V -> V<a>

Interleave a list of actions. If any raises an exception, cancels, or finalizes, propagate that and cancel all others.

fun two/firstof( action1 : () -> <asyncstd/async/async/async: E,ndetstd/core/types/ndet: X|e> a, action2 : () -> <asyncstd/async/async/async: E,ndetstd/core/types/ndet: X|e> a ) : <asyncstd/async/async/async: E,ndetstd/core/types/ndet: X|e> a

Execute a and b interleaved. As soon as one of them finishes, cancel the other one and return the result of finished action.

fun two/interleaved( action1 : () -> <asyncstd/async/async/async: E|e> a, action2 : () -> <asyncstd/async/async/async: E|e> b ) : <asyncstd/async/async/async: E|e> (a, b)

Interleave two actions.

fun timeout( secs : durationstd/time/duration/duration: V, action : () -> <asyncstd/async/async/async: E,ndetstd/core/types/ndet: X|e> a ) : <asyncstd/async/async/async: E,ndetstd/core/types/ndet: X|e> maybestd/core/types/maybe: V -> V<a>

Execute action but if it is not finished within secs seconds duration cancel it (and return Nothingstd/core/types/Nothing: forall<a> maybe<a>). Due to the generality of cancel, this timeoutstd/async/interleave/timeout: forall<a,e> (secs : duration, action : () -> <async,ndet|e> a) -> <async,ndet|e> maybe<a> abstraction can reliably time out over any composition of asynchronous operations and is therefore quite expressive.

private import std/core/typesstd/core/types, std/core/hndstd/core/hnd, std/core/exnstd/core/exn, std/core/boolstd/core/bool, std/core/orderstd/core/order, std/core/charstd/core/char, std/core/intstd/core/int, std/core/vectorstd/core/vector, std/core/bytesstd/core/bytes, std/core/bslicestd/core/bslice, std/core/stringstd/core/string, std/core/sslicestd/core/sslice, std/core/liststd/core/list, std/core/maybestd/core/maybe, std/core/maybe2std/core/maybe2, std/core/eitherstd/core/either, std/core/resultstd/core/result, std/core/tuplestd/core/tuple, std/core/lazystd/core/lazy, std/core/showstd/core/show, std/core/debugstd/core/debug, std/core/delayedstd/core/delayed, std/core/consolestd/core/console, std/corestd/core, std/time/durationstd/time/duration, std/core/unsafestd/core/unsafe, std/core/undivstd/core/undiv, std/async/asyncstd/async/async, std/async/channelstd/async/channel, std/async/internalstd/async/internal