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