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