@colyseus/schema 3.0.2 → 3.0.4

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)) {
@@ -1859,6 +1859,9 @@ class ArraySchema {
1859
1859
  // type format: { array: "string" }
1860
1860
  (type['array'] !== undefined));
1861
1861
  }
1862
+ static from(iterable) {
1863
+ return new ArraySchema(...Array.from(iterable));
1864
+ }
1862
1865
  constructor(...items) {
1863
1866
  this.items = [];
1864
1867
  this.tmpItems = [];
@@ -2225,8 +2228,8 @@ class ArraySchema {
2225
2228
  lastIndexOf(searchElement, fromIndex = this.length - 1) {
2226
2229
  return this.items.lastIndexOf(searchElement, fromIndex);
2227
2230
  }
2228
- every(callbackfn, thisArg) {
2229
- return this.items.every(callbackfn, thisArg);
2231
+ every(predicate, thisArg) {
2232
+ return this.items.every(predicate, thisArg);
2230
2233
  }
2231
2234
  /**
2232
2235
  * Determines whether the specified callback function returns true for any element of an array.
@@ -2408,6 +2411,9 @@ class ArraySchema {
2408
2411
  //
2409
2412
  with(index, value) {
2410
2413
  const copy = this.items.slice();
2414
+ // Allow negative indexing from the end
2415
+ if (index < 0)
2416
+ index += this.length;
2411
2417
  copy[index] = value;
2412
2418
  return new ArraySchema(...copy);
2413
2419
  }
@@ -4483,7 +4489,10 @@ function getDecoderStateCallbacks(decoder) {
4483
4489
  }
4484
4490
  }
4485
4491
  // trigger onChange
4486
- if (change.value !== change.previousValue) {
4492
+ if (change.value !== change.previousValue &&
4493
+ // FIXME: see "should not encode item if added and removed at the same patch" test case.
4494
+ // some "ADD" + "DELETE" operations on same patch are being encoded as "DELETE"
4495
+ (change.value !== undefined || change.previousValue !== undefined)) {
4487
4496
  const replaceCallbacks = $callbacks[OPERATION.REPLACE];
4488
4497
  for (let i = replaceCallbacks?.length - 1; i >= 0; i--) {
4489
4498
  replaceCallbacks[i](change.value, change.dynamicIndex ?? change.field);
@@ -4596,6 +4605,9 @@ function getDecoderStateCallbacks(decoder) {
4596
4605
  const onRemove = function (ref, callback) {
4597
4606
  return $root.addCallback($root.refIds.get(ref), OPERATION.DELETE, callback);
4598
4607
  };
4608
+ const onChange = function (ref, callback) {
4609
+ return $root.addCallback($root.refIds.get(ref), OPERATION.REPLACE, callback);
4610
+ };
4599
4611
  return new Proxy({
4600
4612
  onAdd: function (callback, immediate = true) {
4601
4613
  //
@@ -4627,6 +4639,19 @@ function getDecoderStateCallbacks(decoder) {
4627
4639
  return onRemove(context.instance, callback);
4628
4640
  }
4629
4641
  },
4642
+ onChange: function (callback) {
4643
+ if (context.onInstanceAvailable) {
4644
+ // collection instance not received yet
4645
+ let detachCallback = () => { };
4646
+ context.onInstanceAvailable((ref) => {
4647
+ detachCallback = onChange(ref, callback);
4648
+ });
4649
+ return () => detachCallback();
4650
+ }
4651
+ else if (context.instance) {
4652
+ return onChange(context.instance, callback);
4653
+ }
4654
+ },
4630
4655
  }, {
4631
4656
  get(target, prop) {
4632
4657
  if (!target[prop]) {