@colyseus/schema 3.0.0-alpha.23 → 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) {
@@ -4146,7 +4148,11 @@ function getDecoderStateCallbacks(decoder) {
4146
4148
  }
4147
4149
  else {
4148
4150
  // collection instance not received yet
4149
- context.onInstanceAvailable((ref, existing) => onAdd(ref, prop, callback, immediate && existing));
4151
+ let detachCallback = () => { };
4152
+ context.onInstanceAvailable((ref, existing) => {
4153
+ detachCallback = onAdd(ref, prop, callback, immediate && existing);
4154
+ });
4155
+ return () => detachCallback();
4150
4156
  }
4151
4157
  },
4152
4158
  onChange: function onChange(callback) {
@@ -4221,19 +4227,27 @@ function getDecoderStateCallbacks(decoder) {
4221
4227
  //
4222
4228
  if (context.onInstanceAvailable) {
4223
4229
  // collection instance not received yet
4224
- context.onInstanceAvailable((ref, existing) => onAdd(ref, callback, immediate && existing && !isTriggeringOnAdd));
4230
+ let detachCallback = () => { };
4231
+ context.onInstanceAvailable((ref, existing) => {
4232
+ detachCallback = onAdd(ref, callback, immediate && existing && !isTriggeringOnAdd);
4233
+ });
4234
+ return () => detachCallback();
4225
4235
  }
4226
4236
  else if (context.instance) {
4227
- onAdd(context.instance, callback, immediate && !isTriggeringOnAdd);
4237
+ return onAdd(context.instance, callback, immediate && !isTriggeringOnAdd);
4228
4238
  }
4229
4239
  },
4230
4240
  onRemove: function (callback) {
4231
4241
  if (context.onInstanceAvailable) {
4232
4242
  // collection instance not received yet
4233
- context.onInstanceAvailable((ref) => onRemove(ref, callback));
4243
+ let detachCallback = () => { };
4244
+ context.onInstanceAvailable((ref) => {
4245
+ detachCallback = onRemove(ref, callback);
4246
+ });
4247
+ return () => detachCallback();
4234
4248
  }
4235
4249
  else if (context.instance) {
4236
- onRemove(context.instance, callback);
4250
+ return onRemove(context.instance, callback);
4237
4251
  }
4238
4252
  },
4239
4253
  }, {