Fork me on GitHub
#missionary
<
2022-02-08
>
martinklepsch09:02:21

I ran into a breaking change with this piece of code when upgrading from b.20 to b.26

(defn- wait-until
  [doc-ref pred]
  (->> (m/observe (fn [emit!] (.onSnapshot doc-ref emit!))) ; get flow of updates to document
       (m/eduction (comp (map firebase/doc) (drop-while #(not (pred %))) (take 1))) ; get first doc matching predicate
       (m/reduce #(reduced %2)) ; return doc
       (m/timeout 10000))) ; fail after 10s

martinklepsch09:02:50

The error I’m seeing is TypeError: task.call is not a function

martinklepsch09:02:04

ah, that looks like timeout now takes the task as first arg instead of second?

martinklepsch09:02:22

(which I guess makes sense if you think about tasks as maps and flows as seqs)

leonoel09:02:51

also instead of failing on timeout it succeeds with an optional value

martinklepsch10:02:56

Do you have an idea why this would never log :x

(defn- wait-until
  [doc-ref pred]
  (m/timeout
   (->> (m/observe (fn [emit!] (.onSnapshot doc-ref emit!))) ; get flow of updates to document
        (m/eduction (comp (map (fn [x] (js/console.log :x x) x))
                          (take 1)))
        (m/reduce #(do (js/console.log :wait-reduced %2) (reduced %2)))) ; return doc
   10000)) ; fail after 10s

martinklepsch10:02:21

it does log wait-reduced nil

leonoel10:02:32

reduce is wrong, if initial value is not provided the reducer must have zero arity

martinklepsch10:02:42

I’m not sure I understand, zero arity would mean no arguments? how would that work?

martinklepsch10:02:07

The docstring doesn’t indicate this btw.

leonoel10:02:43

I think it does

leonoel10:02:20

clojure.core/reduce does that too

leonoel10:02:20

e.g (reduce conj (range 10)) will call conj with no argument to get the seed

martinklepsch10:02:29

Ah, ok I think I’m getting there

martinklepsch10:02:40

it’ll call (reduced nil) if theres no initial value

martinklepsch10:02:04

A utility to return the first value of a flow and terminate after it could be nice maybe

martinklepsch10:02:19

Also interestingly the code above worked fine with b.20

leonoel10:02:58

I don't fully understand why. It may be due to https://github.com/leonoel/missionary/issues/13 however it looks like in clojurescript when a function is called for an undefined arity it's called for a superior arity with nil args instead of raising an exception