@colyseus/schema 3.0.72 → 3.0.74

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.
@@ -3524,7 +3524,15 @@ function schema(fieldsAndMethods, name, inherits = Schema) {
3524
3524
  // Collection: Set → new SetSchema()
3525
3525
  defaultValues[fieldName] = new SetSchema();
3526
3526
  }
3527
- else if (value['type'] !== undefined && Schema.is(value['type'])) ;
3527
+ else if (value['type'] !== undefined && Schema.is(value['type'])) {
3528
+ // Direct Schema type: Type → new Type()
3529
+ if (!value['type'].prototype.initialize || value['type'].prototype.initialize.length === 0) {
3530
+ // only auto-initialize Schema instances if:
3531
+ // - they don't have an initialize method
3532
+ // - or initialize method doesn't accept any parameters
3533
+ defaultValues[fieldName] = new value['type']();
3534
+ }
3535
+ }
3528
3536
  }
3529
3537
  else {
3530
3538
  defaultValues[fieldName] = value['default'];
@@ -3533,8 +3541,12 @@ function schema(fieldsAndMethods, name, inherits = Schema) {
3533
3541
  else if (typeof (value) === "function") {
3534
3542
  if (Schema.is(value)) {
3535
3543
  // Direct Schema type: Type → new Type()
3536
- // TODO: should we auto-initialize Schema instances, in case their initialize() method is not defined?
3537
- // defaultValues[fieldName] = new value();
3544
+ if (!value.prototype.initialize || value.prototype.initialize.length === 0) {
3545
+ // only auto-initialize Schema instances if:
3546
+ // - they don't have an initialize method
3547
+ // - or initialize method doesn't accept any parameters
3548
+ defaultValues[fieldName] = new value();
3549
+ }
3538
3550
  fields[fieldName] = getNormalizedType(value);
3539
3551
  }
3540
3552
  else {
@@ -3577,7 +3589,13 @@ function schema(fieldsAndMethods, name, inherits = Schema) {
3577
3589
  // call initialize method
3578
3590
  if (methods.initialize && typeof methods.initialize === 'function') {
3579
3591
  super(Object.assign({}, getDefaultValues(), getParentProps(args[0] || {})));
3580
- methods.initialize.apply(this, args);
3592
+ /**
3593
+ * only call initialize() in the current class, not the parent ones.
3594
+ * see "should not call initialize automatically when creating an instance of inherited Schema"
3595
+ */
3596
+ if (new.target === klass) {
3597
+ methods.initialize.apply(this, args);
3598
+ }
3581
3599
  }
3582
3600
  else {
3583
3601
  super(Object.assign({}, getDefaultValues(), args[0] || {}));