Fork me on GitHub
#aleph
<
2020-02-12
>
erik11:02:46

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?

erik11:02:03

the question is about Manifold not Aleph, but I thought this would be the right place to ask nevertheless

mccraigmccraig11:02:30

i don't know that you can do that @eallik - there's just the single xform on the stream constructor

mccraigmccraig11:02:03

but why do you want to do that ? why not just put the object directly on the stream ?

erik11:02:41

I have WS connections for exchanging messages in JSON format.

erik11:02:10

I thought I'd just remove the hassle of json-> and ->json every time I take! from or put! to a connection

mccraigmccraig11:02:01

(stream/transform (map json->) s)

mccraigmccraig11:02:16

(stream/transform (map ->json) s)

erik11:02:32

and then make one sink-only and the other source-only?

mccraigmccraig11:02:33

will those not do the trick ?

mccraigmccraig11:02:44

yes, use sink-only and source-only where it makes sense

erik11:02:47

wait, transform returns a source, not a stream

erik11:02:08

(stream/transform (map json->) s) makes sense but the other doesn't

mccraigmccraig11:02:15

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

erik11:02:35

(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)))

erik11:02:59

this is what I came up with. new to Manifold, making sure I got it right.

mccraigmccraig11:02:20

looks like it will work

mccraigmccraig11:02:45

(without me doing any testing, anyway 😬)

erik21:02:59

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

erik21:02:50

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

erik21:02:36

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

erik21:02:52

does that make sense?