@colyseus/schema 3.0.0-alpha.36 → 3.0.0-alpha.38
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/build/cjs/index.js +83 -22
- package/build/cjs/index.js.map +1 -1
- package/build/esm/index.mjs +83 -22
- package/build/esm/index.mjs.map +1 -1
- package/build/umd/index.js +83 -22
- package/lib/Metadata.d.ts +1 -1
- package/lib/Metadata.js +53 -11
- package/lib/Metadata.js.map +1 -1
- package/lib/Reflection.js +23 -8
- package/lib/Reflection.js.map +1 -1
- package/lib/annotations.js +3 -3
- package/lib/annotations.js.map +1 -1
- package/lib/encoder/Encoder.d.ts +1 -0
- package/lib/encoder/Encoder.js +4 -0
- package/lib/encoder/Encoder.js.map +1 -1
- package/package.json +1 -1
- package/src/Metadata.ts +58 -11
- package/src/Reflection.ts +30 -11
- package/src/annotations.ts +3 -3
- package/src/encoder/Encoder.ts +7 -0
package/build/esm/index.mjs
CHANGED
|
@@ -181,7 +181,11 @@ const Metadata = {
|
|
|
181
181
|
name,
|
|
182
182
|
});
|
|
183
183
|
// create "descriptors" map
|
|
184
|
-
metadata
|
|
184
|
+
Object.defineProperty(metadata, $descriptors, {
|
|
185
|
+
value: metadata[$descriptors] || {},
|
|
186
|
+
enumerable: false,
|
|
187
|
+
configurable: true,
|
|
188
|
+
});
|
|
185
189
|
if (descriptor) {
|
|
186
190
|
// for encoder
|
|
187
191
|
metadata[$descriptors][name] = descriptor;
|
|
@@ -256,7 +260,7 @@ const Metadata = {
|
|
|
256
260
|
TypeContext.register(constructor);
|
|
257
261
|
const parentClass = Object.getPrototypeOf(constructor);
|
|
258
262
|
const parentMetadata = parentClass && parentClass[Symbol.metadata];
|
|
259
|
-
const metadata = Metadata.initialize(constructor
|
|
263
|
+
const metadata = Metadata.initialize(constructor);
|
|
260
264
|
// Use Schema's methods if not defined in the class
|
|
261
265
|
if (!constructor[$track]) {
|
|
262
266
|
constructor[$track] = Schema[$track];
|
|
@@ -304,24 +308,62 @@ const Metadata = {
|
|
|
304
308
|
configurable: true,
|
|
305
309
|
});
|
|
306
310
|
},
|
|
307
|
-
initialize(constructor
|
|
311
|
+
initialize(constructor) {
|
|
312
|
+
const parentClass = Object.getPrototypeOf(constructor);
|
|
313
|
+
const parentMetadata = parentClass[Symbol.metadata];
|
|
308
314
|
let metadata = constructor[Symbol.metadata] ?? Object.create(null);
|
|
309
315
|
// make sure inherited classes have their own metadata object.
|
|
310
|
-
if (
|
|
316
|
+
if (parentClass !== Schema && metadata === parentMetadata) {
|
|
311
317
|
metadata = Object.create(null);
|
|
312
318
|
if (parentMetadata) {
|
|
319
|
+
//
|
|
313
320
|
// assign parent metadata to current
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
321
|
+
//
|
|
322
|
+
Object.setPrototypeOf(metadata, parentMetadata);
|
|
323
|
+
// Object.keys(parentMetadata).forEach((fieldIndex) => {
|
|
324
|
+
// const index = Number(fieldIndex);
|
|
325
|
+
// const field = parentMetadata[index];
|
|
326
|
+
// metadata[index] = field;
|
|
327
|
+
// // Object.defineProperty(metadata, field.name, {
|
|
328
|
+
// // value: index,
|
|
329
|
+
// // enumerable: false,
|
|
330
|
+
// // configurable: true,
|
|
331
|
+
// // });
|
|
332
|
+
// });
|
|
333
|
+
// $numFields
|
|
334
|
+
Object.defineProperty(metadata, $numFields, {
|
|
335
|
+
value: parentMetadata[$numFields],
|
|
336
|
+
enumerable: false,
|
|
337
|
+
configurable: true,
|
|
338
|
+
writable: true,
|
|
339
|
+
});
|
|
340
|
+
// $viewFieldIndexes / $fieldIndexesByViewTag
|
|
341
|
+
if (parentMetadata[$viewFieldIndexes] !== undefined) {
|
|
342
|
+
Object.defineProperty(metadata, $viewFieldIndexes, {
|
|
343
|
+
value: [...parentMetadata[$viewFieldIndexes]],
|
|
319
344
|
enumerable: false,
|
|
320
345
|
configurable: true,
|
|
346
|
+
writable: true,
|
|
347
|
+
});
|
|
348
|
+
Object.defineProperty(metadata, $fieldIndexesByViewTag, {
|
|
349
|
+
value: { ...parentMetadata[$fieldIndexesByViewTag] },
|
|
350
|
+
enumerable: false,
|
|
351
|
+
configurable: true,
|
|
352
|
+
writable: true,
|
|
321
353
|
});
|
|
322
354
|
}
|
|
323
|
-
|
|
324
|
-
|
|
355
|
+
// $refTypeFieldIndexes
|
|
356
|
+
if (parentMetadata[$refTypeFieldIndexes] !== undefined) {
|
|
357
|
+
Object.defineProperty(metadata, $refTypeFieldIndexes, {
|
|
358
|
+
value: [...parentMetadata[$refTypeFieldIndexes]],
|
|
359
|
+
enumerable: false,
|
|
360
|
+
configurable: true,
|
|
361
|
+
writable: true,
|
|
362
|
+
});
|
|
363
|
+
}
|
|
364
|
+
// $descriptors
|
|
365
|
+
Object.defineProperty(metadata, $descriptors, {
|
|
366
|
+
value: { ...parentMetadata[$descriptors] },
|
|
325
367
|
enumerable: false,
|
|
326
368
|
configurable: true,
|
|
327
369
|
writable: true,
|
|
@@ -2768,13 +2810,13 @@ function type(type, options) {
|
|
|
2768
2810
|
// for inheritance support
|
|
2769
2811
|
TypeContext.register(constructor);
|
|
2770
2812
|
const parentClass = Object.getPrototypeOf(constructor);
|
|
2771
|
-
const parentMetadata = parentClass
|
|
2772
|
-
const metadata = Metadata.initialize(constructor
|
|
2813
|
+
const parentMetadata = parentClass[Symbol.metadata];
|
|
2814
|
+
const metadata = Metadata.initialize(constructor);
|
|
2773
2815
|
let fieldIndex = metadata[field];
|
|
2774
2816
|
/**
|
|
2775
2817
|
* skip if descriptor already exists for this field (`@deprecated()`)
|
|
2776
2818
|
*/
|
|
2777
|
-
if (metadata[fieldIndex]) {
|
|
2819
|
+
if (metadata[fieldIndex] !== undefined) {
|
|
2778
2820
|
if (metadata[fieldIndex].deprecated) {
|
|
2779
2821
|
// do not create accessors for deprecated properties.
|
|
2780
2822
|
return;
|
|
@@ -3900,6 +3942,10 @@ class Encoder {
|
|
|
3900
3942
|
number$1(bytes, targetTypeId, it);
|
|
3901
3943
|
}
|
|
3902
3944
|
}
|
|
3945
|
+
get hasChanges() {
|
|
3946
|
+
return (this.root.changes.length > 0 ||
|
|
3947
|
+
this.root.filteredChanges.length > 0);
|
|
3948
|
+
}
|
|
3903
3949
|
}
|
|
3904
3950
|
|
|
3905
3951
|
class DecodingWarning extends Error {
|
|
@@ -4247,18 +4293,14 @@ class Reflection extends Schema {
|
|
|
4247
4293
|
const parentClass = typeContext.get(reflectionType.extendsId) ?? Schema;
|
|
4248
4294
|
const schema = class _ extends parentClass {
|
|
4249
4295
|
};
|
|
4250
|
-
const parentMetadata = parentClass[Symbol.metadata];
|
|
4251
4296
|
// register for inheritance support
|
|
4252
4297
|
TypeContext.register(schema);
|
|
4253
|
-
// for inheritance support
|
|
4254
|
-
Metadata.initialize(schema
|
|
4298
|
+
// // for inheritance support
|
|
4299
|
+
// Metadata.initialize(schema);
|
|
4255
4300
|
typeContext.add(schema, reflectionType.id);
|
|
4256
4301
|
}, {});
|
|
4257
|
-
//
|
|
4258
|
-
|
|
4259
|
-
const schemaType = typeContext.get(reflectionType.id);
|
|
4260
|
-
const metadata = schemaType[Symbol.metadata];
|
|
4261
|
-
const parentFieldIndex = 0;
|
|
4302
|
+
// define fields
|
|
4303
|
+
const addFields = (metadata, reflectionType, parentFieldIndex) => {
|
|
4262
4304
|
reflectionType.fields.forEach((field, i) => {
|
|
4263
4305
|
const fieldIndex = parentFieldIndex + i;
|
|
4264
4306
|
if (field.referencedType !== undefined) {
|
|
@@ -4281,6 +4323,25 @@ class Reflection extends Schema {
|
|
|
4281
4323
|
Metadata.addField(metadata, fieldIndex, field.name, field.type);
|
|
4282
4324
|
}
|
|
4283
4325
|
});
|
|
4326
|
+
};
|
|
4327
|
+
// 2nd pass, set fields
|
|
4328
|
+
reflection.types.forEach((reflectionType) => {
|
|
4329
|
+
const schema = typeContext.get(reflectionType.id);
|
|
4330
|
+
// for inheritance support
|
|
4331
|
+
const metadata = Metadata.initialize(schema);
|
|
4332
|
+
const inheritedTypes = [];
|
|
4333
|
+
let parentType = reflectionType;
|
|
4334
|
+
do {
|
|
4335
|
+
inheritedTypes.push(parentType);
|
|
4336
|
+
parentType = reflection.types.find((t) => t.id === parentType.extendsId);
|
|
4337
|
+
} while (parentType);
|
|
4338
|
+
let parentFieldIndex = 0;
|
|
4339
|
+
inheritedTypes.reverse().forEach((reflectionType) => {
|
|
4340
|
+
// add fields from all inherited classes
|
|
4341
|
+
// TODO: refactor this to avoid adding fields from parent classes
|
|
4342
|
+
addFields(metadata, reflectionType, parentFieldIndex);
|
|
4343
|
+
parentFieldIndex += reflectionType.fields.length;
|
|
4344
|
+
});
|
|
4284
4345
|
});
|
|
4285
4346
|
const state = new (typeContext.get(0))();
|
|
4286
4347
|
return new Decoder(state, typeContext);
|