Clojure - 使用 core.async 通道和 Yada/Aleph

Clojure - use a core.async channel with Yada/Aleph

我正在尝试将 Clojure manifold library, and in order to understand it, I need wanted to convert a core.async 通道用于流形流。

我想使用 core.async 通道创建以下等效项:

 (require '[manifold.stream :as s])
 (s/periodically 100 #(str " ok "))

  ;; Here is what I tried, it fails with an error 500
  (let [ch (chan)]
      (go-loop []
      (>! ch " ok ")
      (<! (timeout 100))
      (recur))
    (s/->source ch))

我正在尝试将 core.async 频道输入 yada。使用 manifold.stream/periodic 的第一个代码示例有效,但其他使用 core.async 的示例无效。我试过 yada 1.0.01.1.0-SNAPSHOT.

使用 manifold.stream/periodic 有效:

(def get-stream
  (yada (fn [ctx]
          (-> (:response ctx)
              (assoc :status 202)
              (assoc :body (s/periodically 1000 #(str (System/currentTimeMillis) " ")))))
        {:representations [{:media-type "application/json"
                            :charset    "UTF-8"}
                           {:media-type "application/edn"
                            :charset    "UTF-8"}]}))

使用manifold.stream/->source returns 错误500 :

(def get-stream
  (yada (fn [ctx]
          (-> (:response ctx)
              (assoc :status 202)
              ;; Similar to this : https://github.com/juxt/yada/blob/94f3ee93de155a8513b27e0508608691ed556a55/dev/src/yada/dev/async.clj
              (assoc :body (let [ch (chan)]
                             (go-loop []
                               (>! ch " ok ")
                               (<! (timeout 100))
                               (recur))
                             (s/->source ch)))))
        {:representations [{:media-type "application/json"
                            :charset    "UTF-8"}
                           {:media-type "application/edn"
                            :charset    "UTF-8"}]}))

;; Error on the page :

;; 500: Unknown
;; Error on GET

;; #error {
;;         :cause "No implementation of method: :to-body of protocol: #'yada.body/MessageBody found for class: manifold.stream.async.CoreAsyncSource"
;;         :via
;;         [{:type clojure.lang.ExceptionInfo
;;           :message "Error on GET"
;;           :data {:response #yada.response.Response{:representation {:media-type #yada.media-type[application/json;q=1.0], :charset #yada.charset.CharsetMap{:alias "UTF-8", :quality 1.0}}, :vary #{:media-type}}, :resource #function[backend.routes.examples.ts/fn--57734]}
;;                                                                                                  :at [clojure.core$ex_info invoke "core.clj" 4593]}
;;                                                                     {:type java.lang.IllegalArgumentException
;;                                                                      :message "No implementation of method: :to-body of protocol: #'yada.body/MessageBody found for class: manifold.stream.async.CoreAsyncSource"
;;                                                                      :at [clojure.core$_cache_protocol_fn invoke "core_deftype.clj" 554]}]
;;                                                    :trace

第三次尝试,使用 core.async 通道(不同的错误 500):

(def get-stream
  (yada (fn [ctx]
          (-> (:response ctx)
          (assoc :status 202)
          (assoc :body (chan)))
        {:representations [{:media-type "application/json"
                            :charset    "UTF-8"}
                           {:media-type "application/edn"
                            :charset    "UTF-8"}]}))

;; Error on the page :

;; 500: Unknown

;; Error on GET

;; #error {
;;         :cause "No implementation of method: :to-body of protocol: #'yada.body/MessageBody found for class: clojure.core.async.impl.channels.ManyToManyChannel"
;;         :via
;;         [{:type clojure.lang.ExceptionInfo
;;           :message "Error on GET"
;;           :data {:response #yada.response.Response{:representation {:media-type #yada.media-type[application/json;q=1.0], :charset #yada.charset.CharsetMap{:alias "UTF-8", :quality 1.0}}, :vary #{:media-type}}, :resource #function[backend.routes.api.subscribe/subscribe$fn--64130]}
;;                                                                                                  :at [clojure.core$ex_info invoke "core.clj" 4593]}
;;                                                                     {:type java.lang.IllegalArgumentException
;;                                                                      :message "No implementation of method: :to-body of protocol: #'yada.body/MessageBody found for class: clojure.core.async.impl.channels.ManyToManyChannel"
;;                                                                      :at [clojure.core$_cache_protocol_fn invoke "core_deftype.clj" 554]}]
;;                                                    :trace

关键错误是这样的:

"No implementation of method: :to-body of protocol: 
 #'yada.body/MessageBody     
 found for class: manifold.stream.async.CoreAsyncSource"

这表明您正在使用的 yada 版本试图强制转换它不理解的体型。较新版本的 yada 对您可以发送到网络服务器的内容更加宽容,并允许它不知道如何转换的任何内容。