@colyseus/schema 3.0.0-alpha.24 → 3.0.0-alpha.25

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.
@@ -148,7 +148,7 @@ const Metadata = {
148
148
  // TODO: remove/refactor this...
149
149
  //
150
150
  const metadata = {};
151
- klass.constructor[Symbol.metadata] = metadata;
151
+ klass[Symbol.metadata] = metadata;
152
152
  Object.defineProperty(metadata, -1, {
153
153
  value: 0,
154
154
  enumerable: false,
@@ -2415,6 +2415,12 @@ class TypeContext {
2415
2415
  return false;
2416
2416
  }
2417
2417
  this.types[typeid] = schema;
2418
+ //
2419
+ // Workaround to allow using an empty Schema (with no `@type()` fields)
2420
+ //
2421
+ if (schema[Symbol.metadata] === undefined) {
2422
+ Metadata.init(schema);
2423
+ }
2418
2424
  this.schemas.set(schema, typeid);
2419
2425
  return true;
2420
2426
  }
@@ -3430,12 +3436,12 @@ class Encoder {
3430
3436
  static { this.BUFFER_SIZE = 8 * 1024; } // 8KB
3431
3437
  constructor(root) {
3432
3438
  this.sharedBuffer = Buffer.allocUnsafeSlow(Encoder.BUFFER_SIZE);
3433
- this.setRoot(root);
3434
3439
  //
3435
3440
  // TODO: cache and restore "Context" based on root schema
3436
3441
  // (to avoid creating a new context for every new room)
3437
3442
  //
3438
3443
  this.context = new TypeContext(root.constructor);
3444
+ this.setRoot(root);
3439
3445
  // console.log(">>>>>>>>>>>>>>>> Encoder types");
3440
3446
  // this.context.schemas.forEach((id, schema) => {
3441
3447
  // console.log("type:", id, schema.name, Object.keys(schema[Symbol.metadata]));
@@ -3444,10 +3450,6 @@ class Encoder {
3444
3450
  setRoot(state) {
3445
3451
  this.root = new Root();
3446
3452
  this.state = state;
3447
- // Workaround to allow using an empty Schema.
3448
- if (state.constructor[Symbol.metadata] === undefined) {
3449
- Metadata.init(state);
3450
- }
3451
3453
  state[$changes].setRoot(this.root);
3452
3454
  }
3453
3455
  encode(it = { offset: 0 }, view, buffer = this.sharedBuffer, changeTrees = this.root.changes, isEncodeAll = this.root.allChanges === changeTrees) {