@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
@@ -115,7 +115,7 @@ class TypeContext {
115
115
  // Workaround to allow using an empty Schema (with no `@type()` fields)
116
116
  //
117
117
  if (schema[Symbol.metadata] === undefined) {
118
- Metadata.init(schema);
118
+ Metadata.initialize(schema);
119
119
  }
120
120
  this.schemas.set(schema, typeid);
121
121
  return true;
@@ -131,6 +131,14 @@ class TypeContext {
131
131
  TypeContext.inheritedTypes.get(klass)?.forEach((child) => {
132
132
  this.discoverTypes(child, parentIndex, parentFieldViewTag);
133
133
  });
134
+ // add parent classes
135
+ let parent = klass;
136
+ while ((parent = Object.getPrototypeOf(parent)) &&
137
+ parent !== Schema && // stop at root (Schema)
138
+ parent !== Function.prototype // stop at root (non-Schema)
139
+ ) {
140
+ this.discoverTypes(parent);
141
+ }
134
142
  const metadata = (klass[Symbol.metadata] ??= {});
135
143
  // if any schema/field has filters, mark "context" as having filters.
136
144
  if (metadata[$viewFieldIndexes]) {
@@ -169,6 +177,13 @@ class TypeContext {
169
177
  }
170
178
  }
171
179
 
180
+ function getNormalizedType(type) {
181
+ return (Array.isArray(type))
182
+ ? { array: type[0] }
183
+ : (typeof (type['type']) !== "undefined")
184
+ ? type['type']
185
+ : type;
186
+ }
172
187
  const Metadata = {
173
188
  addField(metadata, index, name, type, descriptor) {
174
189
  if (index > 64) {
@@ -176,9 +191,7 @@ const Metadata = {
176
191
  }
177
192
  metadata[index] = Object.assign(metadata[index] || {}, // avoid overwriting previous field metadata (@owned / @deprecated)
178
193
  {
179
- type: (Array.isArray(type))
180
- ? { array: type[0] }
181
- : type,
194
+ type: getNormalizedType(type),
182
195
  index,
183
196
  name,
184
197
  });
@@ -285,11 +298,15 @@ const Metadata = {
285
298
  fieldIndex++;
286
299
  for (const field in fields) {
287
300
  const type = fields[field];
301
+ const normalizedType = getNormalizedType(type);
288
302
  // FIXME: this code is duplicated from @type() annotation
289
303
  const complexTypeKlass = (Array.isArray(type))
290
304
  ? getType("array")
291
305
  : (typeof (Object.keys(type)[0]) === "string") && getType(Object.keys(type)[0]);
292
- Metadata.addField(metadata, fieldIndex, field, type, getPropertyDescriptor(`_${field}`, fieldIndex, type, complexTypeKlass));
306
+ const childType = (complexTypeKlass)
307
+ ? Object.values(type)[0]
308
+ : normalizedType;
309
+ Metadata.addField(metadata, fieldIndex, field, type, getPropertyDescriptor(`_${field}`, fieldIndex, childType, complexTypeKlass));
293
310
  fieldIndex++;
294
311
  }
295
312
  return target;
@@ -322,16 +339,6 @@ const Metadata = {
322
339
  // assign parent metadata to current
323
340
  //
324
341
  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
342
  // $numFields
336
343
  Object.defineProperty(metadata, $numFields, {
337
344
  value: parentMetadata[$numFields],
@@ -1140,7 +1147,6 @@ const encodeSchemaOperation = function (encoder, bytes, changeTree, index, opera
1140
1147
  return;
1141
1148
  }
1142
1149
  const ref = changeTree.ref;
1143
- // const metadata: Metadata = ref.constructor[Symbol.metadata];
1144
1150
  const field = metadata[index];
1145
1151
  // TODO: inline this function call small performance gain
1146
1152
  encodeValue(encoder, bytes, metadata[index].type, ref[field.name], operation, it);
@@ -1215,7 +1221,8 @@ const encodeArray = function (encoder, bytes, changeTree, field, operation, it,
1215
1221
  // encode operation
1216
1222
  bytes[it.offset++] = operation & 255;
1217
1223
  // custom operations
1218
- if (operation === exports.OPERATION.CLEAR) {
1224
+ if (operation === exports.OPERATION.CLEAR ||
1225
+ operation === exports.OPERATION.REVERSE) {
1219
1226
  return;
1220
1227
  }
1221
1228
  // encode index
@@ -1693,6 +1700,10 @@ const decodeArray = function (decoder, bytes, it, ref, allChanges) {
1693
1700
  ref.clear();
1694
1701
  return;
1695
1702
  }
1703
+ else if (operation === exports.OPERATION.REVERSE) {
1704
+ ref.reverse();
1705
+ return;
1706
+ }
1696
1707
  else if (operation === exports.OPERATION.DELETE_BY_REFID) {
1697
1708
  // TODO: refactor here, try to follow same flow as below
1698
1709
  const refId = number(bytes, it);
@@ -2968,6 +2979,37 @@ function defineTypes(target, fields, options) {
2968
2979
  }
2969
2980
  return target;
2970
2981
  }
2982
+ function schema(fields, name, inherits = Schema) {
2983
+ const defaultValues = {};
2984
+ const viewTagFields = {};
2985
+ for (let fieldName in fields) {
2986
+ const field = fields[fieldName];
2987
+ if (typeof (field) === "object") {
2988
+ if (field['default'] !== undefined) {
2989
+ defaultValues[fieldName] = field['default'];
2990
+ }
2991
+ if (field['view'] !== undefined) {
2992
+ viewTagFields[fieldName] = (typeof (field['view']) === "boolean")
2993
+ ? DEFAULT_VIEW_TAG
2994
+ : field['view'];
2995
+ }
2996
+ }
2997
+ }
2998
+ const klass = Metadata.setFields(class extends inherits {
2999
+ constructor(...args) {
3000
+ args[0] = Object.assign({}, defaultValues, args[0]);
3001
+ super(...args);
3002
+ }
3003
+ }, fields);
3004
+ for (let fieldName in viewTagFields) {
3005
+ view(viewTagFields[fieldName])(klass.prototype, fieldName);
3006
+ }
3007
+ if (name) {
3008
+ Object.defineProperty(klass, "name", { value: name });
3009
+ }
3010
+ klass.extends = (fields, name) => schema(fields, name, klass);
3011
+ return klass;
3012
+ }
2971
3013
 
2972
3014
  function getIndent(level) {
2973
3015
  return (new Array(level).fill(0)).map((_, i) => (i === level - 1) ? `└─ ` : ` `).join("");
@@ -4215,13 +4257,20 @@ class Reflection extends Schema {
4215
4257
  /**
4216
4258
  * Encodes the TypeContext of an Encoder into a buffer.
4217
4259
  *
4218
- * @param context TypeContext instance
4260
+ * @param encoder Encoder instance
4219
4261
  * @param it
4220
4262
  * @returns
4221
4263
  */
4222
- static encode(context, it = { offset: 0 }) {
4264
+ static encode(encoder, it = { offset: 0 }) {
4265
+ const context = encoder.context;
4223
4266
  const reflection = new Reflection();
4224
4267
  const reflectionEncoder = new Encoder(reflection);
4268
+ // rootType is usually the first schema passed to the Encoder
4269
+ // (unless it inherits from another schema)
4270
+ const rootType = context.schemas.get(encoder.state.constructor);
4271
+ if (rootType > 0) {
4272
+ reflection.rootType = rootType;
4273
+ }
4225
4274
  const buildType = (currentType, metadata) => {
4226
4275
  for (const fieldIndex in metadata) {
4227
4276
  const index = Number(fieldIndex);
@@ -4345,13 +4394,16 @@ class Reflection extends Schema {
4345
4394
  parentFieldIndex += reflectionType.fields.length;
4346
4395
  });
4347
4396
  });
4348
- const state = new (typeContext.get(0))();
4397
+ const state = new (typeContext.get(reflection.rootType || 0))();
4349
4398
  return new Decoder(state, typeContext);
4350
4399
  }
4351
4400
  }
4352
4401
  __decorate([
4353
4402
  type([ReflectionType])
4354
4403
  ], Reflection.prototype, "types", void 0);
4404
+ __decorate([
4405
+ type("number")
4406
+ ], Reflection.prototype, "rootType", void 0);
4355
4407
 
4356
4408
  function getDecoderStateCallbacks(decoder) {
4357
4409
  const $root = decoder.root;
@@ -4824,6 +4876,7 @@ exports.encodeSchemaOperation = encodeSchemaOperation;
4824
4876
  exports.getDecoderStateCallbacks = getDecoderStateCallbacks;
4825
4877
  exports.getRawChangesCallback = getRawChangesCallback;
4826
4878
  exports.registerType = registerType;
4879
+ exports.schema = schema;
4827
4880
  exports.type = type;
4828
4881
  exports.view = view;
4829
4882
  //# sourceMappingURL=index.js.map