@colyseus/schema 3.0.0-alpha.16 → 3.0.0-alpha.18

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.
@@ -144,6 +144,14 @@ const Metadata = {
144
144
  isDeprecated(metadata, field) {
145
145
  return metadata[field].deprecated === true;
146
146
  },
147
+ init(klass) {
148
+ //
149
+ // Used only to initialize an empty Schema (Encoder#constructor)
150
+ //
151
+ const metadata = {};
152
+ klass.constructor[Symbol.metadata] = metadata;
153
+ Object.defineProperty(metadata, -1, { value: 0, enumerable: false, configurable: true });
154
+ },
147
155
  isValidInstance(klass) {
148
156
  return (klass.constructor[Symbol.metadata] &&
149
157
  Object.prototype.hasOwnProperty.call(klass.constructor[Symbol.metadata], -1));
@@ -3412,6 +3420,10 @@ class Encoder {
3412
3420
  setRoot(state) {
3413
3421
  this.root = new Root();
3414
3422
  this.state = state;
3423
+ // Workaround to allow using an empty Schema.
3424
+ if (state.constructor[Symbol.metadata] === undefined) {
3425
+ Metadata.init(state);
3426
+ }
3415
3427
  state[$changes].setRoot(this.root);
3416
3428
  }
3417
3429
  encode(it = { offset: 0 }, view, buffer = this.sharedBuffer, changeTrees = this.root.changes, isEncodeAll = this.root.allChanges === changeTrees) {
@@ -4234,12 +4246,18 @@ class StateView {
4234
4246
  console.warn("StateView#add(), invalid object:", obj);
4235
4247
  return this;
4236
4248
  }
4249
+ // FIXME: ArraySchema/MapSchema does not have metadata
4250
+ const metadata = obj.constructor[Symbol.metadata];
4237
4251
  let changeTree = obj[$changes];
4238
4252
  this.items.add(changeTree);
4239
4253
  // Add children of this ChangeTree to this view
4240
- changeTree.forEachChild((change, _) => this.add(change.ref, tag));
4241
- // FIXME: ArraySchema/MapSchema does not have metadata
4242
- const metadata = obj.constructor[Symbol.metadata];
4254
+ changeTree.forEachChild((change, index) => {
4255
+ // Do not ADD children that don't have the same tag
4256
+ if (metadata && metadata[metadata[index]].tag !== tag) {
4257
+ return;
4258
+ }
4259
+ this.add(change.ref, tag);
4260
+ });
4243
4261
  // add parent ChangeTree's, if they are invisible to this view
4244
4262
  // TODO: REFACTOR addParent()
4245
4263
  this.addParent(changeTree, tag);
@@ -4266,7 +4284,6 @@ class StateView {
4266
4284
  tags = this.tags.get(changeTree);
4267
4285
  }
4268
4286
  tags.add(tag);
4269
- // console.log("BY TAG:", tag);
4270
4287
  // Ref: add tagged properties
4271
4288
  metadata?.[-3]?.[tag]?.forEach((index) => {
4272
4289
  if (changeTree.getChange(index) !== exports.OPERATION.DELETE) {