@colyseus/schema 4.0.5 → 4.0.6

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/build/index.js CHANGED
@@ -933,7 +933,11 @@
933
933
  });
934
934
  }
935
935
  }
936
- constructor[Symbol.metadata] = metadata;
936
+ Object.defineProperty(constructor, Symbol.metadata, {
937
+ value: metadata,
938
+ writable: false,
939
+ configurable: true
940
+ });
937
941
  return metadata;
938
942
  },
939
943
  isValidInstance(klass) {
@@ -3555,7 +3559,12 @@
3555
3559
  }
3556
3560
  else if (value['type'] !== undefined && Schema.is(value['type'])) {
3557
3561
  // Direct Schema type: Type → new Type()
3558
- defaultValues[fieldName] = new value['type']();
3562
+ if (!value['type'].prototype.initialize || value['type'].prototype.initialize.length === 0) {
3563
+ // only auto-initialize Schema instances if:
3564
+ // - they don't have an initialize method
3565
+ // - or initialize method doesn't accept any parameters
3566
+ defaultValues[fieldName] = new value['type']();
3567
+ }
3559
3568
  }
3560
3569
  }
3561
3570
  else {
@@ -3565,7 +3574,12 @@
3565
3574
  else if (typeof (value) === "function") {
3566
3575
  if (Schema.is(value)) {
3567
3576
  // Direct Schema type: Type → new Type()
3568
- defaultValues[fieldName] = new value();
3577
+ if (!value.prototype.initialize || value.prototype.initialize.length === 0) {
3578
+ // only auto-initialize Schema instances if:
3579
+ // - they don't have an initialize method
3580
+ // - or initialize method doesn't accept any parameters
3581
+ defaultValues[fieldName] = new value();
3582
+ }
3569
3583
  fields[fieldName] = getNormalizedType(value);
3570
3584
  }
3571
3585
  else {
@@ -3592,13 +3606,32 @@
3592
3606
  }
3593
3607
  return defaults;
3594
3608
  };
3609
+ const getParentProps = (props) => {
3610
+ const fieldNames = Object.keys(fields);
3611
+ const parentProps = {};
3612
+ for (const key in props) {
3613
+ if (!fieldNames.includes(key)) {
3614
+ parentProps[key] = props[key];
3615
+ }
3616
+ }
3617
+ return parentProps;
3618
+ };
3595
3619
  /** @codegen-ignore */
3596
3620
  const klass = Metadata.setFields(class extends inherits {
3597
3621
  constructor(...args) {
3598
- super(Object.assign({}, getDefaultValues(), args[0] || {}));
3599
3622
  // call initialize method
3600
3623
  if (methods.initialize && typeof methods.initialize === 'function') {
3601
- methods.initialize.apply(this, args);
3624
+ super(Object.assign({}, getDefaultValues(), getParentProps(args[0] || {})));
3625
+ /**
3626
+ * only call initialize() in the current class, not the parent ones.
3627
+ * see "should not call initialize automatically when creating an instance of inherited Schema"
3628
+ */
3629
+ if (new.target === klass) {
3630
+ methods.initialize.apply(this, args);
3631
+ }
3632
+ }
3633
+ else {
3634
+ super(Object.assign({}, getDefaultValues(), args[0] || {}));
3602
3635
  }
3603
3636
  }
3604
3637
  }, fields);
package/build/index.mjs CHANGED
@@ -927,7 +927,11 @@ const Metadata = {
927
927
  });
928
928
  }
929
929
  }
930
- constructor[Symbol.metadata] = metadata;
930
+ Object.defineProperty(constructor, Symbol.metadata, {
931
+ value: metadata,
932
+ writable: false,
933
+ configurable: true
934
+ });
931
935
  return metadata;
932
936
  },
933
937
  isValidInstance(klass) {
@@ -3549,7 +3553,12 @@ function schema(fieldsAndMethods, name, inherits = Schema) {
3549
3553
  }
3550
3554
  else if (value['type'] !== undefined && Schema.is(value['type'])) {
3551
3555
  // Direct Schema type: Type → new Type()
3552
- defaultValues[fieldName] = new value['type']();
3556
+ if (!value['type'].prototype.initialize || value['type'].prototype.initialize.length === 0) {
3557
+ // only auto-initialize Schema instances if:
3558
+ // - they don't have an initialize method
3559
+ // - or initialize method doesn't accept any parameters
3560
+ defaultValues[fieldName] = new value['type']();
3561
+ }
3553
3562
  }
3554
3563
  }
3555
3564
  else {
@@ -3559,7 +3568,12 @@ function schema(fieldsAndMethods, name, inherits = Schema) {
3559
3568
  else if (typeof (value) === "function") {
3560
3569
  if (Schema.is(value)) {
3561
3570
  // Direct Schema type: Type → new Type()
3562
- defaultValues[fieldName] = new value();
3571
+ if (!value.prototype.initialize || value.prototype.initialize.length === 0) {
3572
+ // only auto-initialize Schema instances if:
3573
+ // - they don't have an initialize method
3574
+ // - or initialize method doesn't accept any parameters
3575
+ defaultValues[fieldName] = new value();
3576
+ }
3563
3577
  fields[fieldName] = getNormalizedType(value);
3564
3578
  }
3565
3579
  else {
@@ -3586,13 +3600,32 @@ function schema(fieldsAndMethods, name, inherits = Schema) {
3586
3600
  }
3587
3601
  return defaults;
3588
3602
  };
3603
+ const getParentProps = (props) => {
3604
+ const fieldNames = Object.keys(fields);
3605
+ const parentProps = {};
3606
+ for (const key in props) {
3607
+ if (!fieldNames.includes(key)) {
3608
+ parentProps[key] = props[key];
3609
+ }
3610
+ }
3611
+ return parentProps;
3612
+ };
3589
3613
  /** @codegen-ignore */
3590
3614
  const klass = Metadata.setFields(class extends inherits {
3591
3615
  constructor(...args) {
3592
- super(Object.assign({}, getDefaultValues(), args[0] || {}));
3593
3616
  // call initialize method
3594
3617
  if (methods.initialize && typeof methods.initialize === 'function') {
3595
- methods.initialize.apply(this, args);
3618
+ super(Object.assign({}, getDefaultValues(), getParentProps(args[0] || {})));
3619
+ /**
3620
+ * only call initialize() in the current class, not the parent ones.
3621
+ * see "should not call initialize automatically when creating an instance of inherited Schema"
3622
+ */
3623
+ if (new.target === klass) {
3624
+ methods.initialize.apply(this, args);
3625
+ }
3626
+ }
3627
+ else {
3628
+ super(Object.assign({}, getDefaultValues(), args[0] || {}));
3596
3629
  }
3597
3630
  }
3598
3631
  }, fields);