Hi folks,
Please forgive my ignorance, but I'm noticing something surprising when reacting to a button click using a Token.
You will see that I can deref a server-side future at the top level but not inside the when-some block.
Is this expected behavior? What is the proper way to implement async (server-side) calls when reacting to a button press?
Thank you.
(ns repro
(:require [hyperfiddle.electric3 :as e]
[hyperfiddle.electric-dom3 :as dom]]))
(e/defn Repro []
(e/client
(println "Top-level Future: " (e/server @(future "hi"))) ; works
(when-some [t (dom/button (dom/text "Press Me")
(let [e (dom/On "click" identity nil)
[t err] (e/Token e)]
t))]
(println "The answer is: " (e/server 5)) ; works
(println "CB Future: " (e/server @(future "hi"))) ; doesn't work
(t))))
calling (t) spends the token and t turns nil, unmounting the when-some branch. This races with the server calls, i.e. they might cancel. A simple way to wait for a server call to finish is to sequence with case
(case (e/server @(future "hi"))
(t))there is an implicit do in when-some and in electric, do is concurrent, so (t) runs concurrently with the two printlns
Great. Thank you!