Does lwt provide unbounded channels?

If not, what is the closest thing it provides ?

  1. I am looking at Lwt manual and can not find an obvious match.

  2. I am looking for something with approximately three functions:

create: 'a chan
push: 'a chan -> 'a -> unit
get: 'a chan -> 'a Lwt.t

create/push always succeeds
get: if there is an elem, return it; otherwise, suspend current Lwt green thread, waking when element is pushed to channel

XY problem: converting JS .on_message hook to channel like API

Does channels here bear any ressemble to stdlib channels?
If not, then I think Module Lwt_stream is what you are looking for, isn’t it?

let chan, push = Lwt_stream.create ();;
let () = push (Some x);;
let delayed_x = Lwt_stream.get chan;;

I believe Lwt_stream can be bounded, but aren’t by default.


I think you are right.

I saw the from / from_direct constructors … and moved on.

One more line and i would have seen the create constructor. :slight_smile:

There’s also Lwt_mvar which has a similar API:

  • val create_empty : unit -> 'a t
  • val put : 'a t -> 'a -> unit Lwt.t
  • val take : 'a t -> 'a Lwt.t

The main difference here being that put will block if there’s already a value in the “mailbox”, which is why it returns a unit Lwt.t.

1 Like