Fork me on GitHub
#missionary
<
2021-07-22
>
martinklepsch09:07:33

I have to say the “Hello flow” docs are somehow a bit over my head so I hope you don’t mind me asking more questions here haha 😄

martinklepsch09:07:40

So with everything being async how do I “consume” a flow? Like e.g. watching for a specific event and then calling something once it occurred. I understood that m/reduce can turn a flow into a task but I’m not sure where to go from here.

martinklepsch09:07:38

Also I’m trying to get some basic stuff going in a CLJS REPL and this is failing:

(m/?
 (m/sp (js/console.log "1")
       (m/? (m/sleep 500))
       (js/console.log "1")))
Despite it being the same as the example here https://cljdoc.org/d/missionary/missionary/b.20/doc/readme/tutorials/hello-task#sequential-composition (?)

leonoel09:07:53

to run a task in clojurescript, you can call it as a function of two arguments, the success callback and the failure callback

leonoel09:07:35

you can even pass it to the promise constructor if you don't need cancellation

leonoel09:07:37

m/? outside of sp / ap works only on JVM because it blocks the thread

martinklepsch10:07:53

Ok, got something working 🎉 posting here for posterity:

(defn run-task [t]
   (-> (js/Promise. t)
       (p/then #(js/console.log "done" %))
       (p/catch #(js/console.log "err" %))))

 (->> (m/observe
       (fn [emit!]
         (.onSnapshot (firebase/->ref [:groups gid])
                      (fn [v]
                        (js/console.log "emit! called")
                        (emit! v)))))
      (m/eduction (comp (take 2) (map #(select-keys (firebase/doc %) [:bla]))))
      (m/reduce conj [])
      (run-task))

martinklepsch10:07:18

the way you can use transducers is kind of cool

martinklepsch10:07:11

Promesa supports cancellation to some extent, would that mean that there could be an identical API for promesa promises and missionary tasks? https://github.com/funcool/promesa/blob/30d10ff3403675e131eb50b3ed6403c8e2acbbd2/src/promesa/impl.cljc

martinklepsch10:07:00

Also on cancellation in general — how would I cancel this task/flow if I no longer need it (e.g. the component using it unmounted or similar)

leonoel10:07:29

when you call a task it returns a zero-argument function which cancels the pending process

martinklepsch10:07:10

this API feels very much made for React hooks

leonoel10:07:29

well, this surely is a pure coincidence

martinklepsch10:07:22

not complaining 😅

martinklepsch10:07:57

When processing a flow, how can I make it just return a single value instead of a seq? Do I do that via the task?

leonoel10:07:03

could you elaborate the use case ?

martinklepsch10:07:52

Waiting for a specific event and wanting to return that event instead of a list with just that one event

leonoel10:07:50

m/reduce with a reducing function that returns a reduced on the specific event

martinklepsch11:07:09

nice! guess I could have come up with that myself haha 😅 thanks for your quick help though, missionary is quite fun 🙂

martinklepsch11:07:50

for posterity

(->> (m/eduction (comp (drop-while #(not (pred %))) (take 1)))
     (m/reduce #(reduced %2))))

martinklepsch10:07:24

When cancelling a task the success callback is called, is there a way to know that it was cancelled? (not sure I need this but just curious)

leonoel10:07:39

not in the general case. the contract just says a cancelled process must terminate asap, it's up to the task implementation to define its behavior in reaction to the cancellation signal.

seancorfield19:07:17

@leonoel I discovered Missionary just this week, due to @mjmeintjes and @dustingetz and I have to say it is fascinating but a bit mind-bending for someone not used to RxJava etc. It took me about an hour of playing with the debounce/`clock` example in the REPL with a bunch of println calls added before I actually understood what it was really doing! 🤯 You have a big notice on the repo that Missionary is "Experimental status, breaking changes should be expected." and I saw Dustin's tweet that HyperFiddle's Photon is going to be built on top of Missionary so that makes me wonder: is Missionary actually safe/ready to use in a production codebase? Is it anywhere close to a stable API at this point?

leonoel21:07:13

@seancorfield Thank you for your interest ! Missionary is definitely close to a stable API. I haven't found any major flaw in current design after 2 years of extensive use, the latest breaking change was renaming a bunch of operators so at this point I would consider future breakages very unlikely. The only feature I still consider slightly experimental is the reactor, however it's under heavy testing right now and the results are very promising so I'm highly confident switching to a production release policy in the near future.

☺️ 7
seancorfield21:07:14

That's very encouraging, thank you @leonoel I will spend more time looking at it then...