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