xaviergonz / mobx-keystone

A MobX powered state management solution based on data trees with first class support for Typescript, support for snapshots, patches and much more
https://mobx-keystone.js.org
MIT License
554 stars 25 forks source link

Does onPatches support batching of map object updates? #546

Closed agronbajraktari closed 3 months ago

agronbajraktari commented 3 months ago

Currently I receive only singular changes in the onPatches listener even if i make batch updates to a map object. In a react component wrapped with "observer", it re-renders the component only once correctly given that I use @modelAction (or transaction). However the patches are fired 1-by-1. Is it possible to receive them batched?

xaviergonz commented 3 months ago

they don't, but there's a workaround which is to use onSnapshot (which only happens on transactions) and record patches on an array and "flush" them when onSnapshot callback gets called.

so something like:

const patches = []
onPatches -> patches.push(patch)
onSnapshot -> process patches array and set length to 0
agronbajraktari commented 3 months ago

@xaviergonz Understand. That's a nice workaround. Will try it. Thanks!