@colyseus/schema 4.0.15 → 4.0.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@colyseus/schema",
3
- "version": "4.0.15",
3
+ "version": "4.0.17",
4
4
  "description": "Binary state serializer with delta encoding for games",
5
5
  "type": "module",
6
6
  "bin": {
package/src/Schema.ts CHANGED
@@ -122,7 +122,7 @@ export class Schema<C = any> implements IRef {
122
122
  * @param jsonData JSON data to restore the instance from
123
123
  * @returns
124
124
  */
125
- public restore(jsonData: ToJSON<this>): this {
125
+ public restore<T extends Partial<this>>(jsonData: ToJSON<T>): this {
126
126
  const metadata: Metadata = (this.constructor as typeof Schema)[Symbol.metadata];
127
127
 
128
128
  for (const fieldIndex in metadata) {
@@ -224,7 +224,7 @@ export class Schema<C = any> implements IRef {
224
224
  return cloned;
225
225
  }
226
226
 
227
- toJSON (this: any): ToJSON<this> {
227
+ toJSON (this: any): ToJSON<Partial<this>> {
228
228
  const obj: any = {};
229
229
  const metadata = this.constructor[Symbol.metadata];
230
230
  for (const index in metadata) {
@@ -261,14 +261,31 @@ export class StateView {
261
261
 
262
262
  } else {
263
263
  // delete all "tagged" properties.
264
- metadata?.[$viewFieldIndexes]?.forEach((index) =>
265
- changes[index] = OPERATION.DELETE);
264
+ metadata?.[$viewFieldIndexes]?.forEach((index) => {
265
+ changes[index] = OPERATION.DELETE;
266
+
267
+ // Remove child structures of @view() fields from visible set.
268
+ // (They were added during view.add() via forEachChild)
269
+ const value = changeTree.ref[metadata[index].name as keyof Ref];
270
+ if (value?.[$changes]) {
271
+ this.visible.delete(value[$changes]);
272
+ this._recursiveDeleteVisibleChangeTree(value[$changes]);
273
+ }
274
+ });
266
275
  }
267
276
 
268
277
  } else {
269
278
  // delete only tagged properties
270
- metadata?.[$fieldIndexesByViewTag][tag].forEach((index) =>
271
- changes[index] = OPERATION.DELETE);
279
+ metadata?.[$fieldIndexesByViewTag][tag].forEach((index) => {
280
+ changes[index] = OPERATION.DELETE;
281
+
282
+ // Remove child structures from visible set
283
+ const value = changeTree.ref[metadata[index].name as keyof Ref];
284
+ if (value?.[$changes]) {
285
+ this.visible.delete(value[$changes]);
286
+ this._recursiveDeleteVisibleChangeTree(value[$changes]);
287
+ }
288
+ });
272
289
  }
273
290
 
274
291
  // remove tag