This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-02-28
Channels
- # announcements (11)
- # babashka (66)
- # beginners (19)
- # calva (4)
- # clj-kondo (26)
- # cljsrn (33)
- # clojure (38)
- # clojure-australia (10)
- # clojure-dev (3)
- # clojurescript (18)
- # conjure (8)
- # data-science (2)
- # datahike (1)
- # datascript (5)
- # defnpodcast (3)
- # emacs (3)
- # events (2)
- # fulcro (17)
- # funcool (66)
- # girouette (28)
- # jobs-discuss (1)
- # lsp (33)
- # malli (1)
- # membrane (11)
- # off-topic (2)
- # practicalli (1)
- # re-frame (141)
- # rewrite-clj (6)
- # sci (1)
- # shadow-cljs (68)
- # sql (5)
- # startup-in-a-month (7)
- # vim (1)
- # xtdb (21)
Hi @smith.adriane, I’m still tinkering with membrane. I’m pretty sure the (slightly to big) toy project I’d like to build requires hybrid state consisting of:
• “ephemeral” ui data (e.g. which item is selected), stored in memory (atom?)
• “durable” data (e.g. the descriptions of items), stored in a database
Right now I’m struggling to figure out what is the best way to build this. I was hoping to run my current idea by you:
• Create some custom structure that supports IDeref.
• The structure contains a regular map, storing the “ephemeral” data.
• However some paths in the map would contain database queries.
• Whenever the structure is dereffed, all queries are executed and their results stored under the corresponding paths (this step could be cached as well).
With this setup I could still use membrane’s state management solution for “ephemeral” data e.g. [:update path]
, [:delete path]
etc. When a component wants to access some data, all it has to do is trigger an effect handler which [:set]
s some path in the structure to be query.
Do you think this is a viable route? Or is it completely crazy?
That sounds reasonable, but I don't think you need to mimic IDeref. In your make-app
function, you can just deref the atom that holds the ephemeral state and merge in the data from the database.
maybe something like:
(defn make-app
([ui-var conn query]
(let [
ephemeral-state (atom {})
handler (component/default-handler ephemeral-state)
arglist (-> ui-var
meta
:arglists
first)
m (first arglist)
arg-names (disj (set (:keys m))
'extra
'context)
defaults (:or m)
top-level (fn []
(component/top-level-ui {:state (merge @ephemeral-state
(db/query @conn query))
:$state []
:body ui-var
:arg-names arg-names
:defaults defaults
:handler handler}))]
top-level)))
If you're able to share, what database and backend are you planning on using? I might be able to provide a better example.
that's true. I made some improvements to defui
that makes :$state
superfluous in this case
generally, the keywords that start with dollar sign allow you to explicitly provide the reference for the corresponding key.
usually, all the $
args are automatically handled for you, but there are some cases where it's useful for dev tooling and testing