@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.
@@ -1545,7 +1545,7 @@ function decodeValue(decoder, operation, ref, index, type, bytes, it, allChanges
1545
1545
  // previousValue,
1546
1546
  // });
1547
1547
  }
1548
- value = null;
1548
+ value = undefined;
1549
1549
  }
1550
1550
  if (operation === OPERATION.DELETE) ;
1551
1551
  else if (Schema.is(type)) {
@@ -4483,7 +4483,10 @@ function getDecoderStateCallbacks(decoder) {
4483
4483
  }
4484
4484
  }
4485
4485
  // trigger onChange
4486
- if (change.value !== change.previousValue) {
4486
+ if (change.value !== change.previousValue &&
4487
+ // FIXME: see "should not encode item if added and removed at the same patch" test case.
4488
+ // some "ADD" + "DELETE" operations on same patch are being encoded as "DELETE"
4489
+ (change.value !== undefined || change.previousValue !== undefined)) {
4487
4490
  const replaceCallbacks = $callbacks[OPERATION.REPLACE];
4488
4491
  for (let i = replaceCallbacks?.length - 1; i >= 0; i--) {
4489
4492
  replaceCallbacks[i](change.value, change.dynamicIndex ?? change.field);
@@ -4596,6 +4599,9 @@ function getDecoderStateCallbacks(decoder) {
4596
4599
  const onRemove = function (ref, callback) {
4597
4600
  return $root.addCallback($root.refIds.get(ref), OPERATION.DELETE, callback);
4598
4601
  };
4602
+ const onChange = function (ref, callback) {
4603
+ return $root.addCallback($root.refIds.get(ref), OPERATION.REPLACE, callback);
4604
+ };
4599
4605
  return new Proxy({
4600
4606
  onAdd: function (callback, immediate = true) {
4601
4607
  //
@@ -4627,6 +4633,19 @@ function getDecoderStateCallbacks(decoder) {
4627
4633
  return onRemove(context.instance, callback);
4628
4634
  }
4629
4635
  },
4636
+ onChange: function (callback) {
4637
+ if (context.onInstanceAvailable) {
4638
+ // collection instance not received yet
4639
+ let detachCallback = () => { };
4640
+ context.onInstanceAvailable((ref) => {
4641
+ detachCallback = onChange(ref, callback);
4642
+ });
4643
+ return () => detachCallback();
4644
+ }
4645
+ else if (context.instance) {
4646
+ return onChange(context.instance, callback);
4647
+ }
4648
+ },
4630
4649
  }, {
4631
4650
  get(target, prop) {
4632
4651
  if (!target[prop]) {