@colyseus/schema 3.0.0-alpha.19 → 3.0.0-alpha.22

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.
Files changed (75) hide show
  1. package/build/cjs/index.js +94 -63
  2. package/build/cjs/index.js.map +1 -1
  3. package/build/esm/index.mjs +94 -61
  4. package/build/esm/index.mjs.map +1 -1
  5. package/build/umd/index.js +94 -63
  6. package/lib/Metadata.d.ts +1 -0
  7. package/lib/Metadata.js +32 -1
  8. package/lib/Metadata.js.map +1 -1
  9. package/lib/Reflection.d.ts +0 -1
  10. package/lib/Reflection.js +27 -20
  11. package/lib/Reflection.js.map +1 -1
  12. package/lib/Schema.d.ts +1 -1
  13. package/lib/annotations.js +12 -10
  14. package/lib/annotations.js.map +1 -1
  15. package/lib/codegen/api.js +1 -2
  16. package/lib/codegen/api.js.map +1 -1
  17. package/lib/codegen/languages/cpp.js +1 -2
  18. package/lib/codegen/languages/cpp.js.map +1 -1
  19. package/lib/codegen/languages/csharp.js +1 -2
  20. package/lib/codegen/languages/csharp.js.map +1 -1
  21. package/lib/codegen/languages/haxe.js +1 -2
  22. package/lib/codegen/languages/haxe.js.map +1 -1
  23. package/lib/codegen/languages/java.js +1 -2
  24. package/lib/codegen/languages/java.js.map +1 -1
  25. package/lib/codegen/languages/js.js +1 -2
  26. package/lib/codegen/languages/js.js.map +1 -1
  27. package/lib/codegen/languages/lua.js +1 -2
  28. package/lib/codegen/languages/lua.js.map +1 -1
  29. package/lib/codegen/languages/ts.js +1 -2
  30. package/lib/codegen/languages/ts.js.map +1 -1
  31. package/lib/codegen/parser.js +2 -3
  32. package/lib/codegen/parser.js.map +1 -1
  33. package/lib/codegen/types.js +3 -3
  34. package/lib/codegen/types.js.map +1 -1
  35. package/lib/decoder/DecodeOperation.d.ts +0 -1
  36. package/lib/decoder/DecodeOperation.js +2 -2
  37. package/lib/decoder/DecodeOperation.js.map +1 -1
  38. package/lib/decoder/Decoder.d.ts +0 -1
  39. package/lib/decoder/strategy/RawChanges.js +1 -2
  40. package/lib/decoder/strategy/RawChanges.js.map +1 -1
  41. package/lib/decoder/strategy/StateCallbacks.js +1 -2
  42. package/lib/decoder/strategy/StateCallbacks.js.map +1 -1
  43. package/lib/encoder/EncodeOperation.d.ts +0 -1
  44. package/lib/encoder/EncodeOperation.js +3 -3
  45. package/lib/encoder/EncodeOperation.js.map +1 -1
  46. package/lib/encoder/Encoder.d.ts +0 -1
  47. package/lib/encoder/Encoder.js +5 -5
  48. package/lib/encoder/Encoder.js.map +1 -1
  49. package/lib/encoder/StateView.js +2 -2
  50. package/lib/encoder/StateView.js.map +1 -1
  51. package/lib/encoding/assert.js +3 -3
  52. package/lib/encoding/assert.js.map +1 -1
  53. package/lib/encoding/decode.js +20 -21
  54. package/lib/encoding/decode.js.map +1 -1
  55. package/lib/encoding/encode.d.ts +0 -1
  56. package/lib/encoding/encode.js +17 -17
  57. package/lib/encoding/encode.js.map +1 -1
  58. package/lib/index.d.ts +1 -0
  59. package/lib/index.js.map +1 -1
  60. package/lib/types/custom/ArraySchema.d.ts +2 -2
  61. package/lib/types/custom/ArraySchema.js +0 -8
  62. package/lib/types/custom/ArraySchema.js.map +1 -1
  63. package/lib/types/registry.js +3 -4
  64. package/lib/types/registry.js.map +1 -1
  65. package/lib/types/utils.js +1 -2
  66. package/lib/types/utils.js.map +1 -1
  67. package/lib/utils.js +3 -4
  68. package/lib/utils.js.map +1 -1
  69. package/package.json +5 -5
  70. package/src/Metadata.ts +39 -1
  71. package/src/Reflection.ts +30 -20
  72. package/src/annotations.ts +6 -2
  73. package/src/encoder/Encoder.ts +5 -5
  74. package/src/index.ts +2 -0
  75. package/src/types/custom/ArraySchema.ts +2 -1
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@colyseus/schema",
3
- "version": "3.0.0-alpha.19",
3
+ "version": "3.0.0-alpha.22",
4
4
  "description": "Binary state serializer with delta encoding for games",
5
5
  "bin": {
6
6
  "schema-codegen": "./bin/schema-codegen"
7
7
  },
8
8
  "scripts": {
9
- "build": "tsc && rollup -c rollup.config.js",
9
+ "build": "tsc && rollup -c rollup.config.mjs",
10
10
  "watch": "tsc -w",
11
11
  "test": "mocha --require ts-node/register test/*.test.ts test/**/*.test.ts",
12
12
  "coverage": "nyc mocha --require ts-node/register --require source-map-support/register --recursive test/**Test.ts",
@@ -73,13 +73,13 @@
73
73
  "nanoid": "^3.1.10",
74
74
  "nyc": "^14.1.1",
75
75
  "rimraf": "^3.0.0",
76
- "rollup": "^2.40.0",
76
+ "rollup": "^4.18.0",
77
77
  "sinon": "^7.2.2",
78
78
  "source-map-support": "^0.5.13",
79
79
  "ts-node": "^10.9.2",
80
80
  "tslib": "^2.1.0",
81
- "tsx": "^4.15.7",
82
- "typescript": "^5.3.3"
81
+ "tsx": "^3.13.0",
82
+ "typescript": "^5.4.5"
83
83
  },
84
84
  "nyc": {
85
85
  "extension": [
package/src/Metadata.ts CHANGED
@@ -120,10 +120,48 @@ export const Metadata = {
120
120
  init(klass: any) {
121
121
  //
122
122
  // Used only to initialize an empty Schema (Encoder#constructor)
123
+ // TODO: remove/refactor this...
123
124
  //
124
125
  const metadata = {};
125
126
  klass.constructor[Symbol.metadata] = metadata;
126
- Object.defineProperty(metadata, -1, { value: 0, enumerable: false, configurable: true });
127
+ Object.defineProperty(metadata, -1, {
128
+ value: 0,
129
+ enumerable: false,
130
+ configurable: true,
131
+ });
132
+ },
133
+
134
+ initialize(constructor: any, parentMetadata?: any) {
135
+ let metadata: Metadata = constructor[Symbol.metadata] ?? Object.create(null);
136
+
137
+ // make sure inherited classes have their own metadata object.
138
+ if (constructor[Symbol.metadata] === parentMetadata) {
139
+ metadata = Object.create(null);
140
+
141
+ if (parentMetadata) {
142
+ // assign parent metadata to current
143
+ Object.assign(metadata, parentMetadata);
144
+
145
+ for (let i = 0; i <= parentMetadata[-1]; i++) {
146
+ Object.defineProperty(metadata, i, {
147
+ value: parentMetadata[i],
148
+ enumerable: false,
149
+ configurable: true,
150
+ });
151
+ }
152
+
153
+ Object.defineProperty(metadata, -1, {
154
+ value: parentMetadata[-1],
155
+ enumerable: false,
156
+ configurable: true,
157
+ writable: true,
158
+ });
159
+ }
160
+ }
161
+
162
+ constructor[Symbol.metadata] = metadata;
163
+
164
+ return metadata;
127
165
  },
128
166
 
129
167
  isValidInstance(klass: any) {
package/src/Reflection.ts CHANGED
@@ -106,62 +106,72 @@ export class Reflection extends Schema {
106
106
  const reflectionDecoder = new Decoder(reflection);
107
107
  reflectionDecoder.decode(bytes, it);
108
108
 
109
- const context = new TypeContext();
109
+ const typeContext = new TypeContext();
110
110
 
111
- const schemaTypes = reflection.types.reduce((types, reflectionType) => {
112
- const parentKlass: typeof Schema = types[reflectionType.extendsId] || Schema;
113
- const schema: typeof Schema = class _ extends parentKlass {};
111
+ // 1st pass, initialize metadata + inheritance
112
+ reflection.types.forEach((reflectionType) => {
113
+ const parentClass: typeof Schema = typeContext.get(reflectionType.extendsId) ?? Schema;
114
+ const schema: typeof Schema = class _ extends parentClass {};
114
115
 
115
- // const _metadata = Object.create(_classSuper[Symbol.metadata] ?? null);
116
- const _metadata = parentKlass && parentKlass[Symbol.metadata] || Object.create(null);
117
- Object.defineProperty(schema, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata })
116
+ const parentMetadata = parentClass[Symbol.metadata];
118
117
 
119
118
  // register for inheritance support
120
119
  TypeContext.register(schema);
121
120
 
122
- const typeid = reflectionType.id;
123
- types[typeid] = schema
124
- context.add(schema, typeid);
125
- return types;
121
+ // for inheritance support
122
+ Metadata.initialize(schema, parentMetadata);
123
+
124
+ typeContext.add(schema, reflectionType.id);
126
125
  }, {});
127
126
 
127
+ // 2nd pass, set fields
128
128
  reflection.types.forEach((reflectionType) => {
129
- const schemaType = schemaTypes[reflectionType.id];
129
+ const schemaType = typeContext.get(reflectionType.id);
130
130
  const metadata = schemaType[Symbol.metadata];
131
131
 
132
- const parentKlass = reflection.types[reflectionType.extendsId];
133
- const parentFieldIndex = parentKlass && parentKlass.fields.length || 0;
132
+ // FIXME: use metadata[-1] to get field count
133
+ const parentFieldIndex = 0;
134
+
135
+ // console.log("--------------------");
136
+ // // console.log("reflectionType", reflectionType.toJSON());
137
+ // console.log("reflectionType.fields", reflectionType.fields.toJSON());
138
+ // console.log("parentFieldIndex", parentFieldIndex);
139
+
140
+ //
141
+ // FIXME: set fields using parentKlass as well
142
+ // currently the fields are duplicated on inherited classes
143
+ //
144
+ // // const parentKlass = reflection.types[reflectionType.extendsId];
145
+ // // parentKlass.fields
134
146
 
135
147
  reflectionType.fields.forEach((field, i) => {
136
148
  const fieldIndex = parentFieldIndex + i;
137
149
 
138
150
  if (field.referencedType !== undefined) {
139
151
  let fieldType = field.type;
140
- let refType = schemaTypes[field.referencedType];
152
+ let refType: PrimitiveType = typeContext.get(field.referencedType);
141
153
 
142
154
  // map or array of primitive type (-1)
143
155
  if (!refType) {
144
156
  const typeInfo = field.type.split(":");
145
157
  fieldType = typeInfo[0];
146
- refType = typeInfo[1];
158
+ refType = typeInfo[1] as PrimitiveType; // string
147
159
  }
148
160
 
149
161
  if (fieldType === "ref") {
150
- // type(refType)(schemaType.prototype, field.name);
151
162
  Metadata.addField(metadata, fieldIndex, field.name, refType);
152
163
 
153
164
  } else {
154
- // type({ [fieldType]: refType } as DefinitionType)(schemaType.prototype, field.name);
155
165
  Metadata.addField(metadata, fieldIndex, field.name, { [fieldType]: refType } as DefinitionType);
156
166
  }
157
167
 
158
168
  } else {
159
- // type(field.type as PrimitiveType)(schemaType.prototype, field.name);
160
169
  Metadata.addField(metadata, fieldIndex, field.name, field.type as PrimitiveType);
161
170
  }
162
171
  });
163
172
  });
164
173
 
165
- return new (schemaTypes[0])();
174
+ // @ts-ignore
175
+ return new (typeContext.get(0))();
166
176
  }
167
177
  }
@@ -330,6 +330,8 @@ export function view<T> (tag: number = DEFAULT_VIEW_TAG) {
330
330
 
331
331
  const parentClass = Object.getPrototypeOf(constructor);
332
332
  const parentMetadata = parentClass[Symbol.metadata];
333
+
334
+ // TODO: use Metadata.initialize()
333
335
  const metadata: Metadata = (constructor[Symbol.metadata] ??= Object.assign({}, constructor[Symbol.metadata], parentMetadata ?? Object.create(null)));
334
336
 
335
337
  if (!metadata[fieldName]) {
@@ -356,6 +358,8 @@ export function unreliable<T> (target: T, field: string) {
356
358
 
357
359
  const parentClass = Object.getPrototypeOf(constructor);
358
360
  const parentMetadata = parentClass[Symbol.metadata];
361
+
362
+ // TODO: use Metadata.initialize()
359
363
  const metadata: Metadata = (constructor[Symbol.metadata] ??= Object.assign({}, constructor[Symbol.metadata], parentMetadata ?? Object.create(null)));
360
364
 
361
365
  if (!metadata[field]) {
@@ -389,8 +393,8 @@ export function type (
389
393
  TypeContext.register(constructor);
390
394
 
391
395
  const parentClass = Object.getPrototypeOf(constructor);
392
- const parentMetadata = parentClass[Symbol.metadata];
393
- const metadata: Metadata = (constructor[Symbol.metadata] ??= Object.assign({}, constructor[Symbol.metadata], parentMetadata ?? Object.create(null)));
396
+ const parentMetadata = parentClass && parentClass[Symbol.metadata];
397
+ const metadata = Metadata.initialize(constructor, parentMetadata);
394
398
 
395
399
  let fieldIndex: number;
396
400
 
@@ -112,11 +112,11 @@ export class Encoder<T extends Schema = any> {
112
112
  // try { throw new Error(); } catch (e) {
113
113
  // // only print if not coming from Reflection.ts
114
114
  // if (!e.stack.includes("src/Reflection.ts")) {
115
- // // console.log("WILL ENCODE", {
116
- // // ref: changeTree.ref.constructor.name,
117
- // // fieldIndex,
118
- // // operation: OPERATION[operation],
119
- // // });
115
+ // console.log("WILL ENCODE", {
116
+ // ref: changeTree.ref.constructor.name,
117
+ // fieldIndex,
118
+ // operation: OPERATION[operation],
119
+ // });
120
120
  // }
121
121
  // }
122
122
 
package/src/index.ts CHANGED
@@ -4,6 +4,8 @@ export type { DataChange } from "./decoder/DecodeOperation";
4
4
  import { $track, $encoder, $decoder, $filter, $getByIndex, $deleteByIndex, $changes, $childType } from "./types/symbols";
5
5
  export { $track, $encoder, $decoder, $filter, $getByIndex, $deleteByIndex, $changes, $childType };
6
6
 
7
+ export type { ToJSON } from "./types/HelperTypes";
8
+
7
9
  import { MapSchema } from "./types/custom/MapSchema"
8
10
  export { MapSchema };
9
11
 
@@ -471,6 +471,7 @@ export class ArraySchema<V = any> implements Array<V>, Collection<number, V> {
471
471
  * @param thisArg An object to which the this keyword can refer in the callbackfn function.
472
472
  * If thisArg is omitted, undefined is used as the this value.
473
473
  */
474
+ every<S extends V>(predicate: (value: V, index: number, array: V[]) => value is S, thisArg?: any): this is S[];
474
475
  every(callbackfn: (value: V, index: number, array: V[]) => unknown, thisArg?: any): boolean {
475
476
  return this.items.every(callbackfn, thisArg);
476
477
  }
@@ -690,7 +691,7 @@ export class ArraySchema<V = any> implements Array<V>, Collection<number, V> {
690
691
  //
691
692
  // ES2023
692
693
  //
693
- with(index: number, value: V): V[] {
694
+ with(index: number, value: V): ArraySchema<V> {
694
695
  const copy = this.items.slice();
695
696
  copy[index] = value;
696
697
  return new ArraySchema(...copy);