@colyseus/schema 5.0.8 → 5.0.9

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@colyseus/schema",
3
- "version": "5.0.8",
3
+ "version": "5.0.9",
4
4
  "description": "Binary state serializer with delta encoding for games",
5
5
  "type": "module",
6
6
  "bin": {
@@ -575,6 +575,30 @@ export function deprecated(throws: boolean = true): PropertyDecorator {
575
575
  }
576
576
  }
577
577
 
578
+ let defineTypesWarned = false;
579
+
580
+ /**
581
+ * Adds synchronizable fields to an existing `Schema` subclass — the pre-5.0
582
+ * helper for plain JavaScript users.
583
+ *
584
+ * @deprecated Use `schema()` with `t.*` field builders instead:
585
+ * https://docs.colyseus.io/state/schema
586
+ */
587
+ export function defineTypes(
588
+ target: typeof Schema,
589
+ fields: Definition,
590
+ options?: TypeOptions
591
+ ) {
592
+ if (!defineTypesWarned) {
593
+ defineTypesWarned = true;
594
+ console.warn("@colyseus/schema: defineTypes() is deprecated and will be removed in a future release. Use schema() with t.* field builders instead → https://docs.colyseus.io/state/schema");
595
+ }
596
+ for (let field in fields) {
597
+ type(fields[field], options)(target.prototype, field);
598
+ }
599
+ return target;
600
+ }
601
+
578
602
  // Helper type to extract InitProps from initialize method.
579
603
  // - Non-empty initialize params: use them directly.
580
604
  // - Zero-arg initialize: no args accepted (`never`) — user-supplied field
@@ -8,6 +8,8 @@ let currentProperty: Property;
8
8
 
9
9
  let globalContext: Context;
10
10
 
11
+ let defineTypesWarned = false;
12
+
11
13
  const BUILDER_COLLECTION_KINDS = new Set(["array", "map", "set", "collection"]);
12
14
 
13
15
  /**
@@ -254,6 +256,44 @@ function inspectNode(node: ts.Node, context: Context, decoratorName: string) {
254
256
  defineProperty(property, prop.initializer);
255
257
  }
256
258
 
259
+ } else if (
260
+ node.getText() === "defineTypes" &&
261
+ (
262
+ node.parent.kind === ts.SyntaxKind.CallExpression ||
263
+ node.parent.kind === ts.SyntaxKind.PropertyAccessExpression
264
+ )
265
+ ) {
266
+ /**
267
+ * JavaScript source file (`.js`)
268
+ * Using `defineTypes()` (deprecated)
269
+ */
270
+ const callExpression = (node.parent.kind === ts.SyntaxKind.PropertyAccessExpression)
271
+ ? node.parent.parent as ts.CallExpression
272
+ : node.parent as ts.CallExpression;
273
+
274
+ if (callExpression.kind !== ts.SyntaxKind.CallExpression) {
275
+ break;
276
+ }
277
+
278
+ if (!defineTypesWarned) {
279
+ defineTypesWarned = true;
280
+ console.warn("schema-codegen: defineTypes() is deprecated and will be removed in a future release. Use schema() with t.* field builders instead → https://docs.colyseus.io/state/schema");
281
+ }
282
+
283
+ const className = callExpression.arguments[0].getText()
284
+ currentStructure.name = className;
285
+
286
+ const types = callExpression.arguments[1] as any;
287
+ for (let i = 0; i < types.properties.length; i++) {
288
+ const prop = types.properties[i];
289
+
290
+ const property = currentProperty || new Property();
291
+ property.name = prop.name.escapedText;
292
+ currentStructure.addProperty(property);
293
+
294
+ defineProperty(property, prop.initializer);
295
+ }
296
+
257
297
  }
258
298
 
259
299
  if (node.parent.kind === ts.SyntaxKind.ClassDeclaration) {
package/src/index.ts CHANGED
@@ -49,6 +49,7 @@ export { Metadata } from "./Metadata.js";
49
49
  export {
50
50
  type,
51
51
  deprecated,
52
+ defineTypes,
52
53
  owned,
53
54
  unreliable,
54
55
  transient,