electric
electric copied to clipboard
consistency glitch when effect depends on value derived from network round trip
trafficstars
- Here, we expect
cntto be Pending on the client because it is derived fromdirvia network. - We observe a glitched state: when
dir(client) updates,(println :dir dir :files cnt)(client) updates immediately with the fresh value ofdir(client) but stale/dirty value ofcnt(server) whiledirround trips to produce a newcnt(server) - Electric v2 already accounts for this circumstance and therefore the observed behavior is unexpected and we have logged it as a bug.
To reproduce
- Clone https://github.com/hyperfiddle/electric-starter-app at rev 307af9e
- Update
main.cljjcto create the below deps and print out their values
(ns electric-starter-app.main
(:require [hyperfiddle.electric :as e]
[hyperfiddle.electric-dom2 :as dom]
[hyperfiddle.electric-ui4 :as ui]))
#?(:clj
(defn dir-list [dir]
(.listFiles (clojure.java.io/file dir))))
(e/defn Main [ring-request]
(e/client
(binding [dom/node js/document.body]
(let [!dir (atom "c:/tmp/")
dir (e/watch !dir)]
(ui/input dir (e/fn [v] (reset! !dir v))
(dom/props {:type "search"}))
(e/server
(let [files (dir-list dir)
cnt (count files)]
(println :dir dir :files cnt)
(e/client
(println :dir dir :files cnt))))))))
- Assuming you have a
/tmpdirectory with some files, also create a/tmp/edirectory with zero files in it. - Start the program with
clj -A:dev -X dev/-main - When the program starts, you should get the
dirandcntprinted out (c:\tmpand5in my case), both on server and client
server output
:dir c:/tmp/ :files 5
client output
shadow-cljs: #3 ready!
:dir c:/tmp/ :files 5
- Change the input
!dirtoc:/tmp/eon the browser, I expect to reportc:/tmp/eand count0on both server and client, but the client gets an additionalc/tmp/eand count5printed out on the client
server output
:dir c:/tmp/e :files 0
client output
:dir c:/tmp/e :files 5 ;; didn't expect this
:dir c:/tmp/e :files 0
slack discussion: https://clojurians.slack.com/archives/C7Q9GSHFV/p1710441337420199