joebobmiles / zustand-middleware-yjs

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

[BUG] Uncaught TypeError: sharedType.toJSON is not a function #49

Closed arvinxx closed 1 year ago

arvinxx commented 1 year ago

Describe the bug

I'm try to intergrate into our product, but I found a problem:

Uncaught TypeError: sharedType.toJSON is not a function
    at patchSharedType (yjs.mjs:293:1)
    at yjs.mjs:341:1
    at Array.forEach (<anonymous>)
    at patchSharedType (yjs.mjs:294:1)
    at yjs.mjs:344:1
    at Array.forEach (<anonymous>)
    at patchSharedType (yjs.mjs:294:1)
    at yjs.mjs:341:1
    at Array.forEach (<anonymous>)
    at patchSharedType (yjs.mjs:294:1)
    at yjs.mjs:341:1
    at Array.forEach (<anonymous>)
    at patchSharedType (yjs.mjs:294:1)
    at yjs.mjs:464:1
    at transact (yjs.mjs:3292:1)
    at Doc.transact (yjs.mjs:505:1)
    at yjs.mjs:463:1
image

here is my data snapshot:

image

Screenshots

Versions (please complete the following information):

Additional context Add any other context about the problem here.

joebobmiles commented 1 year ago

I'm going to need a minimal reproduction to debug this. Can you set up a CodeSandbox or repo that reproduces this error? Or at least the full JSON of your data snapshot instead of a screencap?

alexkreidler commented 1 year ago

I've got the same error. I made a CodeSandbox example.

Reproduction steps:

  1. Open devtools
  2. Click on test
  3. Click on test2. You should see the error in the console (You may have to open a second tab with the same page before step 2/3)
  4. If you repeat clicking on any of the text items you should see the error repeat

I initially thought it might be an issue with the immer Zustand extension. But I removed it from the code and still got the error.

joebobmiles commented 1 year ago

Thanks Alex!

From debugging your example, the bug is obvious: I forgot to convert strings to YText objects before shoving them in Yjs...

I'll get that fixed as soon as I have the time!

joebobmiles commented 1 year ago

Release 1.3.1 fixes this issue, so going to close for now. If something else is broken, don't hesitate to reopen it!