Closed joebobmiles closed 3 years ago
There's something wonky going on with patchSharedType
that leads to the Zustand store having nested objects and arrays erased. See the results of the failing tests for examples of this.
For those (including future me) who are too lazy to parse the test output, here's what I've been observing:
set
operation.patchSharedType
.patchSharedType
creates a diff between the shared type and the updated state to figure out what changes need to be applied to the shared type via sharedType.set()
(for Y.Maps) or the Y.Array set algorithm. Only when we see a pending
change (indicates a nested array or map in state diff) do we recurse.
sharedType
to the object stored in sharedType.get(property)
, where property is the index in the diff where the pending
change was detected. The new newState
is set to newState[property]
.After patching the shared type, we return to the original call site.
get()
or getState()
would match ymap.toJSON()
(ymap
being the Yjs shared type backing the store.Yet these results don't match. Instead, we observe that ymap.toJSON()
has the correct new state, and get()
gives us a truncated state.
getState().getOlder(); // Increases person age from zero to one.
ymap.toJSON(); // => { "person": { "name": "Joe", "age": 1 } }
getState() // => { "getOlder": [Function], "person": {} }
// ^^^^^^^^^^^^ Truncated state! person is an empty object!
After a break and some further reflection, it seems not to be a problem isolated solely to patchSharedType
. Instead, it seems that it's an interaction between patchSharedType
and patchStore
when patchStore
is invoked inside a map.observeDeep
call.
I'd still like to know what interaction is going on that causes the problem I've been observing.
I figured out that the problem was actually inside patchStore
. When there were no changes to 'patch' into the old store, an empty array or object would be returned. This caused the truncation I observed. Simply adding a check for when there are no changes circumvented that problem.
:tada: This PR is included in version 1.0.0 :tada:
The release is available on:
Your semantic-release bot :package::rocket:
The code currently in the master branch is a mess, and I barely understand how it works. I know it works since the tests pass, but there is significant room for improvement. This PR will track my progress on the rewrite.
TODO: