react

martinklepsch 2021-09-17T09:08:26.010900Z

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.

martinklepsch 2021-09-17T09:35:10.013100Z

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.

lilactown 2021-09-17T16:06:49.013500Z

makes sense to me

lilactown 2021-09-17T16:07:34.014500Z

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

lilactown 2021-09-17T16:09:47.016800Z

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

lilactown 2021-09-17T16:10:55.017700Z

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

lilactown 2021-09-17T16:12:01.018400Z

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

lilactown 2021-09-17T16:13:31.019600Z

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

lilactown 2021-09-17T16:15:44.020600Z

ensuring accuracy of the timer is a little tricky too

lilactown 2021-09-17T16:24:59.025700Z

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/

martinklepsch 2021-09-17T16:43:33.026700Z

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

martinklepsch 2021-09-17T16:43:56.027100Z

(which is totally possible ofc)

martinklepsch 2021-09-17T16:45:19.027900Z

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

lilactown 2021-09-17T17:01:29.028100Z

not sure what you mean

lilactown 2021-09-17T17:06:52.028900Z

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

lilactown 2021-09-17T17:07:26.029200Z

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

lilactown 2021-09-17T17:09:02.029600Z

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

lilactown 2021-09-17T17:14:29.033500Z

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

lilactown 2021-09-17T17:19:22.034800Z

e.g.

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

lilactown 2021-09-17T17:21:28.035Z

is that what you mean?

dominicm 2021-09-17T21:35:33.036300Z

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

lilactown 2021-09-17T22:42:12.037100Z

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

lilactown 2021-09-17T22:42:22.037400Z

(In subscriptions)