Fork me on GitHub
#react
<
2021-09-17
>
martinklepsch09:09:26

Wrote down some thoughts on our internal subscription layer this morning and thought maybe it’s interesting to share here as well. It’s more questions than answers really but I’m curious if it prompts any reactions/thoughts from other folks working on this type of stuff.

martinklepsch09:09:10

Probably a third approach worth mentioning is using a database type thing on the client that allows for efficient queries combining data (e.g. entitydb or autonormal) but I think that still leaves a few things open, in particular around the time stuff.

lilactown16:09:49

makes sense to me

lilactown16:09:34

the watch vs. delta vs. database thing I think you're right you can kind of boil all of them down to events

lilactown16:09:47

one thing I've been thinking about for autonormal is adding delta capabilities to it. since it knows all of the entities you're adding to the map in order to normalize them, I think you can reliably obtain a "which entities changed" delta. so instead of listening to the whole db, you could write a wrapper that listens just to the entities you care about

lilactown16:09:55

the bones of this are already in there with add-report and pull-report it just needs to be exercised to see if if it's reliable

lilactown16:09:01

ofc if your data source already has these delta events then that might be more efficient, assuming the events are at the level of granularity you want

lilactown16:09:31

w.r.t. time I think your idea of setting a timer is best. you can have an object that tracks end_dt and clears and recreates the timer when end_dt changes

lilactown16:09:44

ensuring accuracy of the timer is a little tricky too

lilactown16:09:59

I could imagine something like:

(defn use-timer
  [end-dt on-end]
  (let [cb (useRef on-end)]
    ;; ensure ref is updated after each render so we always
    ;; have the latest on-end fn
    (useEffect #(set! (.-current cb) on-end))
    (useEffect
      (fn []
        (let [interval (js/setInterval
                         #(when (>= (js/Date.now) end-dt)
                            ((.-current cb)))
                         ;; check every 100ms
                         100)]
          #(js/clearInterval interval))
      #js [end-dt])))
might work. copped some of this from https://overreacted.io/making-setinterval-declarative-with-react-hooks/

martinklepsch16:09:33

Yeah that’s an approach we use in a few places but I’d almost like to handle it outside of react closer to the layer that transforms data for usage in views

martinklepsch16:09:56

(which is totally possible ofc)

martinklepsch16:09:19

@lilactown with autonormal, I was wondering: is there a functionality to identify records with “slots” like in entitydb or idx?

lilactown17:09:29

not sure what you mean

lilactown17:09:52

I tried ctrl-f "slot" in entitydb and idx's READMEs

lilactown17:09:26

ah I see the previous iteration of entitydb has it...

lilactown17:09:02

ah so like a custom index. yes I think you can do that with autonormal

lilactown17:09:29

(-> (a/db [])
    (a/add {:users {:current {:user/id 1 :user/name "martinklepsch" :user/likes #{"programming"}}}}))
;; => {:users {:current [:user/id 1]}
;;     :user/id {1 {:user/id 1 :user/name "martinklepsch" :user/likes #{"programming"}}}
pull queries on :users :current will look up the [:user/id 1] path in the db to descend

lilactown17:09:22

e.g.

(a/pull *1 [{:users [{:current [:user/name]}]}])
;; => {:users {:current {:user/name "martinklepsch"}}}

lilactown17:09:28

is that what you mean?

dominicm21:09:33

> since it knows all of the entities you're adding to the map in order to normalize them, I think you can reliably obtain a "which entities changed" delta. so instead of listening to the whole db, you could write a wrapper that listens just to the entities you care about I think this would be useful for @roman01la’s thoughts around "kafka for the frontend" too. I've personally run into re-frame performance issues where I really just needed a delta & to update indexes myself rather than rebuilding them from scratch.

lilactown22:09:12

Re-frame’s inability to reason about time does make it very cumbersome to do stuff like that, yeah

lilactown22:09:22

(In subscriptions)