joebobmiles / zustand-middleware-yjs

Zustand middleware that enables sharing of state between clients via Yjs.
MIT License
103 stars 10 forks source link

[BUG] There is bug when deleting multiple items from array #61

Open akrv7591 opened 1 month ago

akrv7591 commented 1 month ago

It seems there is bug in applyChangesToArray function when deleting multiple items. In applyChangesToArray function to apply changes array it is being sorted on index field on ASC order then with reduce function it is applying changes to it. The bug is happening when removing element from array revisedArray.splice(index, 1). Let's say we have array with length of 5 and we want to delete 5 elements from array. On every iteration index is going up and the size of array is going down, by the time you are deleting element with index 3 revisedArray length is 2 then it cant remove any element. at the end it returns remaining 2 elements. I have extracted applyChangesToArray function logic from code. You can try it in typescrip playground. Code is provided below