Fork me on GitHub
#hyperfiddle
<
2023-05-31
>
Amos14:05:37

Hi, I wonder what is the answer for Quiz: in (e/def x (e/server (e/watch !x))), why do we e/watch on the server? Also, is there API docs for the electric?

xificurC18:05:19

A server atom can only be watched on the server and vice versa . The client can read the watched reactive values, provided they are serializable

xificurC18:05:44

no API docs yet, sorry! We have a https://electric-examples-app.fly.dev/ that should help you ramp up quickly

Dustin Getz19:05:11

to add to what Peter wrote - the atom !x is not serializable and cannot transfer; the watch is implemented by calling clojure.core/add-watch on the atom to register a change callback, therefore the watch must co-locate with the atom

Amos01:06:07

For this line #?(:clj (defonce !x (atom true))) , how can we know that the !x is a ‘server’ atom? not perhaps ‘client’ atom? Perhaps if not specified, will be the server one?

tobias01:06:54

The #?(:clj ...) reader conditional means that the defonce is only executed in clojure (i.e. the server), so that atom only exists on the server. If you wanted a client-side atom you could use #?(:cljs (defonce !x (atom true))) . Docs for reader conditionals: https://clojure.org/guides/reader_conditionals

Amos02:06:19

Got it. Thank you so much.

🙂 2