Follow

I can't imagine anyone being nerdy enough to care. 

Running into annoying race condition between React and PouchDB—my React component unmounts and remounts when PouchDB reports a change, and during that interval React misses many changes.

Basically I need PouchDB's change mechanism (a standard event emitter) to losslessly throttle with a certain cooldown period.

I don't want to write that logic—surely a throttle or debounce middleware for Node emitters exists? Doesn't seem like it. What…?

I can't imagine anyone being nerdy enough to care. 

@22 It sounds more like you need another level of indirection in the middle. I mean, something as simple as a queue which receives messages from pouchdb and then the react component reads from the queue.
I know nothing of the architecture, so I don't know why the component unmounts and remounts, but that could be an antipattern too.

I can't imagine anyone being nerdy enough to care. 

@redglow Deep sigh yeah I'll change it exactly the way you describe: the PouchDB changes listener, instead of updating the React component state, will just emit a Redux action, which eventually makes its way to the component. Redux doesn't shut its ears 🙉 so it can accept a firehose of Pouchdb changes.

re: I can't imagine anyone being nerdy enough to care. 

@22 Ah, yes, Redux will surely do what you need!

Another solution which came to my mind and is typical for React would be an initial state sync with pouchdb & listener registration on mount (componentDidMount / useEffect hook), deregistration of listeners on unmount (componentWillUnmount / useEffect hook return value).

(though, if you already have Redux in, that's probably the best solution)

re: I can't imagine anyone being nerdy enough to care. 

@redglow No that (the React register/deregister) is exactly what I have right now: I use the Effects hook so after each render, a changes callback is registered with PouchDB which updates the local component state (and a cancel callback for unmount). That's when I get the issue: Pouchdb sends like 5 changes in rapid succession—the first one React gets but the rest it never gets cuz it's off unmounting and remounting. Thanks for your interest!

re: I can't imagine anyone being nerdy enough to care. 

@22 yeah, that's why I named also the initial sync at every mount: this way you re-synchronize with the current PouchDB state, and even if you lost some messages before it's no problem.

Anyway, I hope you get it solved ;)

re: I can't imagine anyone being nerdy enough to care. 

@redglow Ohh I see what you mean, resync the entire Pouchdb state during component load. Yes, Redux is the answer 😅!

Sign in to participate in the conversation
Octodon

The social network of the future: No ads, no corporate surveillance, ethical design, and decentralization! Own your data with Mastodon!