@colyseus/schema 3.0.71 → 3.0.73

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.
@@ -3528,7 +3528,15 @@
3528
3528
  // Collection: Set → new SetSchema()
3529
3529
  defaultValues[fieldName] = new SetSchema();
3530
3530
  }
3531
- else if (value['type'] !== undefined && Schema.is(value['type'])) ;
3531
+ else if (value['type'] !== undefined && Schema.is(value['type'])) {
3532
+ // Direct Schema type: Type → new Type()
3533
+ if (!value['type'].prototype.initialize || value['type'].prototype.initialize.length === 0) {
3534
+ // only auto-initialize Schema instances if:
3535
+ // - they don't have an initialize method
3536
+ // - or initialize method doesn't accept any parameters
3537
+ defaultValues[fieldName] = new value['type']();
3538
+ }
3539
+ }
3532
3540
  }
3533
3541
  else {
3534
3542
  defaultValues[fieldName] = value['default'];
@@ -3537,8 +3545,12 @@
3537
3545
  else if (typeof (value) === "function") {
3538
3546
  if (Schema.is(value)) {
3539
3547
  // Direct Schema type: Type → new Type()
3540
- // TODO: should we auto-initialize Schema instances, in case their initialize() method is not defined?
3541
- // defaultValues[fieldName] = new value();
3548
+ if (!value.prototype.initialize || value.prototype.initialize.length === 0) {
3549
+ // only auto-initialize Schema instances if:
3550
+ // - they don't have an initialize method
3551
+ // - or initialize method doesn't accept any parameters
3552
+ defaultValues[fieldName] = new value();
3553
+ }
3542
3554
  fields[fieldName] = getNormalizedType(value);
3543
3555
  }
3544
3556
  else {
@@ -3565,14 +3577,27 @@
3565
3577
  }
3566
3578
  return defaults;
3567
3579
  };
3580
+ const getParentProps = (props) => {
3581
+ const fieldNames = Object.keys(fields);
3582
+ const parentProps = {};
3583
+ for (const key in props) {
3584
+ if (!fieldNames.includes(key)) {
3585
+ parentProps[key] = props[key];
3586
+ }
3587
+ }
3588
+ return parentProps;
3589
+ };
3568
3590
  /** @codegen-ignore */
3569
3591
  const klass = Metadata.setFields(class extends inherits {
3570
3592
  constructor(...args) {
3571
- super(Object.assign({}, getDefaultValues(), args[0] || {}));
3572
3593
  // call initialize method
3573
3594
  if (methods.initialize && typeof methods.initialize === 'function') {
3595
+ super(Object.assign({}, getDefaultValues(), getParentProps(args[0] || {})));
3574
3596
  methods.initialize.apply(this, args);
3575
3597
  }
3598
+ else {
3599
+ super(Object.assign({}, getDefaultValues(), args[0] || {}));
3600
+ }
3576
3601
  }
3577
3602
  }, fields);
3578
3603
  // Store the getDefaultValues function on the class for inheritance
@@ -418,8 +418,12 @@ function schema(fieldsAndMethods, name, inherits = Schema_1.Schema) {
418
418
  }
419
419
  else if (value['type'] !== undefined && Schema_1.Schema.is(value['type'])) {
420
420
  // Direct Schema type: Type → new Type()
421
- // TODO: should we auto-initialize Schema instances, in case their initialize() method is not defined?
422
- // defaultValues[fieldName] = new value['type']();
421
+ if (!value['type'].prototype.initialize || value['type'].prototype.initialize.length === 0) {
422
+ // only auto-initialize Schema instances if:
423
+ // - they don't have an initialize method
424
+ // - or initialize method doesn't accept any parameters
425
+ defaultValues[fieldName] = new value['type']();
426
+ }
423
427
  }
424
428
  }
425
429
  else {
@@ -429,8 +433,12 @@ function schema(fieldsAndMethods, name, inherits = Schema_1.Schema) {
429
433
  else if (typeof (value) === "function") {
430
434
  if (Schema_1.Schema.is(value)) {
431
435
  // Direct Schema type: Type → new Type()
432
- // TODO: should we auto-initialize Schema instances, in case their initialize() method is not defined?
433
- // defaultValues[fieldName] = new value();
436
+ if (!value.prototype.initialize || value.prototype.initialize.length === 0) {
437
+ // only auto-initialize Schema instances if:
438
+ // - they don't have an initialize method
439
+ // - or initialize method doesn't accept any parameters
440
+ defaultValues[fieldName] = new value();
441
+ }
434
442
  fields[fieldName] = (0, Metadata_1.getNormalizedType)(value);
435
443
  }
436
444
  else {
@@ -457,14 +465,27 @@ function schema(fieldsAndMethods, name, inherits = Schema_1.Schema) {
457
465
  }
458
466
  return defaults;
459
467
  };
468
+ const getParentProps = (props) => {
469
+ const fieldNames = Object.keys(fields);
470
+ const parentProps = {};
471
+ for (const key in props) {
472
+ if (!fieldNames.includes(key)) {
473
+ parentProps[key] = props[key];
474
+ }
475
+ }
476
+ return parentProps;
477
+ };
460
478
  /** @codegen-ignore */
461
479
  const klass = Metadata_1.Metadata.setFields(class extends inherits {
462
480
  constructor(...args) {
463
- super(Object.assign({}, getDefaultValues(), args[0] || {}));
464
481
  // call initialize method
465
482
  if (methods.initialize && typeof methods.initialize === 'function') {
483
+ super(Object.assign({}, getDefaultValues(), getParentProps(args[0] || {})));
466
484
  methods.initialize.apply(this, args);
467
485
  }
486
+ else {
487
+ super(Object.assign({}, getDefaultValues(), args[0] || {}));
488
+ }
468
489
  }
469
490
  }, fields);
470
491
  // Store the getDefaultValues function on the class for inheritance
@@ -1 +1 @@
1
- {"version":3,"file":"annotations.js","sourceRoot":"","sources":["../src/annotations.ts"],"names":[],"mappings":";;;AAiDA,wBAGC;AAwKD,oBAyBC;AAED,gCA0BC;AAED,oBAmFC;AAED,sDA2EC;AAOD,gCA2CC;AAED,kCASC;AAyDD,wBAoHC;AA7pBD,yBAAuB;AACvB,qCAAkC;AAClC,4DAAyD;AACzD,wDAAqD;AACrD,yCAAyD;AACzD,6CAAyF;AACzF,+CAA2D;AAC3D,0CAA4C;AAC5C,qDAAkD;AAClD,8CAAmE;AAEnE,sEAAmE;AACnE,wDAAqD;AAmCxC,QAAA,gBAAgB,GAAG,CAAC,CAAC,CAAC;AAEnC,SAAgB,MAAM,CAAC,WAAgB;IACnC,yBAAW,CAAC,QAAQ,CAAC,WAA4B,CAAC,CAAC;IACnD,OAAO,WAAW,CAAC;AACvB,CAAC;AAED;;;;;;;;;;;;;;;GAeG;AACH,sEAAsE;AACtE,0HAA0H;AAC1H,6CAA6C;AAC7C,0FAA0F;AAC1F,YAAY;AAEZ,iDAAiD;AAEjD,aAAa;AACb,kEAAkE;AAClE,aAAa;AACb,kEAAkE;AAClE,gHAAgH;AAChH,uFAAuF;AACvF,0CAA0C;AAC1C,wBAAwB;AAExB,eAAe;AACf,mFAAmF;AACnF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AAY,GAAG,IAAW;YACtB,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,EAAE,gBAAgB,EAAE,EAAE,IAAI,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC;YAE5D,yBAAyB;YACzB,IAAI,OAAO,CAAC,UAAU,IAAI,OAAO,OAAO,CAAC,UAAU,KAAK,UAAU,EAAE,CAAC;gBACjE,OAAO,CAAC,UAAU,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;YACzC,CAAC;QACL,CAAC;KACJ,EAAE,MAAM,CAA4D,CAAC;IAEtE,mEAAmE;IAClE,KAAa,CAAC,iBAAiB,GAAG,gBAAgB,CAAC;IAEpD,+BAA+B;IAC/B,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC;IAExC,KAAK,IAAI,SAAS,IAAI,aAAa,EAAE,CAAC;QAClC,IAAI,CAAC,aAAa,CAAC,SAAS,CAAC,CAAC,CAAC,KAAK,CAAC,SAAS,EAAE,SAAS,CAAC,CAAC;IAC/D,CAAC;IAED,IAAI,IAAI,EAAE,CAAC;QACP,MAAM,CAAC,cAAc,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;IAC1D,CAAC;IAED,KAAK,CAAC,OAAO,GAAG,CAAqC,MAAU,EAAE,IAAa,EAAE,EAAE,CAC9E,MAAM,CAAK,MAAM,EAAE,IAAI,EAAE,KAAY,CAAkE,CAAC;IAE5G,OAAO,KAAK,CAAC;AACjB,CAAC","sourcesContent":["import \"./symbol.shim\";\nimport { Schema } from './Schema';\nimport { ArraySchema } from './types/custom/ArraySchema';\nimport { MapSchema } from './types/custom/MapSchema';\nimport { getNormalizedType, Metadata } from \"./Metadata\";\nimport { $changes, $childType, $descriptors, $numFields, $track } from \"./types/symbols\";\nimport { TypeDefinition, getType } from \"./types/registry\";\nimport { OPERATION } from \"./encoding/spec\";\nimport { TypeContext } from \"./types/TypeContext\";\nimport { assertInstanceType, assertType } from \"./encoding/assert\";\nimport type { InferValueType, InferSchemaInstanceType, AssignableProps, IsNever } from \"./types/HelperTypes\";\nimport { CollectionSchema } from \"./types/custom/CollectionSchema\";\nimport { SetSchema } from \"./types/custom/SetSchema\";\n\nexport type RawPrimitiveType = \"string\" |\n \"number\" |\n \"boolean\" |\n \"int8\" |\n \"uint8\" |\n \"int16\" |\n \"uint16\" |\n \"int32\" |\n \"uint32\" |\n \"int64\" |\n \"uint64\" |\n \"float32\" |\n \"float64\" |\n \"bigint64\" |\n \"biguint64\";\n\nexport type PrimitiveType = RawPrimitiveType | typeof Schema | object;\n\n// TODO: infer \"default\" value type correctly.\nexport type DefinitionType<T extends PrimitiveType = PrimitiveType> = T\n | T[]\n | { type: T, default?: InferValueType<T>, view?: boolean | number }\n | { array: T, default?: ArraySchema<InferValueType<T>>, view?: boolean | number }\n | { map: T, default?: MapSchema<InferValueType<T>>, view?: boolean | number }\n | { collection: T, default?: CollectionSchema<InferValueType<T>>, view?: boolean | number }\n | { set: T, default?: SetSchema<InferValueType<T>>, view?: boolean | number };\n\nexport type Definition = { [field: string]: DefinitionType };\n\nexport interface TypeOptions {\n manual?: boolean,\n}\n\nexport const DEFAULT_VIEW_TAG = -1;\n\nexport function entity(constructor: any): any {\n TypeContext.register(constructor as typeof Schema);\n return constructor;\n}\n\n/**\n * [See documentation](https://docs.colyseus.io/state/schema/)\n *\n * Annotate a Schema property to be serializeable.\n * \\@type()'d fields are automatically flagged as \"dirty\" for the next patch.\n *\n * @example Standard usage, with automatic change tracking.\n * ```\n * \\@type(\"string\") propertyName: string;\n * ```\n *\n * @example You can provide the \"manual\" option if you'd like to manually control your patches via .setDirty().\n * ```\n * \\@type(\"string\", { manual: true })\n * ```\n */\n// export function type(type: DefinitionType, options?: TypeOptions) {\n// return function ({ get, set }, context: ClassAccessorDecoratorContext): ClassAccessorDecoratorResult<Schema, any> {\n// if (context.kind !== \"accessor\") {\n// throw new Error(\"@type() is only supported for class accessor properties\");\n// }\n\n// const field = context.name.toString();\n\n// //\n// // detect index for this field, considering inheritance\n// //\n// const parent = Object.getPrototypeOf(context.metadata);\n// let fieldIndex: number = context.metadata[$numFields] // current structure already has fields defined\n// ?? (parent && parent[$numFields]) // parent structure has fields defined\n// ?? -1; // no fields defined\n// fieldIndex++;\n\n// if (\n// !parent && // the parent already initializes the `$changes` property\n// !Metadata.hasFields(context.metadata)\n// ) {\n// context.addInitializer(function (this: Ref) {\n// Object.defineProperty(this, $changes, {\n// value: new ChangeTree(this),\n// enumerable: false,\n// writable: true\n// });\n// });\n// }\n\n// Metadata.addField(context.metadata, fieldIndex, field, type);\n\n// const isArray = ArraySchema.is(type);\n// const isMap = !isArray && MapSchema.is(type);\n\n// // if (options && options.manual) {\n// // // do not declare getter/setter descriptor\n// // definition.descriptors[field] = {\n// // enumerable: true,\n// // configurable: true,\n// // writable: true,\n// // };\n// // return;\n// // }\n\n// return {\n// init(value) {\n// // TODO: may need to convert ArraySchema/MapSchema here\n\n// // do not flag change if value is undefined.\n// if (value !== undefined) {\n// this[$changes].change(fieldIndex);\n\n// // automaticallty transform Array into ArraySchema\n// if (isArray) {\n// if (!(value instanceof ArraySchema)) {\n// value = new ArraySchema(...value);\n// }\n// value[$childType] = Object.values(type)[0];\n// }\n\n// // automaticallty transform Map into MapSchema\n// if (isMap) {\n// if (!(value instanceof MapSchema)) {\n// value = new MapSchema(value);\n// }\n// value[$childType] = Object.values(type)[0];\n// }\n\n// // try to turn provided structure into a Proxy\n// if (value['$proxy'] === undefined) {\n// if (isMap) {\n// value = getMapProxy(value);\n// }\n// }\n\n// }\n\n// return value;\n// },\n\n// get() {\n// return get.call(this);\n// },\n\n// set(value: any) {\n// /**\n// * Create Proxy for array or map items\n// */\n\n// // skip if value is the same as cached.\n// if (value === get.call(this)) {\n// return;\n// }\n\n// if (\n// value !== undefined &&\n// value !== null\n// ) {\n// // automaticallty transform Array into ArraySchema\n// if (isArray) {\n// if (!(value instanceof ArraySchema)) {\n// value = new ArraySchema(...value);\n// }\n// value[$childType] = Object.values(type)[0];\n// }\n\n// // automaticallty transform Map into MapSchema\n// if (isMap) {\n// if (!(value instanceof MapSchema)) {\n// value = new MapSchema(value);\n// }\n// value[$childType] = Object.values(type)[0];\n// }\n\n// // try to turn provided structure into a Proxy\n// if (value['$proxy'] === undefined) {\n// if (isMap) {\n// value = getMapProxy(value);\n// }\n// }\n\n// // flag the change for encoding.\n// this[$changes].change(fieldIndex);\n\n// //\n// // call setParent() recursively for this and its child\n// // structures.\n// //\n// if (value[$changes]) {\n// value[$changes].setParent(\n// this,\n// this[$changes].root,\n// Metadata.getIndex(context.metadata, field),\n// );\n// }\n\n// } else if (get.call(this)) {\n// //\n// // Setting a field to `null` or `undefined` will delete it.\n// //\n// this[$changes].delete(field);\n// }\n\n// set.call(this, value);\n// },\n// };\n// }\n// }\n\nexport function view<T> (tag: number = DEFAULT_VIEW_TAG) {\n return function(target: T, fieldName: string) {\n const constructor = target.constructor as typeof Schema;\n\n const parentClass = Object.getPrototypeOf(constructor);\n const parentMetadata = parentClass[Symbol.metadata];\n\n // TODO: use Metadata.initialize()\n const metadata: Metadata = (constructor[Symbol.metadata] ??= Object.assign({}, constructor[Symbol.metadata], parentMetadata ?? Object.create(null)));\n // const fieldIndex = metadata[fieldName];\n\n // if (!metadata[fieldIndex]) {\n // //\n // // detect index for this field, considering inheritance\n // //\n // metadata[fieldIndex] = {\n // type: undefined,\n // index: (metadata[$numFields] // current structure already has fields defined\n // ?? (parentMetadata && parentMetadata[$numFields]) // parent structure has fields defined\n // ?? -1) + 1 // no fields defined\n // }\n // }\n\n Metadata.setTag(metadata, fieldName, tag);\n }\n}\n\nexport function unreliable<T> (target: T, field: string) {\n //\n // FIXME: the following block of code is repeated across `@type()`, `@deprecated()` and `@unreliable()` decorators.\n //\n const constructor = target.constructor as typeof Schema;\n\n const parentClass = Object.getPrototypeOf(constructor);\n const parentMetadata = parentClass[Symbol.metadata];\n\n // TODO: use Metadata.initialize()\n const metadata: Metadata = (constructor[Symbol.metadata] ??= Object.assign({}, constructor[Symbol.metadata], parentMetadata ?? Object.create(null)));\n\n // if (!metadata[field]) {\n // //\n // // detect index for this field, considering inheritance\n // //\n // metadata[field] = {\n // type: undefined,\n // index: (metadata[$numFields] // current structure already has fields defined\n // ?? (parentMetadata && parentMetadata[$numFields]) // parent structure has fields defined\n // ?? -1) + 1 // no fields defined\n // }\n // }\n\n // add owned flag to the field\n metadata[metadata[field]].unreliable = true;\n}\n\nexport function type (\n type: DefinitionType,\n options?: TypeOptions\n): PropertyDecorator {\n return function (target: typeof Schema, field: string) {\n const constructor = target.constructor as typeof Schema;\n\n if (!type) {\n throw new Error(`${constructor.name}: @type() reference provided for \"${field}\" is undefined. Make sure you don't have any circular dependencies.`);\n }\n\n // Normalize type (enum/collection/etc)\n type = getNormalizedType(type);\n\n // for inheritance support\n TypeContext.register(constructor);\n\n const parentClass = Object.getPrototypeOf(constructor);\n const parentMetadata = parentClass[Symbol.metadata];\n const metadata = Metadata.initialize(constructor);\n\n let fieldIndex: number = metadata[field];\n\n /**\n * skip if descriptor already exists for this field (`@deprecated()`)\n */\n if (metadata[fieldIndex] !== undefined) {\n if (metadata[fieldIndex].deprecated) {\n // do not create accessors for deprecated properties.\n return;\n\n } else if (metadata[fieldIndex].type !== undefined) {\n // trying to define same property multiple times across inheritance.\n // https://github.com/colyseus/colyseus-unity3d/issues/131#issuecomment-814308572\n try {\n throw new Error(`@colyseus/schema: Duplicate '${field}' definition on '${constructor.name}'.\\nCheck @type() annotation`);\n\n } catch (e) {\n const definitionAtLine = e.stack.split(\"\\n\")[4].trim();\n throw new Error(`${e.message} ${definitionAtLine}`);\n }\n }\n\n } else {\n //\n // detect index for this field, considering inheritance\n //\n fieldIndex = metadata[$numFields] // current structure already has fields defined\n ?? (parentMetadata && parentMetadata[$numFields]) // parent structure has fields defined\n ?? -1; // no fields defined\n fieldIndex++;\n }\n\n if (options && options.manual) {\n Metadata.addField(\n metadata,\n fieldIndex,\n field,\n type,\n {\n // do not declare getter/setter descriptor\n enumerable: true,\n configurable: true,\n writable: true,\n }\n );\n\n } else {\n const complexTypeKlass = typeof(Object.keys(type)[0]) === \"string\" && getType(Object.keys(type)[0]);\n\n const childType = (complexTypeKlass)\n ? Object.values(type)[0]\n : type;\n\n Metadata.addField(\n metadata,\n fieldIndex,\n field,\n type,\n getPropertyDescriptor(`_${field}`, fieldIndex, childType, complexTypeKlass)\n );\n }\n }\n}\n\nexport function getPropertyDescriptor(\n fieldCached: string,\n fieldIndex: number,\n type: DefinitionType,\n complexTypeKlass: TypeDefinition,\n) {\n return {\n get: function (this: Schema) { return this[fieldCached as keyof Schema]; },\n set: function (this: Schema, value: any) {\n const previousValue = this[fieldCached as keyof Schema] ?? undefined;\n\n // skip if value is the same as cached.\n if (value === previousValue) { return; }\n\n if (\n value !== undefined &&\n value !== null\n ) {\n if (complexTypeKlass) {\n // automaticallty transform Array into ArraySchema\n if (complexTypeKlass.constructor === ArraySchema && !(value instanceof ArraySchema)) {\n value = new ArraySchema(...value);\n }\n\n // automaticallty transform Map into MapSchema\n if (complexTypeKlass.constructor === MapSchema && !(value instanceof MapSchema)) {\n value = new MapSchema(value);\n }\n\n // // automaticallty transform Array into SetSchema\n // if (complexTypeKlass.constructor === SetSchema && !(value instanceof SetSchema)) {\n // value = new SetSchema(value);\n // }\n\n value[$childType] = type;\n\n } else if (typeof (type) !== \"string\") {\n assertInstanceType(value, type as typeof Schema, this, fieldCached.substring(1));\n\n } else {\n assertType(value, type, this, fieldCached.substring(1));\n }\n\n const changeTree = this[$changes];\n\n //\n // Replacing existing \"ref\", remove it from root.\n //\n if (previousValue !== undefined && previousValue[$changes]) {\n changeTree.root?.remove(previousValue[$changes]);\n (this.constructor as typeof Schema)[$track](changeTree, fieldIndex, OPERATION.DELETE_AND_ADD);\n\n } else {\n (this.constructor as typeof Schema)[$track](changeTree, fieldIndex, OPERATION.ADD);\n }\n\n //\n // call setParent() recursively for this and its child\n // structures.\n //\n value[$changes]?.setParent(this, changeTree.root, fieldIndex);\n\n } else if (previousValue !== undefined) {\n //\n // Setting a field to `null` or `undefined` will delete it.\n //\n this[$changes].delete(fieldIndex);\n }\n\n this[fieldCached as keyof Schema] = value;\n },\n\n enumerable: true,\n configurable: true\n };\n}\n\n/**\n * `@deprecated()` flag a field as deprecated.\n * The previous `@type()` annotation should remain along with this one.\n */\n\nexport function deprecated(throws: boolean = true): PropertyDecorator {\n return function (klass: typeof Schema, field: string) {\n //\n // FIXME: the following block of code is repeated across `@type()`, `@deprecated()` and `@unreliable()` decorators.\n //\n const constructor = klass.constructor as typeof Schema;\n\n const parentClass = Object.getPrototypeOf(constructor);\n const parentMetadata = parentClass[Symbol.metadata];\n const metadata: Metadata = (constructor[Symbol.metadata] ??= Object.assign({}, constructor[Symbol.metadata], parentMetadata ?? Object.create(null)));\n const fieldIndex = metadata[field];\n\n // if (!metadata[field]) {\n // //\n // // detect index for this field, considering inheritance\n // //\n // metadata[field] = {\n // type: undefined,\n // index: (metadata[$numFields] // current structure already has fields defined\n // ?? (parentMetadata && parentMetadata[$numFields]) // parent structure has fields defined\n // ?? -1) + 1 // no fields defined\n // }\n // }\n\n metadata[fieldIndex].deprecated = true;\n\n if (throws) {\n metadata[$descriptors] ??= {};\n metadata[$descriptors][field] = {\n get: function () { throw new Error(`${field} is deprecated.`); },\n set: function (this: Schema, value: any) { /* throw new Error(`${field} is deprecated.`); */ },\n enumerable: false,\n configurable: true\n };\n }\n\n // flag metadata[field] as non-enumerable\n Object.defineProperty(metadata, fieldIndex, {\n value: metadata[fieldIndex],\n enumerable: false,\n configurable: true\n });\n }\n}\n\nexport function defineTypes(\n target: typeof Schema,\n fields: Definition,\n options?: TypeOptions\n) {\n for (let field in fields) {\n type(fields[field], options)(target.prototype, field);\n }\n return target;\n}\n\n// Helper type to extract InitProps from initialize method\n// Supports both single object parameter and multiple parameters\n// If no initialize method is specified, use AssignableProps for field initialization\ntype ExtractInitProps<T> = T extends { initialize: (...args: infer P) => void }\n ? P extends readonly []\n ? never\n : P extends readonly [infer First]\n ? First extends object\n ? First\n : P\n : P\n : T extends Definition\n ? AssignableProps<InferSchemaInstanceType<T>>\n : never;\n\n// Helper type to determine if InitProps should be required\ntype IsInitPropsRequired<T> = T extends { initialize: (props: any) => void }\n ? true\n : T extends { initialize: (...args: infer P) => void }\n ? P extends readonly []\n ? false\n : true\n : false;\n\nexport interface SchemaWithExtends<T extends Definition, P extends typeof Schema, > {\n extends: <T2 extends Definition = Definition>(\n fields: T2 & ThisType<InferSchemaInstanceType<T & T2>>,\n name?: string\n ) => SchemaWithExtendsConstructor<T & T2, ExtractInitProps<T2>, P>;\n}\n\n/**\n * Get the type of the schema defined via `schema({...})` method.\n *\n * @example\n * const Entity = schema({\n * x: \"number\",\n * y: \"number\",\n * });\n * type Entity = SchemaType<typeof Entity>;\n */\nexport type SchemaType<T extends {'~type': any}> = T['~type'];\n\nexport interface SchemaWithExtendsConstructor<\n T extends Definition,\n InitProps,\n P extends typeof Schema\n> extends SchemaWithExtends<T, P> {\n '~type': InferSchemaInstanceType<T>;\n new (...args: [InitProps] extends [never] ? [] : InitProps extends readonly any[] ? InitProps : IsInitPropsRequired<T> extends true ? [InitProps] : [InitProps?]): InferSchemaInstanceType<T> & InstanceType<P>;\n prototype: InferSchemaInstanceType<T> & InstanceType<P> & {\n initialize(...args: [InitProps] extends [never] ? [] : InitProps extends readonly any[] ? InitProps : [InitProps]): void;\n };\n}\n\nexport function schema<\n T extends Record<string, DefinitionType>,\n P extends typeof Schema = typeof Schema\n>(\n fieldsAndMethods: T & ThisType<InferSchemaInstanceType<T>>,\n name?: string,\n inherits: P = Schema as P\n): SchemaWithExtendsConstructor<T, ExtractInitProps<T>, P> {\n const fields: any = {};\n const methods: any = {};\n\n const defaultValues: any = {};\n const viewTagFields: any = {};\n\n for (let fieldName in fieldsAndMethods) {\n const value: any = fieldsAndMethods[fieldName] as DefinitionType;\n if (typeof (value) === \"object\") {\n if (value['view'] !== undefined) {\n viewTagFields[fieldName] = (typeof (value['view']) === \"boolean\")\n ? DEFAULT_VIEW_TAG\n : value['view'];\n }\n\n fields[fieldName] = getNormalizedType(value);\n\n // If no explicit default provided, handle automatic instantiation for collection types\n if (!Object.prototype.hasOwnProperty.call(value, 'default')) {\n // TODO: remove Array.isArray() check. Use ['array'] !== undefined only.\n if (Array.isArray(value) || value['array'] !== undefined) {\n // Collection: Array → new ArraySchema()\n defaultValues[fieldName] = new ArraySchema();\n\n } else if (value['map'] !== undefined) {\n // Collection: Map → new MapSchema()\n defaultValues[fieldName] = new MapSchema();\n\n } else if (value['collection'] !== undefined) {\n // Collection: Collection → new CollectionSchema()\n defaultValues[fieldName] = new CollectionSchema();\n\n } else if (value['set'] !== undefined) {\n // Collection: Set → new SetSchema()\n defaultValues[fieldName] = new SetSchema();\n\n } else if (value['type'] !== undefined && Schema.is(value['type'])) {\n // Direct Schema type: Type → new Type()\n // TODO: should we auto-initialize Schema instances, in case their initialize() method is not defined?\n // defaultValues[fieldName] = new value['type']();\n }\n } else {\n defaultValues[fieldName] = value['default'];\n }\n\n\n } else if (typeof (value) === \"function\") {\n if (Schema.is(value)) {\n // Direct Schema type: Type → new Type()\n // TODO: should we auto-initialize Schema instances, in case their initialize() method is not defined?\n // defaultValues[fieldName] = new value();\n fields[fieldName] = getNormalizedType(value);\n } else {\n methods[fieldName] = value;\n }\n\n } else {\n fields[fieldName] = getNormalizedType(value);\n }\n }\n\n const getDefaultValues = () => {\n const defaults: any = {};\n\n // use current class default values\n for (const fieldName in defaultValues) {\n const defaultValue = defaultValues[fieldName];\n if (defaultValue && typeof defaultValue.clone === 'function') {\n // complex, cloneable values, e.g. Schema, ArraySchema, MapSchema, CollectionSchema, SetSchema\n defaults[fieldName] = defaultValue.clone();\n } else {\n // primitives and non-cloneable values\n defaults[fieldName] = defaultValue;\n }\n }\n return defaults;\n };\n\n /** @codegen-ignore */\n const klass = Metadata.setFields<any>(class extends (inherits as any) {\n constructor(...args: any[]) {\n super(Object.assign({}, getDefaultValues(), args[0] || {}));\n\n // call initialize method\n if (methods.initialize && typeof methods.initialize === 'function') {\n methods.initialize.apply(this, args);\n }\n }\n }, fields) as SchemaWithExtendsConstructor<T, ExtractInitProps<T>, P>;\n\n // Store the getDefaultValues function on the class for inheritance\n (klass as any)._getDefaultValues = getDefaultValues;\n\n // Add methods to the prototype\n Object.assign(klass.prototype, methods);\n\n for (let fieldName in viewTagFields) {\n view(viewTagFields[fieldName])(klass.prototype, fieldName);\n }\n\n if (name) {\n Object.defineProperty(klass, \"name\", { value: name });\n }\n\n klass.extends = <T2 extends Definition = Definition>(fields: T2, name?: string) =>\n schema<T2>(fields, name, klass as any) as SchemaWithExtendsConstructor<T & T2, ExtractInitProps<T2>, P>;\n\n return klass;\n}\n"]}
1
+ {"version":3,"file":"annotations.js","sourceRoot":"","sources":["../src/annotations.ts"],"names":[],"mappings":";;;AAiDA,wBAGC;AAwKD,oBAyBC;AAED,gCA0BC;AAED,oBAmFC;AAED,sDA2EC;AAOD,gCA2CC;AAED,kCASC;AAyDD,wBAyIC;AAlrBD,yBAAuB;AACvB,qCAAkC;AAClC,4DAAyD;AACzD,wDAAqD;AACrD,yCAAyD;AACzD,6CAAyF;AACzF,+CAA2D;AAC3D,0CAA4C;AAC5C,qDAAkD;AAClD,8CAAmE;AAEnE,sEAAmE;AACnE,wDAAqD;AAmCxC,QAAA,gBAAgB,GAAG,CAAC,CAAC,CAAC;AAEnC,SAAgB,MAAM,CAAC,WAAgB;IACnC,yBAAW,CAAC,QAAQ,CAAC,WAA4B,CAAC,CAAC;IACnD,OAAO,WAAW,CAAC;AACvB,CAAC;AAED;;;;;;;;;;;;;;;GAeG;AACH,sEAAsE;AACtE,0HAA0H;AAC1H,6CAA6C;AAC7C,0FAA0F;AAC1F,YAAY;AAEZ,iDAAiD;AAEjD,aAAa;AACb,kEAAkE;AAClE,aAAa;AACb,kEAAkE;AAClE,gHAAgH;AAChH,uFAAuF;AAC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AAK,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;YAEzC,CAAC;iBAAM,CAAC;gBACJ,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,EAAE,gBAAgB,EAAE,EAAE,IAAI,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC;YAChE,CAAC;QACL,CAAC;KACJ,EAAE,MAAM,CAA4D,CAAC;IAEtE,mEAAmE;IAClE,KAAa,CAAC,iBAAiB,GAAG,gBAAgB,CAAC;IAEpD,+BAA+B;IAC/B,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC;IAExC,KAAK,IAAI,SAAS,IAAI,aAAa,EAAE,CAAC;QAClC,IAAI,CAAC,aAAa,CAAC,SAAS,CAAC,CAAC,CAAC,KAAK,CAAC,SAAS,EAAE,SAAS,CAAC,CAAC;IAC/D,CAAC;IAED,IAAI,IAAI,EAAE,CAAC;QACP,MAAM,CAAC,cAAc,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;IAC1D,CAAC;IAED,KAAK,CAAC,OAAO,GAAG,CAAqC,MAAU,EAAE,IAAa,EAAE,EAAE,CAC9E,MAAM,CAAK,MAAM,EAAE,IAAI,EAAE,KAAY,CAAkE,CAAC;IAE5G,OAAO,KAAK,CAAC;AACjB,CAAC","sourcesContent":["import \"./symbol.shim\";\nimport { Schema } from './Schema';\nimport { ArraySchema } from './types/custom/ArraySchema';\nimport { MapSchema } from './types/custom/MapSchema';\nimport { getNormalizedType, Metadata } from \"./Metadata\";\nimport { $changes, $childType, $descriptors, $numFields, $track } from \"./types/symbols\";\nimport { TypeDefinition, getType } from \"./types/registry\";\nimport { OPERATION } from \"./encoding/spec\";\nimport { TypeContext } from \"./types/TypeContext\";\nimport { assertInstanceType, assertType } from \"./encoding/assert\";\nimport type { InferValueType, InferSchemaInstanceType, AssignableProps, IsNever } from \"./types/HelperTypes\";\nimport { CollectionSchema } from \"./types/custom/CollectionSchema\";\nimport { SetSchema } from \"./types/custom/SetSchema\";\n\nexport type RawPrimitiveType = \"string\" |\n \"number\" |\n \"boolean\" |\n \"int8\" |\n \"uint8\" |\n \"int16\" |\n \"uint16\" |\n \"int32\" |\n \"uint32\" |\n \"int64\" |\n \"uint64\" |\n \"float32\" |\n \"float64\" |\n \"bigint64\" |\n \"biguint64\";\n\nexport type PrimitiveType = RawPrimitiveType | typeof Schema | object;\n\n// TODO: infer \"default\" value type correctly.\nexport type DefinitionType<T extends PrimitiveType = PrimitiveType> = T\n | T[]\n | { type: T, default?: InferValueType<T>, view?: boolean | number }\n | { array: T, default?: ArraySchema<InferValueType<T>>, view?: boolean | number }\n | { map: T, default?: MapSchema<InferValueType<T>>, view?: boolean | number }\n | { collection: T, default?: CollectionSchema<InferValueType<T>>, view?: boolean | number }\n | { set: T, default?: SetSchema<InferValueType<T>>, view?: boolean | number };\n\nexport type Definition = { [field: string]: DefinitionType };\n\nexport interface TypeOptions {\n manual?: boolean,\n}\n\nexport const DEFAULT_VIEW_TAG = -1;\n\nexport function entity(constructor: any): any {\n TypeContext.register(constructor as typeof Schema);\n return constructor;\n}\n\n/**\n * [See documentation](https://docs.colyseus.io/state/schema/)\n *\n * Annotate a Schema property to be serializeable.\n * \\@type()'d fields are automatically flagged as \"dirty\" for the next patch.\n *\n * @example Standard usage, with automatic change tracking.\n * ```\n * \\@type(\"string\") propertyName: string;\n * ```\n *\n * @example You can provide the \"manual\" option if you'd like to manually control your patches via .setDirty().\n * ```\n * \\@type(\"string\", { manual: true })\n * ```\n */\n// export function type(type: DefinitionType, options?: TypeOptions) {\n// return function ({ get, set }, context: ClassAccessorDecoratorContext): ClassAccessorDecoratorResult<Schema, any> {\n// if (context.kind !== \"accessor\") {\n// throw new Error(\"@type() is only supported for class accessor properties\");\n// }\n\n// const field = context.name.toString();\n\n// //\n// // detect index for this field, considering inheritance\n// //\n// const parent = Object.getPrototypeOf(context.metadata);\n// let fieldIndex: number = context.metadata[$numFields] // current structure already has fields defined\n// ?? (parent && parent[$numFields]) // parent structure has fields defined\n// ?? -1; // no fields defined\n// fieldIndex++;\n\n// if (\n// !parent && // the parent already initializes the `$changes` property\n// !Metadata.hasFields(context.metadata)\n// ) {\n// context.addInitializer(function (this: Ref) {\n// Object.defineProperty(this, $changes, {\n// value: new ChangeTree(this),\n// enumerable: false,\n// writable: true\n// });\n// });\n// }\n\n// Metadata.addField(context.metadata, fieldIndex, field, type);\n\n// const isArray = ArraySchema.is(type);\n// const isMap = !isArray && MapSchema.is(type);\n\n// // if (options && options.manual) {\n// // // do not declare getter/setter descriptor\n// // definition.descriptors[field] = {\n// // enumerable: true,\n// // configurable: true,\n// // writable: true,\n// // };\n// // return;\n// // }\n\n// return {\n// init(value) {\n// // TODO: may need to convert ArraySchema/MapSchema here\n\n// // do not flag change if value is undefined.\n// if (value !== undefined) {\n// this[$changes].change(fieldIndex);\n\n// // automaticallty transform Array into ArraySchema\n// if (isArray) {\n// if (!(value instanceof ArraySchema)) {\n// value = new ArraySchema(...value);\n// }\n// value[$childType] = Object.values(type)[0];\n// }\n\n// // automaticallty transform Map into MapSchema\n// if (isMap) {\n// if (!(value instanceof MapSchema)) {\n// value = new MapSchema(value);\n// }\n// value[$childType] = Object.values(type)[0];\n// }\n\n// // try to turn provided structure into a Proxy\n// if (value['$proxy'] === undefined) {\n// if (isMap) {\n// value = getMapProxy(value);\n// }\n// }\n\n// }\n\n// return value;\n// },\n\n// get() {\n// return get.call(this);\n// },\n\n// set(value: any) {\n// /**\n// * Create Proxy for array or map items\n// */\n\n// // skip if value is the same as cached.\n// if (value === get.call(this)) {\n// return;\n// }\n\n// if (\n// value !== undefined &&\n// value !== null\n// ) {\n// // automaticallty transform Array into ArraySchema\n// if (isArray) {\n// if (!(value instanceof ArraySchema)) {\n// value = new ArraySchema(...value);\n// }\n// value[$childType] = Object.values(type)[0];\n// }\n\n// // automaticallty transform Map into MapSchema\n// if (isMap) {\n// if (!(value instanceof MapSchema)) {\n// value = new MapSchema(value);\n// }\n// value[$childType] = Object.values(type)[0];\n// }\n\n// // try to turn provided structure into a Proxy\n// if (value['$proxy'] === undefined) {\n// if (isMap) {\n// value = getMapProxy(value);\n// }\n// }\n\n// // flag the change for encoding.\n// this[$changes].change(fieldIndex);\n\n// //\n// // call setParent() recursively for this and its child\n// // structures.\n// //\n// if (value[$changes]) {\n// value[$changes].setParent(\n// this,\n// this[$changes].root,\n// Metadata.getIndex(context.metadata, field),\n// );\n// }\n\n// } else if (get.call(this)) {\n// //\n// // Setting a field to `null` or `undefined` will delete it.\n// //\n// this[$changes].delete(field);\n// }\n\n// set.call(this, value);\n// },\n// };\n// }\n// }\n\nexport function view<T> (tag: number = DEFAULT_VIEW_TAG) {\n return function(target: T, fieldName: string) {\n const constructor = target.constructor as typeof Schema;\n\n const parentClass = Object.getPrototypeOf(constructor);\n const parentMetadata = parentClass[Symbol.metadata];\n\n // TODO: use Metadata.initialize()\n const metadata: Metadata = (constructor[Symbol.metadata] ??= Object.assign({}, constructor[Symbol.metadata], parentMetadata ?? Object.create(null)));\n // const fieldIndex = metadata[fieldName];\n\n // if (!metadata[fieldIndex]) {\n // //\n // // detect index for this field, considering inheritance\n // //\n // metadata[fieldIndex] = {\n // type: undefined,\n // index: (metadata[$numFields] // current structure already has fields defined\n // ?? (parentMetadata && parentMetadata[$numFields]) // parent structure has fields defined\n // ?? -1) + 1 // no fields defined\n // }\n // }\n\n Metadata.setTag(metadata, fieldName, tag);\n }\n}\n\nexport function unreliable<T> (target: T, field: string) {\n //\n // FIXME: the following block of code is repeated across `@type()`, `@deprecated()` and `@unreliable()` decorators.\n //\n const constructor = target.constructor as typeof Schema;\n\n const parentClass = Object.getPrototypeOf(constructor);\n const parentMetadata = parentClass[Symbol.metadata];\n\n // TODO: use Metadata.initialize()\n const metadata: Metadata = (constructor[Symbol.metadata] ??= Object.assign({}, constructor[Symbol.metadata], parentMetadata ?? Object.create(null)));\n\n // if (!metadata[field]) {\n // //\n // // detect index for this field, considering inheritance\n // //\n // metadata[field] = {\n // type: undefined,\n // index: (metadata[$numFields] // current structure already has fields defined\n // ?? (parentMetadata && parentMetadata[$numFields]) // parent structure has fields defined\n // ?? -1) + 1 // no fields defined\n // }\n // }\n\n // add owned flag to the field\n metadata[metadata[field]].unreliable = true;\n}\n\nexport function type (\n type: DefinitionType,\n options?: TypeOptions\n): PropertyDecorator {\n return function (target: typeof Schema, field: string) {\n const constructor = target.constructor as typeof Schema;\n\n if (!type) {\n throw new Error(`${constructor.name}: @type() reference provided for \"${field}\" is undefined. Make sure you don't have any circular dependencies.`);\n }\n\n // Normalize type (enum/collection/etc)\n type = getNormalizedType(type);\n\n // for inheritance support\n TypeContext.register(constructor);\n\n const parentClass = Object.getPrototypeOf(constructor);\n const parentMetadata = parentClass[Symbol.metadata];\n const metadata = Metadata.initialize(constructor);\n\n let fieldIndex: number = metadata[field];\n\n /**\n * skip if descriptor already exists for this field (`@deprecated()`)\n */\n if (metadata[fieldIndex] !== undefined) {\n if (metadata[fieldIndex].deprecated) {\n // do not create accessors for deprecated properties.\n return;\n\n } else if (metadata[fieldIndex].type !== undefined) {\n // trying to define same property multiple times across inheritance.\n // https://github.com/colyseus/colyseus-unity3d/issues/131#issuecomment-814308572\n try {\n throw new Error(`@colyseus/schema: Duplicate '${field}' definition on '${constructor.name}'.\\nCheck @type() annotation`);\n\n } catch (e) {\n const definitionAtLine = e.stack.split(\"\\n\")[4].trim();\n throw new Error(`${e.message} ${definitionAtLine}`);\n }\n }\n\n } else {\n //\n // detect index for this field, considering inheritance\n //\n fieldIndex = metadata[$numFields] // current structure already has fields defined\n ?? (parentMetadata && parentMetadata[$numFields]) // parent structure has fields defined\n ?? -1; // no fields defined\n fieldIndex++;\n }\n\n if (options && options.manual) {\n Metadata.addField(\n metadata,\n fieldIndex,\n field,\n type,\n {\n // do not declare getter/setter descriptor\n enumerable: true,\n configurable: true,\n writable: true,\n }\n );\n\n } else {\n const complexTypeKlass = typeof(Object.keys(type)[0]) === \"string\" && getType(Object.keys(type)[0]);\n\n const childType = (complexTypeKlass)\n ? Object.values(type)[0]\n : type;\n\n Metadata.addField(\n metadata,\n fieldIndex,\n field,\n type,\n getPropertyDescriptor(`_${field}`, fieldIndex, childType, complexTypeKlass)\n );\n }\n }\n}\n\nexport function getPropertyDescriptor(\n fieldCached: string,\n fieldIndex: number,\n type: DefinitionType,\n complexTypeKlass: TypeDefinition,\n) {\n return {\n get: function (this: Schema) { return this[fieldCached as keyof Schema]; },\n set: function (this: Schema, value: any) {\n const previousValue = this[fieldCached as keyof Schema] ?? undefined;\n\n // skip if value is the same as cached.\n if (value === previousValue) { return; }\n\n if (\n value !== undefined &&\n value !== null\n ) {\n if (complexTypeKlass) {\n // automaticallty transform Array into ArraySchema\n if (complexTypeKlass.constructor === ArraySchema && !(value instanceof ArraySchema)) {\n value = new ArraySchema(...value);\n }\n\n // automaticallty transform Map into MapSchema\n if (complexTypeKlass.constructor === MapSchema && !(value instanceof MapSchema)) {\n value = new MapSchema(value);\n }\n\n // // automaticallty transform Array into SetSchema\n // if (complexTypeKlass.constructor === SetSchema && !(value instanceof SetSchema)) {\n // value = new SetSchema(value);\n // }\n\n value[$childType] = type;\n\n } else if (typeof (type) !== \"string\") {\n assertInstanceType(value, type as typeof Schema, this, fieldCached.substring(1));\n\n } else {\n assertType(value, type, this, fieldCached.substring(1));\n }\n\n const changeTree = this[$changes];\n\n //\n // Replacing existing \"ref\", remove it from root.\n //\n if (previousValue !== undefined && previousValue[$changes]) {\n changeTree.root?.remove(previousValue[$changes]);\n (this.constructor as typeof Schema)[$track](changeTree, fieldIndex, OPERATION.DELETE_AND_ADD);\n\n } else {\n (this.constructor as typeof Schema)[$track](changeTree, fieldIndex, OPERATION.ADD);\n }\n\n //\n // call setParent() recursively for this and its child\n // structures.\n //\n value[$changes]?.setParent(this, changeTree.root, fieldIndex);\n\n } else if (previousValue !== undefined) {\n //\n // Setting a field to `null` or `undefined` will delete it.\n //\n this[$changes].delete(fieldIndex);\n }\n\n this[fieldCached as keyof Schema] = value;\n },\n\n enumerable: true,\n configurable: true\n };\n}\n\n/**\n * `@deprecated()` flag a field as deprecated.\n * The previous `@type()` annotation should remain along with this one.\n */\n\nexport function deprecated(throws: boolean = true): PropertyDecorator {\n return function (klass: typeof Schema, field: string) {\n //\n // FIXME: the following block of code is repeated across `@type()`, `@deprecated()` and `@unreliable()` decorators.\n //\n const constructor = klass.constructor as typeof Schema;\n\n const parentClass = Object.getPrototypeOf(constructor);\n const parentMetadata = parentClass[Symbol.metadata];\n const metadata: Metadata = (constructor[Symbol.metadata] ??= Object.assign({}, constructor[Symbol.metadata], parentMetadata ?? Object.create(null)));\n const fieldIndex = metadata[field];\n\n // if (!metadata[field]) {\n // //\n // // detect index for this field, considering inheritance\n // //\n // metadata[field] = {\n // type: undefined,\n // index: (metadata[$numFields] // current structure already has fields defined\n // ?? (parentMetadata && parentMetadata[$numFields]) // parent structure has fields defined\n // ?? -1) + 1 // no fields defined\n // }\n // }\n\n metadata[fieldIndex].deprecated = true;\n\n if (throws) {\n metadata[$descriptors] ??= {};\n metadata[$descriptors][field] = {\n get: function () { throw new Error(`${field} is deprecated.`); },\n set: function (this: Schema, value: any) { /* throw new Error(`${field} is deprecated.`); */ },\n enumerable: false,\n configurable: true\n };\n }\n\n // flag metadata[field] as non-enumerable\n Object.defineProperty(metadata, fieldIndex, {\n value: metadata[fieldIndex],\n enumerable: false,\n configurable: true\n });\n }\n}\n\nexport function defineTypes(\n target: typeof Schema,\n fields: Definition,\n options?: TypeOptions\n) {\n for (let field in fields) {\n type(fields[field], options)(target.prototype, field);\n }\n return target;\n}\n\n// Helper type to extract InitProps from initialize method\n// Supports both single object parameter and multiple parameters\n// If no initialize method is specified, use AssignableProps for field initialization\ntype ExtractInitProps<T> = T extends { initialize: (...args: infer P) => void }\n ? P extends readonly []\n ? never\n : P extends readonly [infer First]\n ? First extends object\n ? First\n : P\n : P\n : T extends Definition\n ? AssignableProps<InferSchemaInstanceType<T>>\n : never;\n\n// Helper type to determine if InitProps should be required\ntype IsInitPropsRequired<T> = T extends { initialize: (props: any) => void }\n ? true\n : T extends { initialize: (...args: infer P) => void }\n ? P extends readonly []\n ? false\n : true\n : false;\n\nexport interface SchemaWithExtends<T extends Definition, P extends typeof Schema, > {\n extends: <T2 extends Definition = Definition>(\n fields: T2 & ThisType<InferSchemaInstanceType<T & T2>>,\n name?: string\n ) => SchemaWithExtendsConstructor<T & T2, ExtractInitProps<T2>, P>;\n}\n\n/**\n * Get the type of the schema defined via `schema({...})` method.\n *\n * @example\n * const Entity = schema({\n * x: \"number\",\n * y: \"number\",\n * });\n * type Entity = SchemaType<typeof Entity>;\n */\nexport type SchemaType<T extends {'~type': any}> = T['~type'];\n\nexport interface SchemaWithExtendsConstructor<\n T extends Definition,\n InitProps,\n P extends typeof Schema\n> extends SchemaWithExtends<T, P> {\n '~type': InferSchemaInstanceType<T>;\n new (...args: [InitProps] extends [never] ? [] : InitProps extends readonly any[] ? InitProps : IsInitPropsRequired<T> extends true ? [InitProps] : [InitProps?]): InferSchemaInstanceType<T> & InstanceType<P>;\n prototype: InferSchemaInstanceType<T> & InstanceType<P> & {\n initialize(...args: [InitProps] extends [never] ? [] : InitProps extends readonly any[] ? InitProps : [InitProps]): void;\n };\n}\n\nexport function schema<\n T extends Record<string, DefinitionType>,\n P extends typeof Schema = typeof Schema\n>(\n fieldsAndMethods: T & ThisType<InferSchemaInstanceType<T>>,\n name?: string,\n inherits: P = Schema as P\n): SchemaWithExtendsConstructor<T, ExtractInitProps<T>, P> {\n const fields: any = {};\n const methods: any = {};\n\n const defaultValues: any = {};\n const viewTagFields: any = {};\n\n for (let fieldName in fieldsAndMethods) {\n const value: any = fieldsAndMethods[fieldName] as DefinitionType;\n if (typeof (value) === \"object\") {\n if (value['view'] !== undefined) {\n viewTagFields[fieldName] = (typeof (value['view']) === \"boolean\")\n ? DEFAULT_VIEW_TAG\n : value['view'];\n }\n\n fields[fieldName] = getNormalizedType(value);\n\n // If no explicit default provided, handle automatic instantiation for collection types\n if (!Object.prototype.hasOwnProperty.call(value, 'default')) {\n // TODO: remove Array.isArray() check. Use ['array'] !== undefined only.\n if (Array.isArray(value) || value['array'] !== undefined) {\n // Collection: Array → new ArraySchema()\n defaultValues[fieldName] = new ArraySchema();\n\n } else if (value['map'] !== undefined) {\n // Collection: Map → new MapSchema()\n defaultValues[fieldName] = new MapSchema();\n\n } else if (value['collection'] !== undefined) {\n // Collection: Collection → new CollectionSchema()\n defaultValues[fieldName] = new CollectionSchema();\n\n } else if (value['set'] !== undefined) {\n // Collection: Set → new SetSchema()\n defaultValues[fieldName] = new SetSchema();\n\n } else if (value['type'] !== undefined && Schema.is(value['type'])) {\n // Direct Schema type: Type → new Type()\n if (!value['type'].prototype.initialize || value['type'].prototype.initialize.length === 0) {\n // only auto-initialize Schema instances if:\n // - they don't have an initialize method\n // - or initialize method doesn't accept any parameters\n defaultValues[fieldName] = new value['type']();\n }\n }\n } else {\n defaultValues[fieldName] = value['default'];\n }\n\n\n } else if (typeof (value) === \"function\") {\n if (Schema.is(value)) {\n // Direct Schema type: Type → new Type()\n if (!value.prototype.initialize || value.prototype.initialize.length === 0) {\n // only auto-initialize Schema instances if:\n // - they don't have an initialize method\n // - or initialize method doesn't accept any parameters\n defaultValues[fieldName] = new value();\n }\n fields[fieldName] = getNormalizedType(value);\n } else {\n methods[fieldName] = value;\n }\n\n } else {\n fields[fieldName] = getNormalizedType(value);\n }\n }\n\n const getDefaultValues = () => {\n const defaults: any = {};\n\n // use current class default values\n for (const fieldName in defaultValues) {\n const defaultValue = defaultValues[fieldName];\n if (defaultValue && typeof defaultValue.clone === 'function') {\n // complex, cloneable values, e.g. Schema, ArraySchema, MapSchema, CollectionSchema, SetSchema\n defaults[fieldName] = defaultValue.clone();\n } else {\n // primitives and non-cloneable values\n defaults[fieldName] = defaultValue;\n }\n }\n return defaults;\n };\n\n const getParentProps = (props: any) => {\n const fieldNames = Object.keys(fields);\n const parentProps: any = {};\n for (const key in props) {\n if (!fieldNames.includes(key)) {\n parentProps[key] = props[key];\n }\n }\n return parentProps;\n }\n\n /** @codegen-ignore */\n const klass = Metadata.setFields<any>(class extends (inherits as any) {\n constructor(...args: any[]) {\n // call initialize method\n if (methods.initialize && typeof methods.initialize === 'function') {\n super(Object.assign({}, getDefaultValues(), getParentProps(args[0] || {})));\n methods.initialize.apply(this, args);\n\n } else {\n super(Object.assign({}, getDefaultValues(), args[0] || {}));\n }\n }\n }, fields) as SchemaWithExtendsConstructor<T, ExtractInitProps<T>, P>;\n\n // Store the getDefaultValues function on the class for inheritance\n (klass as any)._getDefaultValues = getDefaultValues;\n\n // Add methods to the prototype\n Object.assign(klass.prototype, methods);\n\n for (let fieldName in viewTagFields) {\n view(viewTagFields[fieldName])(klass.prototype, fieldName);\n }\n\n if (name) {\n Object.defineProperty(klass, \"name\", { value: name });\n }\n\n klass.extends = <T2 extends Definition = Definition>(fields: T2, name?: string) =>\n schema<T2>(fields, name, klass as any) as SchemaWithExtendsConstructor<T & T2, ExtractInitProps<T2>, P>;\n\n return klass;\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@colyseus/schema",
3
- "version": "3.0.71",
3
+ "version": "3.0.73",
4
4
  "description": "Binary state serializer with delta encoding for games",
5
5
  "bin": {
6
6
  "schema-codegen": "bin/schema-codegen",
@@ -597,8 +597,12 @@ export function schema<
597
597
 
598
598
  } else if (value['type'] !== undefined && Schema.is(value['type'])) {
599
599
  // Direct Schema type: Type → new Type()
600
- // TODO: should we auto-initialize Schema instances, in case their initialize() method is not defined?
601
- // defaultValues[fieldName] = new value['type']();
600
+ if (!value['type'].prototype.initialize || value['type'].prototype.initialize.length === 0) {
601
+ // only auto-initialize Schema instances if:
602
+ // - they don't have an initialize method
603
+ // - or initialize method doesn't accept any parameters
604
+ defaultValues[fieldName] = new value['type']();
605
+ }
602
606
  }
603
607
  } else {
604
608
  defaultValues[fieldName] = value['default'];
@@ -608,8 +612,12 @@ export function schema<
608
612
  } else if (typeof (value) === "function") {
609
613
  if (Schema.is(value)) {
610
614
  // Direct Schema type: Type → new Type()
611
- // TODO: should we auto-initialize Schema instances, in case their initialize() method is not defined?
612
- // defaultValues[fieldName] = new value();
615
+ if (!value.prototype.initialize || value.prototype.initialize.length === 0) {
616
+ // only auto-initialize Schema instances if:
617
+ // - they don't have an initialize method
618
+ // - or initialize method doesn't accept any parameters
619
+ defaultValues[fieldName] = new value();
620
+ }
613
621
  fields[fieldName] = getNormalizedType(value);
614
622
  } else {
615
623
  methods[fieldName] = value;
@@ -637,14 +645,27 @@ export function schema<
637
645
  return defaults;
638
646
  };
639
647
 
648
+ const getParentProps = (props: any) => {
649
+ const fieldNames = Object.keys(fields);
650
+ const parentProps: any = {};
651
+ for (const key in props) {
652
+ if (!fieldNames.includes(key)) {
653
+ parentProps[key] = props[key];
654
+ }
655
+ }
656
+ return parentProps;
657
+ }
658
+
640
659
  /** @codegen-ignore */
641
660
  const klass = Metadata.setFields<any>(class extends (inherits as any) {
642
661
  constructor(...args: any[]) {
643
- super(Object.assign({}, getDefaultValues(), args[0] || {}));
644
-
645
662
  // call initialize method
646
663
  if (methods.initialize && typeof methods.initialize === 'function') {
664
+ super(Object.assign({}, getDefaultValues(), getParentProps(args[0] || {})));
647
665
  methods.initialize.apply(this, args);
666
+
667
+ } else {
668
+ super(Object.assign({}, getDefaultValues(), args[0] || {}));
648
669
  }
649
670
  }
650
671
  }, fields) as SchemaWithExtendsConstructor<T, ExtractInitProps<T>, P>;