Description
Bug description
We ran into a few errors related to refs when moving instances through various different schemas. I've provided a reproduction below.
For context, we basically have a sandbox-like game world which replicates its schema state 1:1 from the server to clients. The game world's state can be arbitrarily complex, as it stores not only arbitrary game state, but also visual properties of all entities in the game (such as mesh shapes, sizes, textures, etc.). So the game is largely "server-side rendered". Representing all this state in an ergonomic way often requires sharing and nesting schema instances across different schema objects.
I think we might be pushing the limits of the current schema system, or possibly even using it in an unintended way 😅
Optional: Minimal reproduction
Since the issues seem to be of the form "errors when sharing instances across schemas", I've created a PR here with a few new test cases showcasing the errors: #202
If it's helpful, I could also split this into different Github issues
Running the full test suite should result in this output:
332 passing (965ms)
9 pending
3 failing
1) Encoder
dynamic schema operations
should handle moving shared instances between array <-> map <-> field:
Error: "refId" not found: 4
at Decoder.decode (src/decoder/Decoder.ts:67:39)
at _.src_1.Schema.decode (test/Schema.ts:81:29)
at encodeDecodeAndAssert (test/Encoder.test.ts:40:26)
at Context.<anonymous> (test/Encoder.test.ts:75:13)
at processImmediate (node:internal/timers:483:21)
2) Encoder
dynamic schema operations
should replace instances multiple times in nested structures:
AssertionError [ERR_ASSERTION]: refCount mismatch for refId=1
1 !== 0
+ expected - actual
-1
+0
at encodeDecodeAndAssert (test/Encoder.test.ts:44:24)
at Context.<anonymous> (test/Encoder.test.ts:108:13)
at processImmediate (node:internal/timers:483:21)
3) Encoder
dynamic schema operations
should survive clear & repopulate on ArraySchema with shared children:
Error: "refId" not found: 1
at Decoder.decode (src/decoder/Decoder.ts:67:39)
at _.src_1.Schema.decode (test/Schema.ts:81:29)
at encodeDecodeAndAssert (test/Encoder.test.ts:40:26)
at Context.<anonymous> (test/Encoder.test.ts:139:13)
at processImmediate (node:internal/timers:483:21)