Fork me on GitHub
#membrane
<
2021-02-28
>
jlmr11:02:31

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?

phronmophobic08:03:18

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.

phronmophobic08:03:15

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)))

phronmophobic09:03:22

If you're able to share, what database and backend are you planning on using? I might be able to provide a better example.

jlmr11:03:49

Looks great, thanks!

jlmr11:03:59

I’m planning on using crux

jlmr08:03:00

What does the $state [] keyval do in the call to top-level-ui?

jlmr08:03:37

It doesn’t seem to be something that top-level-ui actually uses

phronmophobic08:03:38

that's true. I made some improvements to defui that makes :$state superfluous in this case

👍 3
phronmophobic08:03:17

generally, the keywords that start with dollar sign allow you to explicitly provide the reference for the corresponding key.

phronmophobic08:03:36

usually, all the $ args are automatically handled for you, but there are some cases where it's useful for dev tooling and testing