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