membrane

https://github.com/phronmophobic/membrane
jlmr 2021-02-28T11:04:31.057800Z

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?

phronmophobic 2021-03-01T08:54:18.060600Z

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.

phronmophobic 2021-03-01T08:58:15.060800Z

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

phronmophobic 2021-03-01T09:02:22.061Z

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

jlmr 2021-03-01T11:38:49.070Z

Looks great, thanks!

jlmr 2021-03-01T11:38:59.070200Z

I’m planning on using crux

jlmr 2021-03-02T08:03:00.070900Z

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

jlmr 2021-03-02T08:03:37.071100Z

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

phronmophobic 2021-03-02T08:20:38.071300Z

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

👍 1
phronmophobic 2021-03-02T08:22:17.071700Z

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

phronmophobic 2021-03-02T08:25:36.071900Z

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