Skip to content

Instantly share code, notes, and snippets.

@jeans11
Created March 2, 2023 10:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jeans11/3401cc6b23f0dd8f889971f6aa76fcac to your computer and use it in GitHub Desktop.
Save jeans11/3401cc6b23f0dd8f889971f6aa76fcac to your computer and use it in GitHub Desktop.
Electric datalevin pending weird behaviour
(ns app
(:require [hyperfiddle.electric :as e]
[hyperfiddle.electric-dom2 :as dom]
[hyperfiddle.electric-ui4 :as ui]
#?(:clj [datalevin.core :as d])
(:import (hyperfiddle.electric Pending)))
#?(:clj
(defonce !conn (d/get-conn "/tmp/foo")))
(comment
(d/close !conn)
(alter-var-root #'!conn (constantly (d/get-conn "/tmp/foo")))
nil)
(e/def db)
#?(:clj
(defn add-item []
(d/transact! !conn [{:db/id -1 :item/id (random-uuid)}])))
#?(:clj
(defn list-items [db]
(d/q '[:find [(pull ?e [:item/id]) ...]
:where [?e :item/id]]
db)))
(e/defn App []
(e/server
(binding [db (e/watch !conn)]
(e/client
(ui/button
(e/fn [] (e/server (e/offload #(add-item)) nil))
(dom/text "+ Add an item"))
(dom/ul
(e/server
(try
(let [items (e/offload #(list-items db))]
(e/for-by :item/id [{:keys [item/id]} items]
(e/client
(dom/li (dom/text id)))))
(catch Pending _ (e/client (dom/p (dom/text "Loading...")))))))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment