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