@colyseus/schema 3.0.0-alpha.38 → 3.0.0-alpha.39

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 (45) hide show
  1. package/build/cjs/index.js +73 -20
  2. package/build/cjs/index.js.map +1 -1
  3. package/build/esm/index.mjs +73 -21
  4. package/build/esm/index.mjs.map +1 -1
  5. package/build/umd/index.js +73 -20
  6. package/lib/Metadata.d.ts +2 -1
  7. package/lib/Metadata.js +14 -14
  8. package/lib/Metadata.js.map +1 -1
  9. package/lib/Reflection.d.ts +4 -3
  10. package/lib/Reflection.js +13 -3
  11. package/lib/Reflection.js.map +1 -1
  12. package/lib/Schema.d.ts +2 -2
  13. package/lib/Schema.js.map +1 -1
  14. package/lib/annotations.d.ts +29 -12
  15. package/lib/annotations.js +33 -0
  16. package/lib/annotations.js.map +1 -1
  17. package/lib/codegen/parser.js +83 -0
  18. package/lib/codegen/parser.js.map +1 -1
  19. package/lib/codegen/types.js +3 -0
  20. package/lib/codegen/types.js.map +1 -1
  21. package/lib/decoder/DecodeOperation.js +4 -0
  22. package/lib/decoder/DecodeOperation.js.map +1 -1
  23. package/lib/decoder/strategy/StateCallbacks.js.map +1 -1
  24. package/lib/encoder/EncodeOperation.js +2 -2
  25. package/lib/encoder/EncodeOperation.js.map +1 -1
  26. package/lib/index.d.ts +1 -1
  27. package/lib/index.js +2 -1
  28. package/lib/index.js.map +1 -1
  29. package/lib/types/HelperTypes.d.ts +34 -2
  30. package/lib/types/HelperTypes.js.map +1 -1
  31. package/lib/types/TypeContext.js +9 -1
  32. package/lib/types/TypeContext.js.map +1 -1
  33. package/package.json +1 -1
  34. package/src/Metadata.ts +16 -17
  35. package/src/Reflection.ts +13 -5
  36. package/src/Schema.ts +2 -3
  37. package/src/annotations.ts +65 -15
  38. package/src/codegen/parser.ts +107 -0
  39. package/src/codegen/types.ts +1 -0
  40. package/src/decoder/DecodeOperation.ts +4 -0
  41. package/src/decoder/strategy/StateCallbacks.ts +1 -1
  42. package/src/encoder/EncodeOperation.ts +4 -2
  43. package/src/index.ts +1 -1
  44. package/src/types/HelperTypes.ts +54 -2
  45. package/src/types/TypeContext.ts +11 -1
@@ -113,7 +113,7 @@ class TypeContext {
113
113
  // Workaround to allow using an empty Schema (with no `@type()` fields)
114
114
  //
115
115
  if (schema[Symbol.metadata] === undefined) {
116
- Metadata.init(schema);
116
+ Metadata.initialize(schema);
117
117
  }
118
118
  this.schemas.set(schema, typeid);
119
119
  return true;
@@ -129,6 +129,14 @@ class TypeContext {
129
129
  TypeContext.inheritedTypes.get(klass)?.forEach((child) => {
130
130
  this.discoverTypes(child, parentIndex, parentFieldViewTag);
131
131
  });
132
+ // add parent classes
133
+ let parent = klass;
134
+ while ((parent = Object.getPrototypeOf(parent)) &&
135
+ parent !== Schema && // stop at root (Schema)
136
+ parent !== Function.prototype // stop at root (non-Schema)
137
+ ) {
138
+ this.discoverTypes(parent);
139
+ }
132
140
  const metadata = (klass[Symbol.metadata] ??= {});
133
141
  // if any schema/field has filters, mark "context" as having filters.
134
142
  if (metadata[$viewFieldIndexes]) {
@@ -167,6 +175,13 @@ class TypeContext {
167
175
  }
168
176
  }
169
177
 
178
+ function getNormalizedType(type) {
179
+ return (Array.isArray(type))
180
+ ? { array: type[0] }
181
+ : (typeof (type['type']) !== "undefined")
182
+ ? type['type']
183
+ : type;
184
+ }
170
185
  const Metadata = {
171
186
  addField(metadata, index, name, type, descriptor) {
172
187
  if (index > 64) {
@@ -174,9 +189,7 @@ const Metadata = {
174
189
  }
175
190
  metadata[index] = Object.assign(metadata[index] || {}, // avoid overwriting previous field metadata (@owned / @deprecated)
176
191
  {
177
- type: (Array.isArray(type))
178
- ? { array: type[0] }
179
- : type,
192
+ type: getNormalizedType(type),
180
193
  index,
181
194
  name,
182
195
  });
@@ -283,11 +296,15 @@ const Metadata = {
283
296
  fieldIndex++;
284
297
  for (const field in fields) {
285
298
  const type = fields[field];
299
+ const normalizedType = getNormalizedType(type);
286
300
  // FIXME: this code is duplicated from @type() annotation
287
301
  const complexTypeKlass = (Array.isArray(type))
288
302
  ? getType("array")
289
303
  : (typeof (Object.keys(type)[0]) === "string") && getType(Object.keys(type)[0]);
290
- Metadata.addField(metadata, fieldIndex, field, type, getPropertyDescriptor(`_${field}`, fieldIndex, type, complexTypeKlass));
304
+ const childType = (complexTypeKlass)
305
+ ? Object.values(type)[0]
306
+ : normalizedType;
307
+ Metadata.addField(metadata, fieldIndex, field, type, getPropertyDescriptor(`_${field}`, fieldIndex, childType, complexTypeKlass));
291
308
  fieldIndex++;
292
309
  }
293
310
  return target;
@@ -320,16 +337,6 @@ const Metadata = {
320
337
  // assign parent metadata to current
321
338
  //
322
339
  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
340
  // $numFields
334
341
  Object.defineProperty(metadata, $numFields, {
335
342
  value: parentMetadata[$numFields],
@@ -1138,7 +1145,6 @@ const encodeSchemaOperation = function (encoder, bytes, changeTree, index, opera
1138
1145
  return;
1139
1146
  }
1140
1147
  const ref = changeTree.ref;
1141
- // const metadata: Metadata = ref.constructor[Symbol.metadata];
1142
1148
  const field = metadata[index];
1143
1149
  // TODO: inline this function call small performance gain
1144
1150
  encodeValue(encoder, bytes, metadata[index].type, ref[field.name], operation, it);
@@ -1213,7 +1219,8 @@ const encodeArray = function (encoder, bytes, changeTree, field, operation, it,
1213
1219
  // encode operation
1214
1220
  bytes[it.offset++] = operation & 255;
1215
1221
  // custom operations
1216
- if (operation === OPERATION.CLEAR) {
1222
+ if (operation === OPERATION.CLEAR ||
1223
+ operation === OPERATION.REVERSE) {
1217
1224
  return;
1218
1225
  }
1219
1226
  // encode index
@@ -1691,6 +1698,10 @@ const decodeArray = function (decoder, bytes, it, ref, allChanges) {
1691
1698
  ref.clear();
1692
1699
  return;
1693
1700
  }
1701
+ else if (operation === OPERATION.REVERSE) {
1702
+ ref.reverse();
1703
+ return;
1704
+ }
1694
1705
  else if (operation === OPERATION.DELETE_BY_REFID) {
1695
1706
  // TODO: refactor here, try to follow same flow as below
1696
1707
  const refId = number(bytes, it);
@@ -2966,6 +2977,37 @@ function defineTypes(target, fields, options) {
2966
2977
  }
2967
2978
  return target;
2968
2979
  }
2980
+ function schema(fields, name, inherits = Schema) {
2981
+ const defaultValues = {};
2982
+ const viewTagFields = {};
2983
+ for (let fieldName in fields) {
2984
+ const field = fields[fieldName];
2985
+ if (typeof (field) === "object") {
2986
+ if (field['default'] !== undefined) {
2987
+ defaultValues[fieldName] = field['default'];
2988
+ }
2989
+ if (field['view'] !== undefined) {
2990
+ viewTagFields[fieldName] = (typeof (field['view']) === "boolean")
2991
+ ? DEFAULT_VIEW_TAG
2992
+ : field['view'];
2993
+ }
2994
+ }
2995
+ }
2996
+ const klass = Metadata.setFields(class extends inherits {
2997
+ constructor(...args) {
2998
+ args[0] = Object.assign({}, defaultValues, args[0]);
2999
+ super(...args);
3000
+ }
3001
+ }, fields);
3002
+ for (let fieldName in viewTagFields) {
3003
+ view(viewTagFields[fieldName])(klass.prototype, fieldName);
3004
+ }
3005
+ if (name) {
3006
+ Object.defineProperty(klass, "name", { value: name });
3007
+ }
3008
+ klass.extends = (fields, name) => schema(fields, name, klass);
3009
+ return klass;
3010
+ }
2969
3011
 
2970
3012
  function getIndent(level) {
2971
3013
  return (new Array(level).fill(0)).map((_, i) => (i === level - 1) ? `└─ ` : ` `).join("");
@@ -4213,13 +4255,20 @@ class Reflection extends Schema {
4213
4255
  /**
4214
4256
  * Encodes the TypeContext of an Encoder into a buffer.
4215
4257
  *
4216
- * @param context TypeContext instance
4258
+ * @param encoder Encoder instance
4217
4259
  * @param it
4218
4260
  * @returns
4219
4261
  */
4220
- static encode(context, it = { offset: 0 }) {
4262
+ static encode(encoder, it = { offset: 0 }) {
4263
+ const context = encoder.context;
4221
4264
  const reflection = new Reflection();
4222
4265
  const reflectionEncoder = new Encoder(reflection);
4266
+ // rootType is usually the first schema passed to the Encoder
4267
+ // (unless it inherits from another schema)
4268
+ const rootType = context.schemas.get(encoder.state.constructor);
4269
+ if (rootType > 0) {
4270
+ reflection.rootType = rootType;
4271
+ }
4223
4272
  const buildType = (currentType, metadata) => {
4224
4273
  for (const fieldIndex in metadata) {
4225
4274
  const index = Number(fieldIndex);
@@ -4343,13 +4392,16 @@ class Reflection extends Schema {
4343
4392
  parentFieldIndex += reflectionType.fields.length;
4344
4393
  });
4345
4394
  });
4346
- const state = new (typeContext.get(0))();
4395
+ const state = new (typeContext.get(reflection.rootType || 0))();
4347
4396
  return new Decoder(state, typeContext);
4348
4397
  }
4349
4398
  }
4350
4399
  __decorate([
4351
4400
  type([ReflectionType])
4352
4401
  ], Reflection.prototype, "types", void 0);
4402
+ __decorate([
4403
+ type("number")
4404
+ ], Reflection.prototype, "rootType", void 0);
4353
4405
 
4354
4406
  function getDecoderStateCallbacks(decoder) {
4355
4407
  const $root = decoder.root;
@@ -4788,5 +4840,5 @@ registerType("array", { constructor: ArraySchema });
4788
4840
  registerType("set", { constructor: SetSchema });
4789
4841
  registerType("collection", { constructor: CollectionSchema, });
4790
4842
 
4791
- export { $changes, $childType, $decoder, $deleteByIndex, $encoder, $filter, $getByIndex, $track, ArraySchema, ChangeTree, CollectionSchema, Decoder, Encoder, MapSchema, Metadata, OPERATION, Reflection, ReflectionField, ReflectionType, Schema, SetSchema, StateView, TypeContext, decode, decodeKeyValueOperation, decodeSchemaOperation, defineTypes, deprecated, dumpChanges, encode, encodeArray as encodeKeyValueOperation, encodeSchemaOperation, getDecoderStateCallbacks, getRawChangesCallback, registerType, type, view };
4843
+ export { $changes, $childType, $decoder, $deleteByIndex, $encoder, $filter, $getByIndex, $track, ArraySchema, ChangeTree, CollectionSchema, Decoder, Encoder, MapSchema, Metadata, OPERATION, Reflection, ReflectionField, ReflectionType, Schema, SetSchema, StateView, TypeContext, decode, decodeKeyValueOperation, decodeSchemaOperation, defineTypes, deprecated, dumpChanges, encode, encodeArray as encodeKeyValueOperation, encodeSchemaOperation, getDecoderStateCallbacks, getRawChangesCallback, registerType, schema, type, view };
4792
4844
  //# sourceMappingURL=index.mjs.map