electric icon indicating copy to clipboard operation
electric copied to clipboard

consistency glitch when effect depends on value derived from network round trip

Open ikappaki opened this issue 1 year ago • 1 comments
trafficstars

  • Here, we expect cnt to be Pending on the client because it is derived from dir via network.
  • We observe a glitched state: when dir (client) updates, (println :dir dir :files cnt) (client) updates immediately with the fresh value of dir (client) but stale/dirty value of cnt (server) while dir round trips to produce a new cnt (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

  1. Clone https://github.com/hyperfiddle/electric-starter-app at rev 307af9e
  2. Update main.cljjc to 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))))))))
  1. Assuming you have a /tmp directory with some files, also create a /tmp/e directory with zero files in it.
  2. Start the program with clj -A:dev -X dev/-main
  3. When the program starts, you should get the dir and cnt printed out (c:\tmp and 5 in 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
  1. Change the input !dir to c:/tmp/e on the browser, I expect to report c:/tmp/e and count 0 on both server and client, but the client gets an additional c/tmp/e and count 5 printed 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

ikappaki avatar Mar 14 '24 07:03 ikappaki

slack discussion: https://clojurians.slack.com/archives/C7Q9GSHFV/p1710441337420199

dustingetz avatar Mar 14 '24 13:03 dustingetz