@colyseus/schema 3.0.1 → 3.0.3

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/README.md CHANGED
@@ -9,13 +9,14 @@
9
9
 
10
10
  # Features
11
11
 
12
- - Flexible Schema Definition
13
- - Optimized Data Encoding
14
- - Automatic State Synchronization
15
- - Client-side Change Detection
16
- - Per-client portions of the state
17
- - Type Safety
18
- - *...decoders available for multiple languages (C#, Lua, Haxe)*
12
+ - **Incremental State Synchronization**: Send only the properties that have changed.
13
+ - **Trigger Callbacks at Decoding**: [Bring your own](https://docs.colyseus.io/state/callbacks/custom) callback system at decoding, or use the built-in one.
14
+ - **Instance Reference Tracking**: Share references of the same instance across the state.
15
+ - **State Views**: Filter properties that should be sent only to specific clients.
16
+ - **Reflection**: Encode/Decode schema definitions.
17
+ - **Schema Generation**: Generate client-side schema files for strictly typed languages.
18
+ - **Type Safety**: Strictly typed schema definitions.
19
+ - **Multiple Language Support**: Decoders available for multiple languages ([C#](https://github.com/colyseus/colyseus-unity-sdk/tree/master/Assets/Colyseus/Runtime/Colyseus/Serializer/Schema), [Lua](https://github.com/colyseus/colyseus-defold/tree/master/colyseus/serializer/schema), [Haxe](https://github.com/colyseus/colyseus-haxe/tree/master/src/io/colyseus/serializer/schema)).
19
20
 
20
21
  ## Schema definition
21
22
 
@@ -1547,7 +1547,7 @@ function decodeValue(decoder, operation, ref, index, type, bytes, it, allChanges
1547
1547
  // previousValue,
1548
1548
  // });
1549
1549
  }
1550
- value = null;
1550
+ value = undefined;
1551
1551
  }
1552
1552
  if (operation === exports.OPERATION.DELETE) ;
1553
1553
  else if (Schema.is(type)) {
@@ -4485,7 +4485,10 @@ function getDecoderStateCallbacks(decoder) {
4485
4485
  }
4486
4486
  }
4487
4487
  // trigger onChange
4488
- if (change.value !== change.previousValue) {
4488
+ if (change.value !== change.previousValue &&
4489
+ // FIXME: see "should not encode item if added and removed at the same patch" test case.
4490
+ // some "ADD" + "DELETE" operations on same patch are being encoded as "DELETE"
4491
+ (change.value !== undefined || change.previousValue !== undefined)) {
4489
4492
  const replaceCallbacks = $callbacks[exports.OPERATION.REPLACE];
4490
4493
  for (let i = replaceCallbacks?.length - 1; i >= 0; i--) {
4491
4494
  replaceCallbacks[i](change.value, change.dynamicIndex ?? change.field);
@@ -4598,6 +4601,9 @@ function getDecoderStateCallbacks(decoder) {
4598
4601
  const onRemove = function (ref, callback) {
4599
4602
  return $root.addCallback($root.refIds.get(ref), exports.OPERATION.DELETE, callback);
4600
4603
  };
4604
+ const onChange = function (ref, callback) {
4605
+ return $root.addCallback($root.refIds.get(ref), exports.OPERATION.REPLACE, callback);
4606
+ };
4601
4607
  return new Proxy({
4602
4608
  onAdd: function (callback, immediate = true) {
4603
4609
  //
@@ -4629,6 +4635,19 @@ function getDecoderStateCallbacks(decoder) {
4629
4635
  return onRemove(context.instance, callback);
4630
4636
  }
4631
4637
  },
4638
+ onChange: function (callback) {
4639
+ if (context.onInstanceAvailable) {
4640
+ // collection instance not received yet
4641
+ let detachCallback = () => { };
4642
+ context.onInstanceAvailable((ref) => {
4643
+ detachCallback = onChange(ref, callback);
4644
+ });
4645
+ return () => detachCallback();
4646
+ }
4647
+ else if (context.instance) {
4648
+ return onChange(context.instance, callback);
4649
+ }
4650
+ },
4632
4651
  }, {
4633
4652
  get(target, prop) {
4634
4653
  if (!target[prop]) {