    [promesa.protocols :as pt]))
 
 (defmacro go
+  "Schedules the body to be executed asychronously, potentially using
+  virtual thread if available (a normal thread will be used in other
+  case). Returns a promise instance that resolves with the return
+  value when the asynchronous block finishes."
   [& body]
   `(->> (px/wrap-bindings (fn [] ~@body))
         (p/thread-call channel/*executor*)))
 
 (defmacro go-loop
+  "A convencience helper macro that combines go + loop."
   [bindings & body]
   `(go (loop ~bindings ~@body)))
 
 (defn chan
+  "Creates a new channel instance, it optionally accepts buffer,
+  transducer and error handler."
   ([] (chan nil nil nil))
   ([buf] (chan buf nil nil))
   ([buf xf] (chan buf xf nil))

  targeted to the JVM, where you will be able to take advantage of
  virtual threads and seamless blocking operations on channels.

  EXPERIMENTAL API"

  (:require
   [promesa.core :as p]

    ret))

(defn close!
  "Close the channel."
  [port]
  (pt/-close! port)
  nil)

(defn closed?
  "Returns true if channel is closed."
  [port]
  (pt/-closed? port))

(defn timeout
  "Returns a channel that will be closed in the specified timeout. The
  default scheduler will be used. You can provide your own as optional
  first argument."
  ([ms]
   (let [ch (chan)]
     (px/schedule! ms #(pt/-close! ch))
     ch))
  ([scheduler ms]
   (let [ch (chan)]
     (px/schedule! scheduler ms #(pt/-close! ch))
     ch)))

(defn slidding-buffer
  "Create a slidding buffer instance."
  [n]
  (buffers/slidding n))

(defn dropping-buffer
  "Create a dropping buffer instance."
  [n]
  (buffers/dropping n))

(defn fixed-buffer
  "Create a fixed size buffer instance."
  [n]
  (buffers/fixed n))

(defn offer!
  "Puts a val into channel if it's possible to do so immediately.
  Returns a resolved promise with `true` if the operation
  succeeded. Never blocks."
  [port val]
  (let [o (volatile! nil)]
    (pt/-put! port val (channel/volatile->handler o))
    @o))

(defn poll!
  "Takes a val from port if it's possible to do so
  immediatelly. Returns a resolved promise with the value if
  succeeded, `nil` otherwise."
  [port]
  (let [o (volatile! nil)]
    (pt/-take! port (channel/volatile->handler o))
    @o))

(defn pipe
  "Takes elements from the from channel and supplies them to the to
  channel. By default, the to channel will be closed when the from
  channel closes, but can be determined by the close?  parameter. Will
  stop consuming the from channel if the to channel closes."
  ([from to] (pipe from to true))
  ([from to close?]
   (go-loop []
     (let [v (<! from)]
       (if (nil? v)
         (when close? (pt/-close! to))
         (when (>! to v)
           (recur)))))
   to))

(defn onto-chan!
  "Puts the contents of coll into the supplied channel.

  By default the channel will be closed after the items are copied,
  but can be determined by the close? parameter. Returns a channel
  which will close after the items are copied."
  ([ch coll] (onto-chan! ch coll true))
  ([ch coll close?]
   (go-loop [items (seq coll)]
     (if (and items (>! ch (first items)))
       (recur (next items))
       (when close?
         (pt/-close! ch))))))
