I'm trying to create a stream from an existing stream xs
in a way that put!
s are automatically encoded to JSON and takes!
decoded from JSON βΒ it doesn't seem possible with the current API; should I just create two separate streams, one that is sink-only and another that is source-only?
the question is about Manifold not Aleph, but I thought this would be the right place to ask nevertheless
i don't know that you can do that @eallik - there's just the single xform
on the stream
constructor
but why do you want to do that ? why not just put the object directly on the stream ?
I have WS connections for exchanging messages in JSON format.
I thought I'd just remove the hassle of json->
and ->json
every time I take!
from or put!
to a connection
(stream/transform (map json->) s)
(stream/transform (map ->json) s)
and then make one sink-only
and the other source-only
?
will those not do the trick ?
yes, use sink-only
and source-only
where it makes sense
wait, transform
returns a source, not a stream
(stream/transform (map json->) s)
makes sense but the other doesn't
ah, ok - in which case does a plain (stream/map ->json s)
do the trick for you ? iirc stream/map
is very simply implemented with connect-via
(defn convert-stream [f-src f-dst s]
(let [src (s/map f-src s)
sink (s/stream)]
(s/connect-via sink #(s/put! s (f-dst %)) s)
(s/splice sink src)))
this is what I came up with. new to Manifold, making sure I got it right.
looks like it will work
(without me doing any testing, anyway π¬)
it worked when I changed to (s/connect-via sink #(s/put! s (f-src %)) s)
β I got confused by the fact that messages should be delivered from the sink to the source, because on the outside, they are written to the sink and read from the source
in fact, the names f-src
and f-dst
are confusion β they should instead be f-sink
and f-source
to indicate which one gets applied to which end of s
also, it seems that it only makes sense to use my convert-stream
over duplex streams, otherwise any value put!
on the sink will be passed thru both functions
does that make sense?