YousefED / SyncedStore

SyncedStore CRDT is an easy-to-use library for building live, collaborative applications that sync automatically.
https://syncedstore.org
MIT License
1.69k stars 51 forks source link

Sorting an array in Synced Store #87

Closed websiddu closed 1 year ago

websiddu commented 1 year ago

I have an array in synced store and I wanted to sort

I'm using

const [removed] = store.nodes.splice(srcIndex, 1);
store.nodes.splice(destIndex, 0, removed)

I keep getting an error Not supported: reassigning object that already occurs in the tree.

websiddu commented 1 year ago

Never mind, i was trying to add a proxy object directly, and when I convert the proxy to JS it worked fine.

YousefED commented 1 year ago

Alright! Best to do sorting on a non-synced copy of the array indeed

djmaze commented 1 year ago

And what's the recommended way to convert the proxied array to JS? array.slice(0)?

websiddu commented 1 year ago

you use lodash's copy function or just do JSON.parse(JSON.stringify(proxyObject))

djmaze commented 1 year ago

@websiddu That seems pretty inefficient. Wouldn''t it make sense to add a method to the proxy which allows direct access to the underlying array (or similar)?

Also, maybe I am missing something, but as it is not possible to replace the array in the store, how can I change the sort order in the stored array permanently? Or is this not supported and I should rather add a position property to each item instead?