Fork me on GitHub
#hyperfiddle
<
2023-04-26
>
Dustin Getz12:04:45

Headed to Clojure Conj, who else is going?

Rob B16:04:02

I will be a virtual attendant and am looking forward to it.

zane17:04:01

I’ll be there!

alexdavis21:04:06

Myself and some of the Juxt/XTDB team will be there! Already had a few conversations with people here about electric

2
markaddleman21:04:44

I’m trying to incorporate plotly into a sample electric app. I’m trying to set up plotly click events to drive electric functions so I can easily call the server, change state, etc.

markaddleman21:04:45

When the “plotly_click” e/fn handler is called on line 15 (new V1 …) , I get an invalid arity exception

markaddleman21:04:48

What am I doing wrong?

markaddleman21:04:20

I have a feeling that the proper way to do this is that the plotly callback should mutate some atom and then the electric fn should watch that atom

noonian21:04:05

I think you can only call electric functions from within an electric function body or an e/def, and the problem is that plot! Is a regular cljs function and in it you are trying to call V!

xificurC21:04:03

Yes, you should call dom/on with an e/fn

markaddleman21:04:32

What’s the story around interop? Plotly needs a plain javascript fn to handle its callback

markaddleman21:04:01

oh, maybe i get it… instead of (.on div ….) use dom/on ?

markaddleman22:04:58

I’ve updated the code to try dom/on . Now, I get a compiler warning Cannot infer target type in expression (. inst__30182__auto__ newPlot G__46562 G__46563) and it doesn’t seem that the callback is getting invoked at all.

markaddleman23:04:51

Here’s a working version. The big realization for me is that new is a special form (hence all the electric macros). This approach relies on plain Clojurescript for the interop and state atoms to get reactivity with electric.

noonian02:04:21

Adding a ^js type hint to js/Plotly where you call .newPlot may help with the warning. That shouldn't affect behavior in development but will probably break things when you try to compile with advanced optimizations

xificurC12:04:37

we have a convention to name electric functions capitalized. Electric functions are called with new . Therefore if you want to run electric code on a DOM event you call

(dom/on div "plotly_click" (e/fn [data] ... (new V! ...)))
dom/on will call new on your e/fn If a clojure function suffices you can use dom/on!
(dom/on! div "plotly_click" (fn [data] ...))