@colyseus/schema 4.0.22 → 4.0.24

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.
@@ -41,7 +41,7 @@ export declare class StateCallbackStrategy<TState extends IRef> {
41
41
  /**
42
42
  * Listen to any property change on an instance.
43
43
  */
44
- onChange<TInstance extends Schema>(instance: TInstance, handler: InstanceChangeCallback): () => void;
44
+ onChange<TInstance extends object>(instance: TInstance, handler: InstanceChangeCallback): () => void;
45
45
  /**
46
46
  * Listen to item changes in a collection on root state.
47
47
  */
@@ -49,7 +49,7 @@ export declare class StateCallbackStrategy<TState extends IRef> {
49
49
  /**
50
50
  * Listen to item changes in a nested collection.
51
51
  */
52
- onChange<TInstance extends Schema, K extends CollectionPropNames<TInstance>>(instance: TInstance, property: K, handler: KeyValueCallback<CollectionKeyType<TInstance, K>, CollectionValueType<TInstance, K>>): () => void;
52
+ onChange<TInstance extends object, K extends CollectionPropNames<TInstance>>(instance: TInstance, property: K, handler: KeyValueCallback<CollectionKeyType<TInstance, K>, CollectionValueType<TInstance, K>>): () => void;
53
53
  /**
54
54
  * Listen to items added to a collection on root state.
55
55
  */
@@ -57,7 +57,7 @@ export declare class StateCallbackStrategy<TState extends IRef> {
57
57
  /**
58
58
  * Listen to items added to a nested collection.
59
59
  */
60
- onAdd<TInstance extends Schema, K extends CollectionPropNames<TInstance>>(instance: TInstance, property: K, handler: ValueKeyCallback<CollectionValueType<TInstance, K>, CollectionKeyType<TInstance, K>>, immediate?: boolean): () => void;
60
+ onAdd<TInstance, K extends CollectionPropNames<TInstance>>(instance: TInstance, property: K, handler: ValueKeyCallback<CollectionValueType<TInstance, K>, CollectionKeyType<TInstance, K>>, immediate?: boolean): () => void;
61
61
  /**
62
62
  * Listen to items removed from a collection on root state.
63
63
  */
@@ -65,12 +65,12 @@ export declare class StateCallbackStrategy<TState extends IRef> {
65
65
  /**
66
66
  * Listen to items removed from a nested collection.
67
67
  */
68
- onRemove<TInstance extends Schema, K extends CollectionPropNames<TInstance>>(instance: TInstance, property: K, handler: ValueKeyCallback<CollectionValueType<TInstance, K>, CollectionKeyType<TInstance, K>>): () => void;
68
+ onRemove<TInstance, K extends CollectionPropNames<TInstance>>(instance: TInstance, property: K, handler: ValueKeyCallback<CollectionValueType<TInstance, K>, CollectionKeyType<TInstance, K>>): () => void;
69
69
  /**
70
70
  * Bind properties from a Schema instance to a target object.
71
71
  * Changes will be automatically reflected on the target object.
72
72
  */
73
- bindTo<TInstance extends Schema, TTarget>(from: TInstance, to: TTarget, properties?: string[], immediate?: boolean): () => void;
73
+ bindTo<TInstance, TTarget>(from: TInstance, to: TTarget, properties?: string[], immediate?: boolean): () => void;
74
74
  protected triggerChanges(allChanges: DataChange[]): void;
75
75
  }
76
76
  /**
package/build/index.cjs CHANGED
@@ -1231,12 +1231,26 @@ class ChangeTree {
1231
1231
  }
1232
1232
  return;
1233
1233
  }
1234
- const changeSet = (this.filteredChanges !== undefined)
1234
+ // Mirror `change()`: a field's add/delete must target the same
1235
+ // changeset family (filtered vs non-filtered). Otherwise
1236
+ // `deleteOperationAtIndex` falls through to its "find last
1237
+ // operation" branch and evicts an unrelated sibling field —
1238
+ // surfaced as encodeAll() dropping a non-@view field after a
1239
+ // sibling @view field is set to undefined on a Schema with
1240
+ // mixed @view / non-@view fields (filteredChanges defined,
1241
+ // isFiltered false).
1242
+ const isFiltered = this.isFiltered || (this.metadata?.[index]?.tag !== undefined);
1243
+ const changeSet = (isFiltered)
1235
1244
  ? this.filteredChanges
1236
1245
  : this.changes;
1237
1246
  this.indexedOperations[index] = operation ?? exports.OPERATION.DELETE;
1238
1247
  setOperationAtIndex(changeSet, index);
1239
- deleteOperationAtIndex(this.allChanges, allChangesIndex);
1248
+ if (isFiltered) {
1249
+ deleteOperationAtIndex(this.allFilteredChanges, allChangesIndex);
1250
+ }
1251
+ else {
1252
+ deleteOperationAtIndex(this.allChanges, allChangesIndex);
1253
+ }
1240
1254
  const previousValue = this.getValue(index);
1241
1255
  // remove `root` reference
1242
1256
  if (previousValue && previousValue[$changes]) {
@@ -1252,11 +1266,7 @@ class ChangeTree {
1252
1266
  //
1253
1267
  this.root?.remove(previousValue[$changes]);
1254
1268
  }
1255
- //
1256
- // FIXME: this is looking a ugly and repeated
1257
- //
1258
- if (this.filteredChanges !== undefined) {
1259
- deleteOperationAtIndex(this.allFilteredChanges, allChangesIndex);
1269
+ if (isFiltered) {
1260
1270
  this.root?.enqueueChangeTree(this, 'filteredChanges');
1261
1271
  }
1262
1272
  else {
@@ -5534,6 +5544,7 @@ const Callbacks = {
5534
5544
  else if ('decoder' in roomOrDecoder.serializer) {
5535
5545
  return getDecoderStateCallbacks(roomOrDecoder.serializer.decoder);
5536
5546
  }
5547
+ throw new Error('Invalid room or decoder');
5537
5548
  },
5538
5549
  getRawChanges(decoder, callback) {
5539
5550
  return getRawChangesCallback(decoder, callback);