@colyseus/schema 3.0.19 → 3.0.21

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.
@@ -604,6 +604,7 @@
604
604
  * Keeps track of which classes extends which. (parent -> children)
605
605
  */
606
606
  static { this.inheritedTypes = new Map(); }
607
+ static { this.cachedContexts = new Map(); }
607
608
  static register(target) {
608
609
  const parent = Object.getPrototypeOf(target);
609
610
  if (parent !== Schema) {
@@ -615,17 +616,20 @@
615
616
  inherits.add(target);
616
617
  }
617
618
  }
619
+ static cache(rootClass) {
620
+ let context = TypeContext.cachedContexts.get(rootClass);
621
+ if (!context) {
622
+ context = new TypeContext(rootClass);
623
+ TypeContext.cachedContexts.set(rootClass, context);
624
+ }
625
+ return context;
626
+ }
618
627
  constructor(rootClass) {
619
628
  this.types = {};
620
629
  this.schemas = new Map();
621
630
  this.hasFilters = false;
622
631
  this.parentFiltered = {};
623
632
  if (rootClass) {
624
- //
625
- // TODO:
626
- // cache "discoverTypes" results for each rootClass
627
- // to avoid re-discovering types for each new context/room
628
- //
629
633
  this.discoverTypes(rootClass);
630
634
  }
631
635
  }
@@ -3317,7 +3321,7 @@
3317
3321
  static [(_a$1 = $encoder, _b$1 = $decoder, $filter)](ref, index, view) {
3318
3322
  return (!view ||
3319
3323
  typeof (ref[$childType]) === "string" ||
3320
- view.items.has(ref[$getByIndex](index)[$changes]));
3324
+ view.items.has((ref[$getByIndex](index) ?? ref.deletedItems[index])[$changes]));
3321
3325
  }
3322
3326
  static is(type) {
3323
3327
  return type['collection'] !== undefined;
@@ -3325,6 +3329,7 @@
3325
3329
  constructor(initialValues) {
3326
3330
  this.$items = new Map();
3327
3331
  this.$indexes = new Map();
3332
+ this.deletedItems = {};
3328
3333
  this.$refId = 0;
3329
3334
  this[$changes] = new ChangeTree(this);
3330
3335
  this[$changes].indexes = {};
@@ -3374,7 +3379,7 @@
3374
3379
  if (index === undefined) {
3375
3380
  return false;
3376
3381
  }
3377
- this[$changes].delete(index);
3382
+ this.deletedItems[index] = this[$changes].delete(index);
3378
3383
  this.$indexes.delete(index);
3379
3384
  return this.$items.delete(index);
3380
3385
  }
@@ -3419,6 +3424,9 @@
3419
3424
  this.$items.delete(key);
3420
3425
  this.$indexes.delete(index);
3421
3426
  }
3427
+ [$onEncodeEnd]() {
3428
+ this.deletedItems = {};
3429
+ }
3422
3430
  toArray() {
3423
3431
  return Array.from(this.$items.values());
3424
3432
  }
@@ -3473,7 +3481,7 @@
3473
3481
  static [(_a = $encoder, _b = $decoder, $filter)](ref, index, view) {
3474
3482
  return (!view ||
3475
3483
  typeof (ref[$childType]) === "string" ||
3476
- view.items.has(ref[$getByIndex](index)[$changes]));
3484
+ view.items.has((ref[$getByIndex](index) ?? ref.deletedItems[index])[$changes]));
3477
3485
  }
3478
3486
  static is(type) {
3479
3487
  return type['set'] !== undefined;
@@ -3481,6 +3489,7 @@
3481
3489
  constructor(initialValues) {
3482
3490
  this.$items = new Map();
3483
3491
  this.$indexes = new Map();
3492
+ this.deletedItems = {};
3484
3493
  this.$refId = 0;
3485
3494
  this[$changes] = new ChangeTree(this);
3486
3495
  this[$changes].indexes = {};
@@ -3530,7 +3539,7 @@
3530
3539
  if (index === undefined) {
3531
3540
  return false;
3532
3541
  }
3533
- this[$changes].delete(index);
3542
+ this.deletedItems[index] = this[$changes].delete(index);
3534
3543
  this.$indexes.delete(index);
3535
3544
  return this.$items.delete(index);
3536
3545
  }
@@ -3587,6 +3596,9 @@
3587
3596
  this.$items.delete(key);
3588
3597
  this.$indexes.delete(index);
3589
3598
  }
3599
+ [$onEncodeEnd]() {
3600
+ this.deletedItems = {};
3601
+ }
3590
3602
  toArray() {
3591
3603
  return Array.from(this.$items.values());
3592
3604
  }
@@ -3763,10 +3775,12 @@
3763
3775
  constructor(state) {
3764
3776
  this.sharedBuffer = Buffer.allocUnsafe(Encoder.BUFFER_SIZE);
3765
3777
  //
3766
- // TODO: cache and restore "Context" based on root schema
3767
- // (to avoid creating a new context for every new room)
3778
+ // Use .cache() here to avoid re-creating a new context for every new room instance.
3768
3779
  //
3769
- this.context = new TypeContext(state.constructor);
3780
+ // We may need to make this optional in case of dynamically created
3781
+ // schemas - which would lead to memory leaks
3782
+ //
3783
+ this.context = TypeContext.cache(state.constructor);
3770
3784
  this.root = new Root(this.context);
3771
3785
  this.setState(state);
3772
3786
  // console.log(">>>>>>>>>>>>>>>> Encoder types");
@@ -3791,7 +3805,7 @@
3791
3805
  view.invisible.add(changeTree);
3792
3806
  continue; // skip this change tree
3793
3807
  }
3794
- else if (view.invisible.has(changeTree)) {
3808
+ else {
3795
3809
  view.invisible.delete(changeTree); // remove from invisible list
3796
3810
  }
3797
3811
  }
@@ -4271,50 +4285,28 @@
4271
4285
  if (rootType > 0) {
4272
4286
  reflection.rootType = rootType;
4273
4287
  }
4274
- const buildType = (currentType, metadata) => {
4275
- for (const fieldIndex in metadata) {
4276
- const index = Number(fieldIndex);
4277
- const fieldName = metadata[index].name;
4278
- // skip fields from parent classes
4279
- if (!Object.prototype.hasOwnProperty.call(metadata, fieldName)) {
4280
- continue;
4288
+ const includedTypeIds = new Set();
4289
+ const pendingReflectionTypes = {};
4290
+ // add type to reflection in a way that respects inheritance
4291
+ // (parent types should be added before their children)
4292
+ const addType = (type) => {
4293
+ if (type.extendsId === undefined || includedTypeIds.has(type.extendsId)) {
4294
+ includedTypeIds.add(type.id);
4295
+ reflection.types.push(type);
4296
+ const deps = pendingReflectionTypes[type.id];
4297
+ if (deps !== undefined) {
4298
+ delete pendingReflectionTypes[type.id];
4299
+ deps.forEach((childType) => addType(childType));
4281
4300
  }
4282
- const field = new ReflectionField();
4283
- field.name = fieldName;
4284
- let fieldType;
4285
- const type = metadata[index].type;
4286
- if (typeof (type) === "string") {
4287
- fieldType = type;
4288
- }
4289
- else {
4290
- let childTypeSchema;
4291
- //
4292
- // TODO: refactor below.
4293
- //
4294
- if (Schema.is(type)) {
4295
- fieldType = "ref";
4296
- childTypeSchema = type;
4297
- }
4298
- else {
4299
- fieldType = Object.keys(type)[0];
4300
- if (typeof (type[fieldType]) === "string") {
4301
- fieldType += ":" + type[fieldType]; // array:string
4302
- }
4303
- else {
4304
- childTypeSchema = type[fieldType];
4305
- }
4306
- }
4307
- field.referencedType = (childTypeSchema)
4308
- ? context.getTypeId(childTypeSchema)
4309
- : -1;
4301
+ }
4302
+ else {
4303
+ if (pendingReflectionTypes[type.extendsId] === undefined) {
4304
+ pendingReflectionTypes[type.extendsId] = [];
4310
4305
  }
4311
- field.type = fieldType;
4312
- currentType.fields.push(field);
4306
+ pendingReflectionTypes[type.extendsId].push(type);
4313
4307
  }
4314
- reflection.types.push(currentType);
4315
4308
  };
4316
- for (let typeid in context.types) {
4317
- const klass = context.types[typeid];
4309
+ context.schemas.forEach((typeid, klass) => {
4318
4310
  const type = new ReflectionType();
4319
4311
  type.id = Number(typeid);
4320
4312
  // support inheritance
@@ -4322,7 +4314,57 @@
4322
4314
  if (inheritFrom !== Schema) {
4323
4315
  type.extendsId = context.schemas.get(inheritFrom);
4324
4316
  }
4325
- buildType(type, klass[Symbol.metadata]);
4317
+ const metadata = klass[Symbol.metadata];
4318
+ //
4319
+ // FIXME: this is a workaround for inherited types without additional fields
4320
+ // if metadata is the same reference as the parent class - it means the class has no own metadata
4321
+ //
4322
+ if (metadata !== inheritFrom[Symbol.metadata]) {
4323
+ for (const fieldIndex in metadata) {
4324
+ const index = Number(fieldIndex);
4325
+ const fieldName = metadata[index].name;
4326
+ // skip fields from parent classes
4327
+ if (!Object.prototype.hasOwnProperty.call(metadata, fieldName)) {
4328
+ continue;
4329
+ }
4330
+ const reflectionField = new ReflectionField();
4331
+ reflectionField.name = fieldName;
4332
+ let fieldType;
4333
+ const field = metadata[index];
4334
+ if (typeof (field.type) === "string") {
4335
+ fieldType = field.type;
4336
+ }
4337
+ else {
4338
+ let childTypeSchema;
4339
+ //
4340
+ // TODO: refactor below.
4341
+ //
4342
+ if (Schema.is(field.type)) {
4343
+ fieldType = "ref";
4344
+ childTypeSchema = field.type;
4345
+ }
4346
+ else {
4347
+ fieldType = Object.keys(field.type)[0];
4348
+ if (typeof (field.type[fieldType]) === "string") {
4349
+ fieldType += ":" + field.type[fieldType]; // array:string
4350
+ }
4351
+ else {
4352
+ childTypeSchema = field.type[fieldType];
4353
+ }
4354
+ }
4355
+ reflectionField.referencedType = (childTypeSchema)
4356
+ ? context.getTypeId(childTypeSchema)
4357
+ : -1;
4358
+ }
4359
+ reflectionField.type = fieldType;
4360
+ type.fields.push(reflectionField);
4361
+ }
4362
+ }
4363
+ addType(type);
4364
+ });
4365
+ // in case there are types that were not added due to inheritance
4366
+ for (const typeid in pendingReflectionTypes) {
4367
+ pendingReflectionTypes[typeid].forEach((type) => reflection.types.push(type));
4326
4368
  }
4327
4369
  const buf = reflectionEncoder.encodeAll(it);
4328
4370
  return Buffer.from(buf, 0, it.offset);
@@ -4747,10 +4789,11 @@
4747
4789
  } // skip "undefined" indexes
4748
4790
  const op = changeTree.indexedOperations[index] ?? exports.OPERATION.ADD;
4749
4791
  const tagAtIndex = metadata?.[index].tag;
4750
- if ((isInvisible || // if "invisible", include all
4751
- tagAtIndex === undefined || // "all change" with no tag
4752
- tagAtIndex === tag // tagged property
4753
- ) &&
4792
+ if (!changeTree.isNew && // new structures will be added as part of .encode() call, no need to force it to .encodeView()
4793
+ (isInvisible || // if "invisible", include all
4794
+ tagAtIndex === undefined || // "all change" with no tag
4795
+ tagAtIndex === tag // tagged property
4796
+ ) &&
4754
4797
  op !== exports.OPERATION.DELETE) {
4755
4798
  changes[index] = op;
4756
4799
  }
@@ -4779,10 +4822,8 @@
4779
4822
  if (parentChangeTree && (parentChangeTree.filteredChanges !== undefined)) {
4780
4823
  this.addParentOf(changeTree, tag);
4781
4824
  }
4782
- // parent is already available, no need to add it!
4783
- if (!this.invisible.has(changeTree)) {
4784
- return;
4785
- }
4825
+ // // parent is already available, no need to add it!
4826
+ // if (!this.invisible.has(changeTree)) { return; }
4786
4827
  }
4787
4828
  // add parent's tag properties
4788
4829
  if (changeTree.getChange(parentIndex) !== exports.OPERATION.DELETE) {
package/lib/Reflection.js CHANGED
@@ -67,50 +67,28 @@ class Reflection extends Schema_1.Schema {
67
67
  if (rootType > 0) {
68
68
  reflection.rootType = rootType;
69
69
  }
70
- const buildType = (currentType, metadata) => {
71
- for (const fieldIndex in metadata) {
72
- const index = Number(fieldIndex);
73
- const fieldName = metadata[index].name;
74
- // skip fields from parent classes
75
- if (!Object.prototype.hasOwnProperty.call(metadata, fieldName)) {
76
- continue;
70
+ const includedTypeIds = new Set();
71
+ const pendingReflectionTypes = {};
72
+ // add type to reflection in a way that respects inheritance
73
+ // (parent types should be added before their children)
74
+ const addType = (type) => {
75
+ if (type.extendsId === undefined || includedTypeIds.has(type.extendsId)) {
76
+ includedTypeIds.add(type.id);
77
+ reflection.types.push(type);
78
+ const deps = pendingReflectionTypes[type.id];
79
+ if (deps !== undefined) {
80
+ delete pendingReflectionTypes[type.id];
81
+ deps.forEach((childType) => addType(childType));
77
82
  }
78
- const field = new ReflectionField();
79
- field.name = fieldName;
80
- let fieldType;
81
- const type = metadata[index].type;
82
- if (typeof (type) === "string") {
83
- fieldType = type;
84
- }
85
- else {
86
- let childTypeSchema;
87
- //
88
- // TODO: refactor below.
89
- //
90
- if (Schema_1.Schema.is(type)) {
91
- fieldType = "ref";
92
- childTypeSchema = type;
93
- }
94
- else {
95
- fieldType = Object.keys(type)[0];
96
- if (typeof (type[fieldType]) === "string") {
97
- fieldType += ":" + type[fieldType]; // array:string
98
- }
99
- else {
100
- childTypeSchema = type[fieldType];
101
- }
102
- }
103
- field.referencedType = (childTypeSchema)
104
- ? context.getTypeId(childTypeSchema)
105
- : -1;
83
+ }
84
+ else {
85
+ if (pendingReflectionTypes[type.extendsId] === undefined) {
86
+ pendingReflectionTypes[type.extendsId] = [];
106
87
  }
107
- field.type = fieldType;
108
- currentType.fields.push(field);
88
+ pendingReflectionTypes[type.extendsId].push(type);
109
89
  }
110
- reflection.types.push(currentType);
111
90
  };
112
- for (let typeid in context.types) {
113
- const klass = context.types[typeid];
91
+ context.schemas.forEach((typeid, klass) => {
114
92
  const type = new ReflectionType();
115
93
  type.id = Number(typeid);
116
94
  // support inheritance
@@ -118,7 +96,57 @@ class Reflection extends Schema_1.Schema {
118
96
  if (inheritFrom !== Schema_1.Schema) {
119
97
  type.extendsId = context.schemas.get(inheritFrom);
120
98
  }
121
- buildType(type, klass[Symbol.metadata]);
99
+ const metadata = klass[Symbol.metadata];
100
+ //
101
+ // FIXME: this is a workaround for inherited types without additional fields
102
+ // if metadata is the same reference as the parent class - it means the class has no own metadata
103
+ //
104
+ if (metadata !== inheritFrom[Symbol.metadata]) {
105
+ for (const fieldIndex in metadata) {
106
+ const index = Number(fieldIndex);
107
+ const fieldName = metadata[index].name;
108
+ // skip fields from parent classes
109
+ if (!Object.prototype.hasOwnProperty.call(metadata, fieldName)) {
110
+ continue;
111
+ }
112
+ const reflectionField = new ReflectionField();
113
+ reflectionField.name = fieldName;
114
+ let fieldType;
115
+ const field = metadata[index];
116
+ if (typeof (field.type) === "string") {
117
+ fieldType = field.type;
118
+ }
119
+ else {
120
+ let childTypeSchema;
121
+ //
122
+ // TODO: refactor below.
123
+ //
124
+ if (Schema_1.Schema.is(field.type)) {
125
+ fieldType = "ref";
126
+ childTypeSchema = field.type;
127
+ }
128
+ else {
129
+ fieldType = Object.keys(field.type)[0];
130
+ if (typeof (field.type[fieldType]) === "string") {
131
+ fieldType += ":" + field.type[fieldType]; // array:string
132
+ }
133
+ else {
134
+ childTypeSchema = field.type[fieldType];
135
+ }
136
+ }
137
+ reflectionField.referencedType = (childTypeSchema)
138
+ ? context.getTypeId(childTypeSchema)
139
+ : -1;
140
+ }
141
+ reflectionField.type = fieldType;
142
+ type.fields.push(reflectionField);
143
+ }
144
+ }
145
+ addType(type);
146
+ });
147
+ // in case there are types that were not added due to inheritance
148
+ for (const typeid in pendingReflectionTypes) {
149
+ pendingReflectionTypes[typeid].forEach((type) => reflection.types.push(type));
122
150
  }
123
151
  const buf = reflectionEncoder.encodeAll(it);
124
152
  return Buffer.from(buf, 0, it.offset);
@@ -1 +1 @@
1
- {"version":3,"file":"Reflection.js","sourceRoot":"","sources":["../src/Reflection.ts"],"names":[],"mappings":";;;;;;;;;AAAA,+CAAoD;AACpD,qDAAkD;AAClD,yCAAsC;AACtC,4DAAyD;AAEzD,+CAA4C;AAC5C,+CAA4C;AAC5C,qCAAkC;AAElC;;GAEG;AACH,MAAa,eAAgB,SAAQ,eAAM;CAI1C;AAJD,0CAIC;AAHmB;IAAf,IAAA,kBAAI,EAAC,QAAQ,CAAC;6CAAc;AACb;IAAf,IAAA,kBAAI,EAAC,QAAQ,CAAC;6CAAc;AACb;IAAf,IAAA,kBAAI,EAAC,QAAQ,CAAC;uDAAwB;AAG3C,MAAa,cAAe,SAAQ,eAAM;IAA1C;;QAG+B,WAAM,GAAG,IAAI,yBAAW,EAAmB,CAAC;IAC3E,CAAC;CAAA;AAJD,wCAIC;AAHmB;IAAf,IAAA,kBAAI,EAAC,QAAQ,CAAC;0CAAY;AACX;IAAf,IAAA,kBAAI,EAAC,QAAQ,CAAC;iDAAmB;AACP;IAA1B,IAAA,kBAAI,EAAC,CAAE,eAAe,CAAE,CAAC;8CAA6C;AAG3E,MAAa,UAAW,SAAQ,eAAM;IAAtC;;QAC4B,UAAK,GAAgC,IAAI,yBAAW,EAAkB,CAAC;IAmLnG,CAAC;IAhLG;;;;;;OAMG;IACH,MAAM,CAAC,MAAM,CAAC,OAAgB,EAAE,KAAe,EAAE,MAAM,EAAE,CAAC,EAAE;QACxD,MAAM,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC;QAEhC,MAAM,UAAU,GAAG,IAAI,UAAU,EAAE,CAAC;QACpC,MAAM,iBAAiB,GAAG,IAAI,iBAAO,CAAC,UAAU,CAAC,CAAC;QAElD,6DAA6D;QAC7D,2CAA2C;QAC3C,MAAM,QAAQ,GAAG,OAAO,CAAC,OAAO,CAAC,GAAG,CAA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AQ,UAAU,EAAE;YAErB,IAAI,gBAAgB,GAAG,CAAC,CAAC;YAEzB,cAAc,CAAC,OAAO,EAAE,CAAC,OAAO,CAAC,CAAC,cAAc,EAAE,EAAE;gBAChD,wCAAwC;gBACxC,iEAAiE;gBACjE,SAAS,CAAC,QAAQ,EAAE,cAAc,EAAE,gBAAgB,CAAC,CAAC;gBACtD,gBAAgB,IAAI,cAAc,CAAC,MAAM,CAAC,MAAM,CAAC;YACrD,CAAC,CAAC,CAAC;QACP,CAAC,CAAC,CAAC;QAEH,MAAM,KAAK,GAAM,KAAK,WAAW,CAAC,GAAG,CAAC,UAAU,CAAC,QAAQ,IAAI,CAAC,CAAoB,GAAE,CAAC;QAErF,OAAO,IAAI,iBAAO,CAAI,KAAK,EAAE,WAAW,CAAC,CAAC;IAC9C,CAAC;CACJ;AApLD,gCAoLC;AAnL2B;IAAvB,IAAA,kBAAI,EAAC,CAAC,cAAc,CAAC,CAAC;yCAAwE;AAC/E;IAAf,IAAA,kBAAI,EAAC,QAAQ,CAAC;4CAAkB","sourcesContent":["import { type, PrimitiveType } from \"./annotations\";\nimport { TypeContext } from \"./types/TypeContext\";\nimport { Metadata } from \"./Metadata\";\nimport { ArraySchema } from \"./types/custom/ArraySchema\";\nimport { Iterator } from \"./encoding/decode\";\nimport { Encoder } from \"./encoder/Encoder\";\nimport { Decoder } from \"./decoder/Decoder\";\nimport { Schema } from \"./Schema\";\n\n/**\n * Reflection\n */\nexport class ReflectionField extends Schema {\n @type(\"string\") name: string;\n @type(\"string\") type: string;\n @type(\"number\") referencedType: number;\n}\n\nexport class ReflectionType extends Schema {\n @type(\"number\") id: number;\n @type(\"number\") extendsId: number;\n @type([ ReflectionField ]) fields = new ArraySchema<ReflectionField>();\n}\n\nexport class Reflection extends Schema {\n @type([ReflectionType]) types: ArraySchema<ReflectionType> = new ArraySchema<ReflectionType>();\n @type(\"number\") rootType: number;\n\n /**\n * Encodes the TypeContext of an Encoder into a buffer.\n *\n * @param encoder Encoder instance\n * @param it\n * @returns\n */\n static encode(encoder: Encoder, it: Iterator = { offset: 0 }) {\n const context = encoder.context;\n\n const reflection = new Reflection();\n const reflectionEncoder = new Encoder(reflection);\n\n // rootType is usually the first schema passed to the Encoder\n // (unless it inherits from another schema)\n const rootType = context.schemas.get(encoder.state.constructor);\n if (rootType > 0) { reflection.rootType = rootType; }\n\n const buildType = (currentType: ReflectionType, metadata: Metadata) => {\n for (const fieldIndex in metadata) {\n const index = Number(fieldIndex);\n const fieldName = metadata[index].name;\n\n // skip fields from parent classes\n if (!Object.prototype.hasOwnProperty.call(metadata, fieldName)) {\n continue;\n }\n\n const field = new ReflectionField();\n field.name = fieldName;\n\n let fieldType: string;\n\n const type = metadata[index].type;\n\n if (typeof (type) === \"string\") {\n fieldType = type;\n\n } else {\n let childTypeSchema: typeof Schema;\n\n //\n // TODO: refactor below.\n //\n if (Schema.is(type)) {\n fieldType = \"ref\";\n childTypeSchema = type as typeof Schema;\n\n } else {\n fieldType = Object.keys(type)[0];\n\n if (typeof(type[fieldType]) === \"string\") {\n fieldType += \":\" + type[fieldType]; // array:string\n\n } else {\n childTypeSchema = type[fieldType];\n }\n }\n\n field.referencedType = (childTypeSchema)\n ? context.getTypeId(childTypeSchema)\n : -1;\n }\n\n field.type = fieldType;\n currentType.fields.push(field);\n }\n\n reflection.types.push(currentType);\n }\n\n for (let typeid in context.types) {\n const klass = context.types[typeid];\n const type = new ReflectionType();\n type.id = Number(typeid);\n\n // support inheritance\n const inheritFrom = Object.getPrototypeOf(klass);\n if (inheritFrom !== Schema) {\n type.extendsId = context.schemas.get(inheritFrom);\n }\n\n buildType(type, klass[Symbol.metadata]);\n }\n\n const buf = reflectionEncoder.encodeAll(it);\n return Buffer.from(buf, 0, it.offset);\n }\n\n /**\n * Decodes the TypeContext from a buffer into a Decoder instance.\n *\n * @param bytes Reflection.encode() output\n * @param it\n * @returns Decoder instance\n */\n static decode<T extends Schema = Schema>(bytes: Buffer, it?: Iterator): Decoder<T> {\n const reflection = new Reflection();\n\n const reflectionDecoder = new Decoder(reflection);\n reflectionDecoder.decode(bytes, it);\n\n const typeContext = new TypeContext();\n\n // 1st pass, initialize metadata + inheritance\n reflection.types.forEach((reflectionType) => {\n const parentClass: typeof Schema = typeContext.get(reflectionType.extendsId) ?? Schema;\n const schema: typeof Schema = class _ extends parentClass {};\n\n // register for inheritance support\n TypeContext.register(schema);\n\n // // for inheritance support\n // Metadata.initialize(schema);\n\n typeContext.add(schema, reflectionType.id);\n }, {});\n\n // define fields\n const addFields = (metadata: Metadata, reflectionType: ReflectionType, parentFieldIndex: number) => {\n reflectionType.fields.forEach((field, i) => {\n const fieldIndex = parentFieldIndex + i;\n\n if (field.referencedType !== undefined) {\n let fieldType = field.type;\n let refType: PrimitiveType = typeContext.get(field.referencedType);\n\n // map or array of primitive type (-1)\n if (!refType) {\n const typeInfo = field.type.split(\":\");\n fieldType = typeInfo[0];\n refType = typeInfo[1] as PrimitiveType; // string\n }\n\n if (fieldType === \"ref\") {\n Metadata.addField(metadata, fieldIndex, field.name, refType);\n\n } else {\n Metadata.addField(metadata, fieldIndex, field.name, { [fieldType]: refType });\n }\n\n } else {\n Metadata.addField(metadata, fieldIndex, field.name, field.type as PrimitiveType);\n }\n });\n };\n\n // 2nd pass, set fields\n reflection.types.forEach((reflectionType) => {\n const schema = typeContext.get(reflectionType.id);\n\n // for inheritance support\n const metadata = Metadata.initialize(schema);\n\n const inheritedTypes: ReflectionType[] = [];\n\n let parentType: ReflectionType = reflectionType;\n do {\n inheritedTypes.push(parentType);\n parentType = reflection.types.find((t) => t.id === parentType.extendsId);\n } while (parentType);\n\n let parentFieldIndex = 0;\n\n inheritedTypes.reverse().forEach((reflectionType) => {\n // add fields from all inherited classes\n // TODO: refactor this to avoid adding fields from parent classes\n addFields(metadata, reflectionType, parentFieldIndex);\n parentFieldIndex += reflectionType.fields.length;\n });\n });\n\n const state: T = new (typeContext.get(reflection.rootType || 0) as unknown as any)();\n\n return new Decoder<T>(state, typeContext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GAAG,CAAC,CAAC;YAEzB,cAAc,CAAC,OAAO,EAAE,CAAC,OAAO,CAAC,CAAC,cAAc,EAAE,EAAE;gBAChD,wCAAwC;gBACxC,iEAAiE;gBACjE,SAAS,CAAC,QAAQ,EAAE,cAAc,EAAE,gBAAgB,CAAC,CAAC;gBACtD,gBAAgB,IAAI,cAAc,CAAC,MAAM,CAAC,MAAM,CAAC;YACrD,CAAC,CAAC,CAAC;QACP,CAAC,CAAC,CAAC;QAEH,MAAM,KAAK,GAAM,KAAK,WAAW,CAAC,GAAG,CAAC,UAAU,CAAC,QAAQ,IAAI,CAAC,CAAoB,GAAE,CAAC;QAErF,OAAO,IAAI,iBAAO,CAAI,KAAK,EAAE,WAAW,CAAC,CAAC;IAC9C,CAAC;CACJ;AArND,gCAqNC;AApN2B;IAAvB,IAAA,kBAAI,EAAC,CAAC,cAAc,CAAC,CAAC;yCAAwE;AAC/E;IAAf,IAAA,kBAAI,EAAC,QAAQ,CAAC;4CAAkB","sourcesContent":["import { type, PrimitiveType } from \"./annotations\";\nimport { TypeContext } from \"./types/TypeContext\";\nimport { Metadata } from \"./Metadata\";\nimport { ArraySchema } from \"./types/custom/ArraySchema\";\nimport { Iterator } from \"./encoding/decode\";\nimport { Encoder } from \"./encoder/Encoder\";\nimport { Decoder } from \"./decoder/Decoder\";\nimport { Schema } from \"./Schema\";\n\n/**\n * Reflection\n */\nexport class ReflectionField extends Schema {\n @type(\"string\") name: string;\n @type(\"string\") type: string;\n @type(\"number\") referencedType: number;\n}\n\nexport class ReflectionType extends Schema {\n @type(\"number\") id: number;\n @type(\"number\") extendsId: number;\n @type([ ReflectionField ]) fields = new ArraySchema<ReflectionField>();\n}\n\nexport class Reflection extends Schema {\n @type([ReflectionType]) types: ArraySchema<ReflectionType> = new ArraySchema<ReflectionType>();\n @type(\"number\") rootType: number;\n\n /**\n * Encodes the TypeContext of an Encoder into a buffer.\n *\n * @param encoder Encoder instance\n * @param it\n * @returns\n */\n static encode(encoder: Encoder, it: Iterator = { offset: 0 }) {\n const context = encoder.context;\n\n const reflection = new Reflection();\n const reflectionEncoder = new Encoder(reflection);\n\n // rootType is usually the first schema passed to the Encoder\n // (unless it inherits from another schema)\n const rootType = context.schemas.get(encoder.state.constructor);\n if (rootType > 0) { reflection.rootType = rootType; }\n\n const includedTypeIds = new Set<number>();\n const pendingReflectionTypes: { [typeid: number]: ReflectionType[] } = {};\n\n // add type to reflection in a way that respects inheritance\n // (parent types should be added before their children)\n const addType = (type: ReflectionType) => {\n if (type.extendsId === undefined || includedTypeIds.has(type.extendsId)) {\n includedTypeIds.add(type.id);\n\n reflection.types.push(type);\n\n const deps = pendingReflectionTypes[type.id];\n if (deps !== undefined) {\n delete pendingReflectionTypes[type.id];\n deps.forEach((childType) => addType(childType));\n }\n } else {\n if (pendingReflectionTypes[type.extendsId] === undefined) {\n pendingReflectionTypes[type.extendsId] = [];\n }\n pendingReflectionTypes[type.extendsId].push(type);\n }\n };\n\n context.schemas.forEach((typeid, klass) => {\n const type = new ReflectionType();\n type.id = Number(typeid);\n\n // support inheritance\n const inheritFrom = Object.getPrototypeOf(klass);\n if (inheritFrom !== Schema) {\n type.extendsId = context.schemas.get(inheritFrom);\n }\n\n const metadata = klass[Symbol.metadata];\n\n //\n // FIXME: this is a workaround for inherited types without additional fields\n // if metadata is the same reference as the parent class - it means the class has no own metadata\n //\n if (metadata !== inheritFrom[Symbol.metadata]) {\n for (const fieldIndex in metadata) {\n const index = Number(fieldIndex);\n const fieldName = metadata[index].name;\n\n // skip fields from parent classes\n if (!Object.prototype.hasOwnProperty.call(metadata, fieldName)) {\n continue;\n }\n\n const reflectionField = new ReflectionField();\n reflectionField.name = fieldName;\n\n let fieldType: string;\n\n const field = metadata[index];\n\n if (typeof (field.type) === \"string\") {\n fieldType = field.type;\n\n } else {\n let childTypeSchema: typeof Schema;\n\n //\n // TODO: refactor below.\n //\n if (Schema.is(field.type)) {\n fieldType = \"ref\";\n childTypeSchema = field.type as typeof Schema;\n\n } else {\n fieldType = Object.keys(field.type)[0];\n\n if (typeof (field.type[fieldType]) === \"string\") {\n fieldType += \":\" + field.type[fieldType]; // array:string\n\n } else {\n childTypeSchema = field.type[fieldType];\n }\n }\n\n reflectionField.referencedType = (childTypeSchema)\n ? context.getTypeId(childTypeSchema)\n : -1;\n }\n\n reflectionField.type = fieldType;\n type.fields.push(reflectionField);\n }\n }\n\n addType(type);\n });\n\n // in case there are types that were not added due to inheritance\n for (const typeid in pendingReflectionTypes) {\n pendingReflectionTypes[typeid].forEach((type) =>\n reflection.types.push(type))\n }\n\n const buf = reflectionEncoder.encodeAll(it);\n return Buffer.from(buf, 0, it.offset);\n }\n\n /**\n * Decodes the TypeContext from a buffer into a Decoder instance.\n *\n * @param bytes Reflection.encode() output\n * @param it\n * @returns Decoder instance\n */\n static decode<T extends Schema = Schema>(bytes: Buffer, it?: Iterator): Decoder<T> {\n const reflection = new Reflection();\n\n const reflectionDecoder = new Decoder(reflection);\n reflectionDecoder.decode(bytes, it);\n\n const typeContext = new TypeContext();\n\n // 1st pass, initialize metadata + inheritance\n reflection.types.forEach((reflectionType) => {\n const parentClass: typeof Schema = typeContext.get(reflectionType.extendsId) ?? Schema;\n const schema: typeof Schema = class _ extends parentClass {};\n\n // register for inheritance support\n TypeContext.register(schema);\n\n // // for inheritance support\n // Metadata.initialize(schema);\n\n typeContext.add(schema, reflectionType.id);\n }, {});\n\n // define fields\n const addFields = (metadata: Metadata, reflectionType: ReflectionType, parentFieldIndex: number) => {\n reflectionType.fields.forEach((field, i) => {\n const fieldIndex = parentFieldIndex + i;\n\n if (field.referencedType !== undefined) {\n let fieldType = field.type;\n let refType: PrimitiveType = typeContext.get(field.referencedType);\n\n // map or array of primitive type (-1)\n if (!refType) {\n const typeInfo = field.type.split(\":\");\n fieldType = typeInfo[0];\n refType = typeInfo[1] as PrimitiveType; // string\n }\n\n if (fieldType === \"ref\") {\n Metadata.addField(metadata, fieldIndex, field.name, refType);\n\n } else {\n Metadata.addField(metadata, fieldIndex, field.name, { [fieldType]: refType });\n }\n\n } else {\n Metadata.addField(metadata, fieldIndex, field.name, field.type as PrimitiveType);\n }\n });\n };\n\n // 2nd pass, set fields\n reflection.types.forEach((reflectionType) => {\n const schema = typeContext.get(reflectionType.id);\n\n // for inheritance support\n const metadata = Metadata.initialize(schema);\n\n const inheritedTypes: ReflectionType[] = [];\n\n let parentType: ReflectionType = reflectionType;\n do {\n inheritedTypes.push(parentType);\n parentType = reflection.types.find((t) => t.id === parentType.extendsId);\n } while (parentType);\n\n let parentFieldIndex = 0;\n\n inheritedTypes.reverse().forEach((reflectionType) => {\n // add fields from all inherited classes\n // TODO: refactor this to avoid adding fields from parent classes\n addFields(metadata, reflectionType, parentFieldIndex);\n parentFieldIndex += reflectionType.fields.length;\n });\n });\n\n const state: T = new (typeContext.get(reflection.rootType || 0) as unknown as any)();\n\n return new Decoder<T>(state, typeContext);\n }\n}\n"]}
@@ -11,10 +11,12 @@ class Encoder {
11
11
  constructor(state) {
12
12
  this.sharedBuffer = Buffer.allocUnsafe(Encoder.BUFFER_SIZE);
13
13
  //
14
- // TODO: cache and restore "Context" based on root schema
15
- // (to avoid creating a new context for every new room)
14
+ // Use .cache() here to avoid re-creating a new context for every new room instance.
16
15
  //
17
- this.context = new TypeContext_1.TypeContext(state.constructor);
16
+ // We may need to make this optional in case of dynamically created
17
+ // schemas - which would lead to memory leaks
18
+ //
19
+ this.context = TypeContext_1.TypeContext.cache(state.constructor);
18
20
  this.root = new Root_1.Root(this.context);
19
21
  this.setState(state);
20
22
  // console.log(">>>>>>>>>>>>>>>> Encoder types");
@@ -39,7 +41,7 @@ class Encoder {
39
41
  view.invisible.add(changeTree);
40
42
  continue; // skip this change tree
41
43
  }
42
- else if (view.invisible.has(changeTree)) {
44
+ else {
43
45
  view.invisible.delete(changeTree); // remove from invisible list
44
46
  }
45
47
  }
@@ -1 +1 @@
1
- {"version":3,"file":"Encoder.js","sourceRoot":"","sources":["../../src/encoder/Encoder.ts"],"names":[],"mappings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E,YAAY,EAAE,EAAE,CAAC,CAAC;QAC3C,CAAC;IACL,CAAC;IAED,IAAI,UAAU;QACV,OAAO,CACH,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,MAAM,GAAG,CAAC;YAC5B,IAAI,CAAC,IAAI,CAAC,eAAe,CAAC,MAAM,GAAG,CAAC,CACvC,CAAC;IACN,CAAC;;AA7RL,0BA8RC","sourcesContent":["import type { Schema } from \"../Schema\";\nimport { TypeContext } from \"../types/TypeContext\";\nimport { $changes, $encoder, $filter, $getByIndex } from \"../types/symbols\";\n\nimport { encode } from \"../encoding/encode\";\nimport type { Iterator } from \"../encoding/decode\";\n\nimport { OPERATION, SWITCH_TO_STRUCTURE, TYPE_ID } from '../encoding/spec';\nimport { Root } from \"./Root\";\n\nimport type { StateView } from \"./StateView\";\nimport type { Metadata } from \"../Metadata\";\nimport type { ChangeTree } from \"./ChangeTree\";\n\nexport class Encoder<T extends Schema = any> {\n static BUFFER_SIZE = (typeof(Buffer) !== \"undefined\") && Buffer.poolSize || 8 * 1024; // 8KB\n sharedBuffer = Buffer.allocUnsafe(Encoder.BUFFER_SIZE);\n\n context: TypeContext;\n state: T;\n\n root: Root;\n\n constructor(state: T) {\n //\n // TODO: cache and restore \"Context\" based on root schema\n // (to avoid creating a new context for every new room)\n //\n this.context = new TypeContext(state.constructor as typeof Schema);\n this.root = new Root(this.context);\n\n this.setState(state);\n\n // console.log(\">>>>>>>>>>>>>>>> Encoder types\");\n // this.context.schemas.forEach((id, schema) => {\n // console.log(\"type:\", id, schema.name, Object.keys(schema[Symbol.metadata]));\n // });\n }\n\n protected setState(state: T) {\n this.state = state;\n this.state[$changes].setRoot(this.root);\n }\n\n encode(\n it: Iterator = { offset: 0 },\n view?: StateView,\n buffer = this.sharedBuffer,\n changeSetName: \"changes\" | \"allChanges\" | \"filteredChanges\" | \"allFilteredChanges\" = \"changes\",\n isEncodeAll = changeSetName === \"allChanges\",\n initialOffset = it.offset // cache current offset in case we need to resize the buffer\n ): Buffer {\n const hasView = (view !== undefined);\n const rootChangeTree = this.state[$changes];\n\n const shouldDiscardChanges = !isEncodeAll && !hasView;\n const changeTrees = this.root[changeSetName];\n\n for (let i = 0, numChangeTrees = changeTrees.length; i < numChangeTrees; i++) {\n const changeTree = changeTrees[i];\n\n if (hasView) {\n if (!view.items.has(changeTree)) {\n view.invisible.add(changeTree);\n continue; // skip this change tree\n\n } else if (view.invisible.has(changeTree)) {\n view.invisible.delete(changeTree); // remove from invisible list\n }\n }\n\n const operations = changeTree[changeSetName];\n const ref = changeTree.ref;\n\n // TODO: avoid iterating over change tree if no changes were made\n const numChanges = operations.operations.length;\n if (numChanges === 0) { continue; }\n\n const ctor = ref.constructor;\n const encoder = ctor[$encoder];\n const filter = ctor[$filter];\n const metadata = ctor[Symbol.metadata];\n\n // skip root `refId` if it's the first change tree\n // (unless it \"hasView\", which will need to revisit the root)\n if (hasView || it.offset > initialOffset || changeTree !== rootChangeTree) {\n buffer[it.offset++] = SWITCH_TO_STRUCTURE & 255;\n encode.number(buffer, changeTree.refId, it);\n }\n\n for (let j = 0; j < numChanges; j++) {\n const fieldIndex = operations.operations[j];\n\n const operation = (fieldIndex < 0)\n ? Math.abs(fieldIndex) // \"pure\" operation without fieldIndex (e.g. CLEAR, REVERSE, etc.)\n : (isEncodeAll)\n ? OPERATION.ADD\n : changeTree.indexedOperations[fieldIndex];\n\n //\n // first pass (encodeAll), identify \"filtered\" operations without encoding them\n // they will be encoded per client, based on their view.\n //\n // TODO: how can we optimize filtering out \"encode all\" operations?\n // TODO: avoid checking if no view tags were defined\n //\n if (fieldIndex === undefined || operation === undefined || (filter && !filter(ref, fieldIndex, view))) {\n // console.log(\"ADD AS INVISIBLE:\", fieldIndex, changeTree.ref.constructor.name)\n // view?.invisible.add(changeTree);\n continue;\n }\n\n encoder(this, buffer, changeTree, fieldIndex, operation, it, isEncodeAll, hasView, metadata);\n }\n\n // if (shouldDiscardChanges) {\n // changeTree.discard();\n // changeTree.isNew = false; // Not a new instance anymore\n // }\n }\n\n if (it.offset > buffer.byteLength) {\n // we can assume that n + 1 poolSize will suffice given that we are likely done with encoding at this point\n // multiples of poolSize are faster to allocate than arbitrary sizes\n // if we are on an older platform that doesn't implement pooling use 8kb as poolSize (that's the default for node)\n const newSize = Math.ceil(it.offset / (Buffer.poolSize ?? 8 * 1024)) * (Buffer.poolSize ?? 8 * 1024);\n\n console.warn(`@colyseus/schema buffer overflow. Encoded state is higher than default BUFFER_SIZE. Use the following to increase default BUFFER_SIZE:\n\n import { Encoder } from \"@colyseus/schema\";\n Encoder.BUFFER_SIZE = ${Math.round(newSize / 1024)} * 1024; // ${Math.round(newSize / 1024)} KB\n`);\n\n //\n // resize buffer and re-encode (TODO: can we avoid re-encoding here?)\n // -> No we probably can't unless we catch the need for resize before encoding which is likely more computationally expensive than resizing on demand\n //\n buffer = Buffer.alloc(newSize, buffer); // fill with buffer here to memcpy previous encoding steps beyond the initialOffset\n\n // assign resized buffer to local sharedBuffer\n if (buffer === this.sharedBuffer) {\n this.sharedBuffer = buffer;\n }\n\n return this.encode({ offset: initialOffset }, view, buffer, changeSetName, isEncodeAll);\n\n } else {\n //\n // only clear changes after making sure buffer resize is not required.\n //\n if (shouldDiscardChanges) {\n //\n // TODO: avoid iterating over change trees twice.\n //\n for (let i = 0, numChangeTrees = changeTrees.length; i < numChangeTrees; i++) {\n const changeTree = changeTrees[i];\n changeTree.discard();\n changeTree.isNew = false; // Not a new instance anymore\n }\n }\n\n return buffer.subarray(0, it.offset);\n }\n }\n\n encodeAll(it: Iterator = { offset: 0 }, buffer: Buffer = this.sharedBuffer) {\n return this.encode(it, undefined, buffer, \"allChanges\", true);\n }\n\n encodeAllView(view: StateView, sharedOffset: number, it: Iterator, bytes = this.sharedBuffer) {\n const viewOffset = it.offset;\n\n // try to encode \"filtered\" changes\n this.encode(it, view, bytes, \"allFilteredChanges\", true, viewOffset);\n\n return Buffer.concat([\n bytes.subarray(0, sharedOffset),\n bytes.subarray(viewOffset, it.offset)\n ]);\n }\n\n debugChanges(field: \"changes\" | \"allFilteredChanges\" | \"allChanges\" | \"filteredChanges\") {\n const rootChangeSet = (typeof (field) === \"string\")\n ? this.root[field]\n : field;\n\n rootChangeSet.forEach((changeTree) => {\n const changeSet = changeTree[field];\n\n const metadata: Metadata = changeTree.ref.constructor[Symbol.metadata];\n console.log(\"->\", { ref: changeTree.ref.constructor.name, refId: changeTree.refId, changes: Object.keys(changeSet).length });\n for (const index in changeSet) {\n const op = changeSet[index];\n console.log(\" ->\", {\n index,\n field: metadata?.[index],\n op: OPERATION[op],\n });\n }\n });\n }\n\n encodeView(view: StateView, sharedOffset: number, it: Iterator, bytes = this.sharedBuffer) {\n const viewOffset = it.offset;\n\n // encode visibility changes (add/remove for this view)\n for (const [refId, changes] of view.changes) {\n const changeTree: ChangeTree = this.root.changeTrees[refId];\n\n if (changeTree === undefined) {\n // detached instance, remove from view and skip.\n view.changes.delete(refId);\n continue;\n }\n\n const keys = Object.keys(changes);\n if (keys.length === 0) {\n // FIXME: avoid having empty changes if no changes were made\n // console.log(\"changes.size === 0, skip\", changeTree.ref.constructor.name);\n continue;\n }\n\n const ref = changeTree.ref;\n\n const ctor = ref.constructor;\n const encoder = ctor[$encoder];\n const metadata = ctor[Symbol.metadata];\n\n bytes[it.offset++] = SWITCH_TO_STRUCTURE & 255;\n encode.number(bytes, changeTree.refId, it);\n\n for (let i = 0, numChanges = keys.length; i < numChanges; i++) {\n const index = Number(keys[i]);\n // workaround when using view.add() on item that has been deleted from state (see test \"adding to view item that has been removed from state\")\n const value = changeTree.ref[$getByIndex](index);\n const operation = (value !== undefined && changes[index]) || OPERATION.DELETE;\n\n // isEncodeAll = false\n // hasView = true\n encoder(this, bytes, changeTree, index, operation, it, false, true, metadata);\n }\n }\n\n //\n // TODO: only clear view changes after all views are encoded\n // (to allow re-using StateView's for multiple clients)\n //\n // clear \"view\" changes after encoding\n view.changes.clear();\n\n // try to encode \"filtered\" changes\n this.encode(it, view, bytes, \"filteredChanges\", false, viewOffset);\n\n return Buffer.concat([\n bytes.subarray(0, sharedOffset),\n bytes.subarray(viewOffset, it.offset)\n ]);\n }\n\n discardChanges() {\n // discard shared changes\n let length = this.root.changes.length;\n if (length > 0) {\n while (length--) {\n this.root.changes[length]?.endEncode();\n }\n this.root.changes.length = 0;\n }\n\n // discard filtered changes\n length = this.root.filteredChanges.length;\n if (length > 0) {\n while (length--) {\n this.root.filteredChanges[length]?.endEncode();\n }\n this.root.filteredChanges.length = 0;\n }\n }\n\n tryEncodeTypeId (bytes: Buffer, baseType: typeof Schema, targetType: typeof Schema, it: Iterator) {\n const baseTypeId = this.context.getTypeId(baseType);\n const targetTypeId = this.context.getTypeId(targetType);\n\n if (targetTypeId === undefined) {\n console.warn(`@colyseus/schema WARNING: Class \"${targetType.name}\" is not registered on TypeRegistry - Please either tag the class with @entity or define a @type() field.`);\n return;\n }\n\n if (baseTypeId !== targetTypeId) {\n bytes[it.offset++] = TYPE_ID & 255;\n encode.number(bytes, targetTypeId, it);\n }\n }\n\n get hasChanges() {\n return (\n this.root.changes.length > 0 ||\n this.root.filteredChanges.length > 0\n );\n }\n}\n"]}
1
+ {"version":3,"file":"Encoder.js","sourceRoot":"","sources":["../../src/encoder/Encoder.ts"],"names":[],"mappings":";;;AACA,sDAAmD;AACnD,8CAA4E;AAE5E,+CAA4C;AAG5C,2CAA2E;AAC3E,iCAA8B;AAM9B,MAAa,OAAO;aACT,gBAAW,GAAG,CAAC,OAAM,CAAC,MAAM,CAAC,KAAK,WAAW,CAAC,IAAI,MAAM,CAAC,QAAQ,IAAI,CAAC,GAAG,IAAI,AAAlE,CAAmE,GAAC,MAAM;IAQ5F,YAAY,KAAQ;QAPpB,iBAAY,GAAG,MAAM,CAAC,WAAW,CAAC,OAAO,CAAC,WAAW,CAAC,CAAC;QAQnD,EAAE;QACF,oFAAoF;QACpF,EAAE;QACF,mEAAmE;QACnE,6CAA6C;QAC7C,EAAE;QACF,IAAI,CAAC,OAAO,GAAG,yBAAW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AC,EAAE,CAAC;YACb,OAAO,MAAM,EAAE,EAAE,CAAC;gBACd,IAAI,CAAC,IAAI,CAAC,eAAe,CAAC,MAAM,CAAC,EAAE,SAAS,EAAE,CAAC;YACnD,CAAC;YACD,IAAI,CAAC,IAAI,CAAC,eAAe,CAAC,MAAM,GAAG,CAAC,CAAC;QACzC,CAAC;IACL,CAAC;IAED,eAAe,CAAE,KAAa,EAAE,QAAuB,EAAE,UAAyB,EAAE,EAAY;QAC5F,MAAM,UAAU,GAAG,IAAI,CAAC,OAAO,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC;QACpD,MAAM,YAAY,GAAG,IAAI,CAAC,OAAO,CAAC,SAAS,CAAC,UAAU,CAAC,CAAC;QAExD,IAAI,YAAY,KAAK,SAAS,EAAE,CAAC;YAC7B,OAAO,CAAC,IAAI,CAAC,oCAAoC,UAAU,CAAC,IAAI,2GAA2G,CAAC,CAAC;YAC7K,OAAO;QACX,CAAC;QAED,IAAI,UAAU,KAAK,YAAY,EAAE,CAAC;YAC9B,KAAK,CAAC,EAAE,CAAC,MAAM,EAAE,CAAC,GAAG,cAAO,GAAG,GAAG,CAAC;YACnC,eAAM,CAAC,MAAM,CAAC,KAAK,EAAE,YAAY,EAAE,EAAE,CAAC,CAAC;QAC3C,CAAC;IACL,CAAC;IAED,IAAI,UAAU;QACV,OAAO,CACH,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,MAAM,GAAG,CAAC;YAC5B,IAAI,CAAC,IAAI,CAAC,eAAe,CAAC,MAAM,GAAG,CAAC,CACvC,CAAC;IACN,CAAC;;AA/RL,0BAgSC","sourcesContent":["import type { Schema } from \"../Schema\";\nimport { TypeContext } from \"../types/TypeContext\";\nimport { $changes, $encoder, $filter, $getByIndex } from \"../types/symbols\";\n\nimport { encode } from \"../encoding/encode\";\nimport type { Iterator } from \"../encoding/decode\";\n\nimport { OPERATION, SWITCH_TO_STRUCTURE, TYPE_ID } from '../encoding/spec';\nimport { Root } from \"./Root\";\n\nimport type { StateView } from \"./StateView\";\nimport type { Metadata } from \"../Metadata\";\nimport type { ChangeTree } from \"./ChangeTree\";\n\nexport class Encoder<T extends Schema = any> {\n static BUFFER_SIZE = (typeof(Buffer) !== \"undefined\") && Buffer.poolSize || 8 * 1024; // 8KB\n sharedBuffer = Buffer.allocUnsafe(Encoder.BUFFER_SIZE);\n\n context: TypeContext;\n state: T;\n\n root: Root;\n\n constructor(state: T) {\n //\n // Use .cache() here to avoid re-creating a new context for every new room instance.\n //\n // We may need to make this optional in case of dynamically created\n // schemas - which would lead to memory leaks\n //\n this.context = TypeContext.cache(state.constructor as typeof Schema);\n this.root = new Root(this.context);\n\n this.setState(state);\n\n // console.log(\">>>>>>>>>>>>>>>> Encoder types\");\n // this.context.schemas.forEach((id, schema) => {\n // console.log(\"type:\", id, schema.name, Object.keys(schema[Symbol.metadata]));\n // });\n }\n\n protected setState(state: T) {\n this.state = state;\n this.state[$changes].setRoot(this.root);\n }\n\n encode(\n it: Iterator = { offset: 0 },\n view?: StateView,\n buffer = this.sharedBuffer,\n changeSetName: \"changes\" | \"allChanges\" | \"filteredChanges\" | \"allFilteredChanges\" = \"changes\",\n isEncodeAll = changeSetName === \"allChanges\",\n initialOffset = it.offset // cache current offset in case we need to resize the buffer\n ): Buffer {\n const hasView = (view !== undefined);\n const rootChangeTree = this.state[$changes];\n\n const shouldDiscardChanges = !isEncodeAll && !hasView;\n const changeTrees = this.root[changeSetName];\n\n for (let i = 0, numChangeTrees = changeTrees.length; i < numChangeTrees; i++) {\n const changeTree = changeTrees[i];\n\n if (hasView) {\n if (!view.items.has(changeTree)) {\n view.invisible.add(changeTree);\n continue; // skip this change tree\n\n } else {\n view.invisible.delete(changeTree); // remove from invisible list\n }\n }\n\n const operations = changeTree[changeSetName];\n const ref = changeTree.ref;\n\n // TODO: avoid iterating over change tree if no changes were made\n const numChanges = operations.operations.length;\n if (numChanges === 0) { continue; }\n\n const ctor = ref.constructor;\n const encoder = ctor[$encoder];\n const filter = ctor[$filter];\n const metadata = ctor[Symbol.metadata];\n\n // skip root `refId` if it's the first change tree\n // (unless it \"hasView\", which will need to revisit the root)\n if (hasView || it.offset > initialOffset || changeTree !== rootChangeTree) {\n buffer[it.offset++] = SWITCH_TO_STRUCTURE & 255;\n encode.number(buffer, changeTree.refId, it);\n }\n\n for (let j = 0; j < numChanges; j++) {\n const fieldIndex = operations.operations[j];\n\n const operation = (fieldIndex < 0)\n ? Math.abs(fieldIndex) // \"pure\" operation without fieldIndex (e.g. CLEAR, REVERSE, etc.)\n : (isEncodeAll)\n ? OPERATION.ADD\n : changeTree.indexedOperations[fieldIndex];\n\n //\n // first pass (encodeAll), identify \"filtered\" operations without encoding them\n // they will be encoded per client, based on their view.\n //\n // TODO: how can we optimize filtering out \"encode all\" operations?\n // TODO: avoid checking if no view tags were defined\n //\n if (fieldIndex === undefined || operation === undefined || (filter && !filter(ref, fieldIndex, view))) {\n // console.log(\"ADD AS INVISIBLE:\", fieldIndex, changeTree.ref.constructor.name)\n // view?.invisible.add(changeTree);\n continue;\n }\n\n encoder(this, buffer, changeTree, fieldIndex, operation, it, isEncodeAll, hasView, metadata);\n }\n\n // if (shouldDiscardChanges) {\n // changeTree.discard();\n // changeTree.isNew = false; // Not a new instance anymore\n // }\n }\n\n if (it.offset > buffer.byteLength) {\n // we can assume that n + 1 poolSize will suffice given that we are likely done with encoding at this point\n // multiples of poolSize are faster to allocate than arbitrary sizes\n // if we are on an older platform that doesn't implement pooling use 8kb as poolSize (that's the default for node)\n const newSize = Math.ceil(it.offset / (Buffer.poolSize ?? 8 * 1024)) * (Buffer.poolSize ?? 8 * 1024);\n\n console.warn(`@colyseus/schema buffer overflow. Encoded state is higher than default BUFFER_SIZE. Use the following to increase default BUFFER_SIZE:\n\n import { Encoder } from \"@colyseus/schema\";\n Encoder.BUFFER_SIZE = ${Math.round(newSize / 1024)} * 1024; // ${Math.round(newSize / 1024)} KB\n`);\n\n //\n // resize buffer and re-encode (TODO: can we avoid re-encoding here?)\n // -> No we probably can't unless we catch the need for resize before encoding which is likely more computationally expensive than resizing on demand\n //\n buffer = Buffer.alloc(newSize, buffer); // fill with buffer here to memcpy previous encoding steps beyond the initialOffset\n\n // assign resized buffer to local sharedBuffer\n if (buffer === this.sharedBuffer) {\n this.sharedBuffer = buffer;\n }\n\n return this.encode({ offset: initialOffset }, view, buffer, changeSetName, isEncodeAll);\n\n } else {\n //\n // only clear changes after making sure buffer resize is not required.\n //\n if (shouldDiscardChanges) {\n //\n // TODO: avoid iterating over change trees twice.\n //\n for (let i = 0, numChangeTrees = changeTrees.length; i < numChangeTrees; i++) {\n const changeTree = changeTrees[i];\n changeTree.discard();\n changeTree.isNew = false; // Not a new instance anymore\n }\n }\n\n return buffer.subarray(0, it.offset);\n }\n }\n\n encodeAll(it: Iterator = { offset: 0 }, buffer: Buffer = this.sharedBuffer) {\n return this.encode(it, undefined, buffer, \"allChanges\", true);\n }\n\n encodeAllView(view: StateView, sharedOffset: number, it: Iterator, bytes = this.sharedBuffer) {\n const viewOffset = it.offset;\n\n // try to encode \"filtered\" changes\n this.encode(it, view, bytes, \"allFilteredChanges\", true, viewOffset);\n\n return Buffer.concat([\n bytes.subarray(0, sharedOffset),\n bytes.subarray(viewOffset, it.offset)\n ]);\n }\n\n debugChanges(field: \"changes\" | \"allFilteredChanges\" | \"allChanges\" | \"filteredChanges\") {\n const rootChangeSet = (typeof (field) === \"string\")\n ? this.root[field]\n : field;\n\n rootChangeSet.forEach((changeTree) => {\n const changeSet = changeTree[field];\n\n const metadata: Metadata = changeTree.ref.constructor[Symbol.metadata];\n console.log(\"->\", { ref: changeTree.ref.constructor.name, refId: changeTree.refId, changes: Object.keys(changeSet).length });\n for (const index in changeSet) {\n const op = changeSet[index];\n console.log(\" ->\", {\n index,\n field: metadata?.[index],\n op: OPERATION[op],\n });\n }\n });\n }\n\n encodeView(view: StateView, sharedOffset: number, it: Iterator, bytes = this.sharedBuffer) {\n const viewOffset = it.offset;\n\n // encode visibility changes (add/remove for this view)\n for (const [refId, changes] of view.changes) {\n const changeTree: ChangeTree = this.root.changeTrees[refId];\n\n if (changeTree === undefined) {\n // detached instance, remove from view and skip.\n view.changes.delete(refId);\n continue;\n }\n\n const keys = Object.keys(changes);\n if (keys.length === 0) {\n // FIXME: avoid having empty changes if no changes were made\n // console.log(\"changes.size === 0, skip\", changeTree.ref.constructor.name);\n continue;\n }\n\n const ref = changeTree.ref;\n\n const ctor = ref.constructor;\n const encoder = ctor[$encoder];\n const metadata = ctor[Symbol.metadata];\n\n bytes[it.offset++] = SWITCH_TO_STRUCTURE & 255;\n encode.number(bytes, changeTree.refId, it);\n\n for (let i = 0, numChanges = keys.length; i < numChanges; i++) {\n const index = Number(keys[i]);\n // workaround when using view.add() on item that has been deleted from state (see test \"adding to view item that has been removed from state\")\n const value = changeTree.ref[$getByIndex](index);\n const operation = (value !== undefined && changes[index]) || OPERATION.DELETE;\n\n // isEncodeAll = false\n // hasView = true\n encoder(this, bytes, changeTree, index, operation, it, false, true, metadata);\n }\n }\n\n //\n // TODO: only clear view changes after all views are encoded\n // (to allow re-using StateView's for multiple clients)\n //\n // clear \"view\" changes after encoding\n view.changes.clear();\n\n // try to encode \"filtered\" changes\n this.encode(it, view, bytes, \"filteredChanges\", false, viewOffset);\n\n return Buffer.concat([\n bytes.subarray(0, sharedOffset),\n bytes.subarray(viewOffset, it.offset)\n ]);\n }\n\n discardChanges() {\n // discard shared changes\n let length = this.root.changes.length;\n if (length > 0) {\n while (length--) {\n this.root.changes[length]?.endEncode();\n }\n this.root.changes.length = 0;\n }\n\n // discard filtered changes\n length = this.root.filteredChanges.length;\n if (length > 0) {\n while (length--) {\n this.root.filteredChanges[length]?.endEncode();\n }\n this.root.filteredChanges.length = 0;\n }\n }\n\n tryEncodeTypeId (bytes: Buffer, baseType: typeof Schema, targetType: typeof Schema, it: Iterator) {\n const baseTypeId = this.context.getTypeId(baseType);\n const targetTypeId = this.context.getTypeId(targetType);\n\n if (targetTypeId === undefined) {\n console.warn(`@colyseus/schema WARNING: Class \"${targetType.name}\" is not registered on TypeRegistry - Please either tag the class with @entity or define a @type() field.`);\n return;\n }\n\n if (baseTypeId !== targetTypeId) {\n bytes[it.offset++] = TYPE_ID & 255;\n encode.number(bytes, targetTypeId, it);\n }\n }\n\n get hasChanges() {\n return (\n this.root.changes.length > 0 ||\n this.root.filteredChanges.length > 0\n );\n }\n}\n"]}