Fork me on GitHub
#hyperfiddle
<
2023-09-30
>
mattias09:09:10

I need to call a JS function from Electric and receive the return value via a callback I'm passing to the function. I need that value "in Electric" so that I can perform some action with it in the backend. I can achieve this by creating a temporary JS atom and watching that, but perhaps there is a better way to do this with missionary?

(e/client
  (let [!res (atom nil)]
    (js/chrome.tabs.sendMessage js/chrome.devtools.inspectedWindow.tabId
                                (clj->js {:action "do-sth"})
                                #(reset! !res (js->clj %)))
    (when-let [new-res (e/watch !res)]
      (e/server (println "!res changed" new-res)))))

mattias09:09:05

Actually, I either need to wrap it in another e/server call

(e/server
 (e/client
  (let [!res (atom nil)]
    (js/chrome.tabs.sendMessage js/chrome.devtools.inspectedWindow.tabId
                                (clj->js {:action "do-sth"})
                                #(reset! !res (js->clj %)))
    (when-let [new-res (e/watch !res)]
      (e/server (println "!res changed" new-res))))))
or do some other stuff in the server, like another print statement
(e/client
 (e/server (println "sth"))
 (let [!res (atom nil)]
   (js/chrome.tabs.sendMessage js/chrome.devtools.inspectedWindow.tabId
                               (clj->js {:action "do-sth"})
                               #(reset! !res (js->clj %)))
   (when-let [new-res (e/watch !res)]
     (e/server (println "!res changed" new-res)))))
Otherwise I don't see the backend print statement "!res changed"

Dustin Getz10:09:10

answering from mobile - this is a common q, try searching the history

👍 1
joshcho21:09:22

What is differential electric?

1
xificurC10:10:37

The upcoming next version which will internally work on differential data.

👍 2
joshcho11:10:32

Wdym by differential data?

xificurC11:10:59

Diffs. E.g. instead of sending a collection you send "update 5th element to x"

❤️ 2
Dustin Getz11:10:17

differential electric is natively streaming, it works on streaming event sources (like LLM completions) natively. It fixes several implementation issues as well that were preventing Electric e/for from achieving optimal network IO and introducing unexpected latency

❤️ 2
Dustin Getz12:10:03

differential electric is a strong enough foundation to build ultradynamic responsive UIs like say microsoft word. By operating on flows of diffs instead of flows of values it basically means UI code never has to fullscan a collection, even large lists can be rendered without fullscan. Unlike e/for-by today which traverses the full list everytime any element changes

❤️ 2
Dustin Getz12:10:44

It is probably also a strong enough foundation to build reactive databases like Rama and Materialized in a small amount of LOC, though this is not our mission. Such a database could have an electric client/server split right through the middle of the query engine and indexes, potentially solving the local-first problem once and for all. In this worldview basically the DB and UI are unified and interwoven in the same way that client/server are in electric

❤️ 2
joshcho17:10:40

so to the end user, extremely responsive and fast ui, and the programming model doesn't need to worry about how big our collection is? are there other benefits for the user?

joshcho17:10:42

ah so this will be huge for streaming with llms. i thought some parts of electric were already differential

xificurC17:10:00

e/for is, but the differential version will fix some issues it has

👍 2
joshcho11:10:39

Will differential also be an option for strings, e.g. streaming building up a string? Workaround is trivial but

xificurC11:10:42

can you describe the use case?

joshcho12:10:55

llm streaming, building up a string via str

joshcho12:10:15

but a workaround is easy with a list of string tokens instead

xificurC12:10:36

if the string is rendered as text only you can just collect them and dom/text them in. If it's parsed and a DOM structure is built differential electric will help with that. No snippets to share on that yet though 🙂

👀 1
❤️ 1
Dustin Getz12:10:11

Yes arbitrary differential collections including accumulating collections of strings (i.e, for rendering a table view based on a stream of strings)

👍 1
Dustin Getz12:10:39

@U09FL65DK I dont understand your statement

xificurC12:10:14

which part? Also, the question was "streaming building up a string", not accumulating collections of strings