@colyseus/schema 2.0.3 → 2.0.5

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.
Files changed (107) hide show
  1. package/README.md +0 -4
  2. package/build/cjs/index.js +54 -51
  3. package/build/cjs/index.js.map +1 -1
  4. package/build/esm/index.mjs +133 -105
  5. package/build/esm/index.mjs.map +1 -1
  6. package/build/umd/index.js +56 -53
  7. package/lib/Reflection.js +87 -119
  8. package/lib/Reflection.js.map +1 -1
  9. package/lib/Schema.js +197 -257
  10. package/lib/Schema.js.map +1 -1
  11. package/lib/annotations.d.ts +6 -6
  12. package/lib/annotations.js +64 -92
  13. package/lib/annotations.js.map +1 -1
  14. package/lib/changes/ChangeTree.d.ts +1 -1
  15. package/lib/changes/ChangeTree.js +63 -70
  16. package/lib/changes/ChangeTree.js.map +1 -1
  17. package/lib/changes/ReferenceTracker.js +24 -27
  18. package/lib/changes/ReferenceTracker.js.map +1 -1
  19. package/lib/codegen/api.js +9 -9
  20. package/lib/codegen/api.js.map +1 -1
  21. package/lib/codegen/argv.d.ts +1 -1
  22. package/lib/codegen/argv.js +11 -11
  23. package/lib/codegen/argv.js.map +1 -1
  24. package/lib/codegen/cli.js +21 -10
  25. package/lib/codegen/cli.js.map +1 -1
  26. package/lib/codegen/languages/cpp.js +126 -77
  27. package/lib/codegen/languages/cpp.js.map +1 -1
  28. package/lib/codegen/languages/csharp.js +121 -62
  29. package/lib/codegen/languages/csharp.js.map +1 -1
  30. package/lib/codegen/languages/haxe.js +34 -26
  31. package/lib/codegen/languages/haxe.js.map +1 -1
  32. package/lib/codegen/languages/java.js +39 -27
  33. package/lib/codegen/languages/java.js.map +1 -1
  34. package/lib/codegen/languages/js.js +48 -32
  35. package/lib/codegen/languages/js.js.map +1 -1
  36. package/lib/codegen/languages/lua.js +35 -24
  37. package/lib/codegen/languages/lua.js.map +1 -1
  38. package/lib/codegen/languages/ts.js +63 -68
  39. package/lib/codegen/languages/ts.js.map +1 -1
  40. package/lib/codegen/parser.d.ts +9 -1
  41. package/lib/codegen/parser.js +88 -46
  42. package/lib/codegen/parser.js.map +1 -1
  43. package/lib/codegen/types.d.ts +8 -0
  44. package/lib/codegen/types.js +64 -54
  45. package/lib/codegen/types.js.map +1 -1
  46. package/lib/encoding/decode.js +15 -15
  47. package/lib/encoding/decode.js.map +1 -1
  48. package/lib/encoding/encode.js +14 -14
  49. package/lib/encoding/encode.js.map +1 -1
  50. package/lib/events/EventEmitter.d.ts +1 -1
  51. package/lib/events/EventEmitter.js +16 -47
  52. package/lib/events/EventEmitter.js.map +1 -1
  53. package/lib/filters/index.js +7 -8
  54. package/lib/filters/index.js.map +1 -1
  55. package/lib/index.js +11 -11
  56. package/lib/index.js.map +1 -1
  57. package/lib/types/ArraySchema.d.ts +1 -1
  58. package/lib/types/ArraySchema.js +161 -219
  59. package/lib/types/ArraySchema.js.map +1 -1
  60. package/lib/types/CollectionSchema.d.ts +1 -1
  61. package/lib/types/CollectionSchema.js +63 -71
  62. package/lib/types/CollectionSchema.js.map +1 -1
  63. package/lib/types/HelperTypes.d.ts +9 -9
  64. package/lib/types/MapSchema.d.ts +16 -16
  65. package/lib/types/MapSchema.js +68 -78
  66. package/lib/types/MapSchema.js.map +1 -1
  67. package/lib/types/SetSchema.js +62 -71
  68. package/lib/types/SetSchema.js.map +1 -1
  69. package/lib/types/index.js +1 -1
  70. package/lib/types/index.js.map +1 -1
  71. package/lib/types/typeRegistry.js +1 -1
  72. package/lib/types/typeRegistry.js.map +1 -1
  73. package/lib/types/utils.js +9 -10
  74. package/lib/types/utils.js.map +1 -1
  75. package/lib/utils.js +10 -13
  76. package/lib/utils.js.map +1 -1
  77. package/package.json +6 -4
  78. package/src/Reflection.ts +159 -0
  79. package/src/Schema.ts +1024 -0
  80. package/src/annotations.ts +400 -0
  81. package/src/changes/ChangeTree.ts +295 -0
  82. package/src/changes/ReferenceTracker.ts +81 -0
  83. package/src/codegen/api.ts +46 -0
  84. package/src/codegen/argv.ts +40 -0
  85. package/src/codegen/cli.ts +65 -0
  86. package/src/codegen/languages/cpp.ts +297 -0
  87. package/src/codegen/languages/csharp.ts +208 -0
  88. package/src/codegen/languages/haxe.ts +110 -0
  89. package/src/codegen/languages/java.ts +115 -0
  90. package/src/codegen/languages/js.ts +115 -0
  91. package/src/codegen/languages/lua.ts +125 -0
  92. package/src/codegen/languages/ts.ts +129 -0
  93. package/src/codegen/parser.ts +299 -0
  94. package/src/codegen/types.ts +177 -0
  95. package/src/encoding/decode.ts +278 -0
  96. package/src/encoding/encode.ts +283 -0
  97. package/src/filters/index.ts +23 -0
  98. package/src/index.ts +59 -0
  99. package/src/spec.ts +49 -0
  100. package/src/types/ArraySchema.ts +612 -0
  101. package/src/types/CollectionSchema.ts +199 -0
  102. package/src/types/HelperTypes.ts +34 -0
  103. package/src/types/MapSchema.ts +268 -0
  104. package/src/types/SetSchema.ts +208 -0
  105. package/src/types/typeRegistry.ts +19 -0
  106. package/src/types/utils.ts +62 -0
  107. package/src/utils.ts +28 -0
package/lib/Reflection.js CHANGED
@@ -1,19 +1,4 @@
1
1
  "use strict";
2
- var __extends = (this && this.__extends) || (function () {
3
- var extendStatics = function (d, b) {
4
- extendStatics = Object.setPrototypeOf ||
5
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
- function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
- return extendStatics(d, b);
8
- };
9
- return function (d, b) {
10
- if (typeof b !== "function" && b !== null)
11
- throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
- extendStatics(d, b);
13
- function __() { this.constructor = d; }
14
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
- };
16
- })();
17
2
  var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
18
3
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
19
4
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
@@ -25,88 +10,78 @@ var __metadata = (this && this.__metadata) || function (k, v) {
25
10
  };
26
11
  Object.defineProperty(exports, "__esModule", { value: true });
27
12
  exports.Reflection = exports.ReflectionType = exports.ReflectionField = void 0;
28
- var annotations_1 = require("./annotations");
29
- var Schema_1 = require("./Schema");
30
- var ArraySchema_1 = require("./types/ArraySchema");
31
- var typeRegistry_1 = require("./types/typeRegistry");
32
- var reflectionContext = { context: new annotations_1.Context() };
13
+ const annotations_1 = require("./annotations");
14
+ const Schema_1 = require("./Schema");
15
+ const ArraySchema_1 = require("./types/ArraySchema");
16
+ const typeRegistry_1 = require("./types/typeRegistry");
17
+ const reflectionContext = { context: new annotations_1.Context() };
33
18
  /**
34
19
  * Reflection
35
20
  */
36
- var ReflectionField = /** @class */ (function (_super) {
37
- __extends(ReflectionField, _super);
38
- function ReflectionField() {
39
- return _super !== null && _super.apply(this, arguments) || this;
40
- }
41
- __decorate([
42
- annotations_1.type("string", reflectionContext),
43
- __metadata("design:type", String)
44
- ], ReflectionField.prototype, "name", void 0);
45
- __decorate([
46
- annotations_1.type("string", reflectionContext),
47
- __metadata("design:type", String)
48
- ], ReflectionField.prototype, "type", void 0);
49
- __decorate([
50
- annotations_1.type("number", reflectionContext),
51
- __metadata("design:type", Number)
52
- ], ReflectionField.prototype, "referencedType", void 0);
53
- return ReflectionField;
54
- }(Schema_1.Schema));
21
+ class ReflectionField extends Schema_1.Schema {
22
+ }
23
+ __decorate([
24
+ (0, annotations_1.type)("string", reflectionContext),
25
+ __metadata("design:type", String)
26
+ ], ReflectionField.prototype, "name", void 0);
27
+ __decorate([
28
+ (0, annotations_1.type)("string", reflectionContext),
29
+ __metadata("design:type", String)
30
+ ], ReflectionField.prototype, "type", void 0);
31
+ __decorate([
32
+ (0, annotations_1.type)("number", reflectionContext),
33
+ __metadata("design:type", Number)
34
+ ], ReflectionField.prototype, "referencedType", void 0);
55
35
  exports.ReflectionField = ReflectionField;
56
- var ReflectionType = /** @class */ (function (_super) {
57
- __extends(ReflectionType, _super);
58
- function ReflectionType() {
59
- var _this = _super !== null && _super.apply(this, arguments) || this;
60
- _this.fields = new ArraySchema_1.ArraySchema();
61
- return _this;
36
+ class ReflectionType extends Schema_1.Schema {
37
+ constructor() {
38
+ super(...arguments);
39
+ this.fields = new ArraySchema_1.ArraySchema();
62
40
  }
63
- __decorate([
64
- annotations_1.type("number", reflectionContext),
65
- __metadata("design:type", Number)
66
- ], ReflectionType.prototype, "id", void 0);
67
- __decorate([
68
- annotations_1.type([ReflectionField], reflectionContext),
69
- __metadata("design:type", ArraySchema_1.ArraySchema)
70
- ], ReflectionType.prototype, "fields", void 0);
71
- return ReflectionType;
72
- }(Schema_1.Schema));
41
+ }
42
+ __decorate([
43
+ (0, annotations_1.type)("number", reflectionContext),
44
+ __metadata("design:type", Number)
45
+ ], ReflectionType.prototype, "id", void 0);
46
+ __decorate([
47
+ (0, annotations_1.type)([ReflectionField], reflectionContext),
48
+ __metadata("design:type", ArraySchema_1.ArraySchema)
49
+ ], ReflectionType.prototype, "fields", void 0);
73
50
  exports.ReflectionType = ReflectionType;
74
- var Reflection = /** @class */ (function (_super) {
75
- __extends(Reflection, _super);
76
- function Reflection() {
77
- var _this = _super !== null && _super.apply(this, arguments) || this;
78
- _this.types = new ArraySchema_1.ArraySchema();
79
- return _this;
51
+ class Reflection extends Schema_1.Schema {
52
+ constructor() {
53
+ super(...arguments);
54
+ this.types = new ArraySchema_1.ArraySchema();
80
55
  }
81
- Reflection.encode = function (instance) {
82
- var rootSchemaType = instance.constructor;
83
- var reflection = new Reflection();
56
+ static encode(instance) {
57
+ const rootSchemaType = instance.constructor;
58
+ const reflection = new Reflection();
84
59
  reflection.rootType = rootSchemaType._typeid;
85
- var buildType = function (currentType, schema) {
86
- for (var fieldName in schema) {
87
- var field = new ReflectionField();
60
+ const buildType = (currentType, schema) => {
61
+ for (let fieldName in schema) {
62
+ const field = new ReflectionField();
88
63
  field.name = fieldName;
89
- var fieldType = void 0;
64
+ let fieldType;
90
65
  if (typeof (schema[fieldName]) === "string") {
91
66
  fieldType = schema[fieldName];
92
67
  }
93
68
  else {
94
- var type_1 = schema[fieldName];
95
- var childTypeSchema = void 0;
69
+ const type = schema[fieldName];
70
+ let childTypeSchema;
96
71
  //
97
72
  // TODO: refactor below.
98
73
  //
99
- if (Schema_1.Schema.is(type_1)) {
74
+ if (Schema_1.Schema.is(type)) {
100
75
  fieldType = "ref";
101
76
  childTypeSchema = schema[fieldName];
102
77
  }
103
78
  else {
104
- fieldType = Object.keys(type_1)[0];
105
- if (typeof (type_1[fieldType]) === "string") {
106
- fieldType += ":" + type_1[fieldType]; // array:string
79
+ fieldType = Object.keys(type)[0];
80
+ if (typeof (type[fieldType]) === "string") {
81
+ fieldType += ":" + type[fieldType]; // array:string
107
82
  }
108
83
  else {
109
- childTypeSchema = type_1[fieldType];
84
+ childTypeSchema = type[fieldType];
110
85
  }
111
86
  }
112
87
  field.referencedType = (childTypeSchema)
@@ -118,81 +93,74 @@ var Reflection = /** @class */ (function (_super) {
118
93
  }
119
94
  reflection.types.push(currentType);
120
95
  };
121
- var types = rootSchemaType._context.types;
122
- for (var typeid in types) {
123
- var type_2 = new ReflectionType();
124
- type_2.id = Number(typeid);
125
- buildType(type_2, types[typeid]._definition.schema);
96
+ const types = rootSchemaType._context.types;
97
+ for (let typeid in types) {
98
+ const type = new ReflectionType();
99
+ type.id = Number(typeid);
100
+ buildType(type, types[typeid]._definition.schema);
126
101
  }
127
102
  return reflection.encodeAll();
128
- };
129
- Reflection.decode = function (bytes, it) {
130
- var context = new annotations_1.Context();
131
- var reflection = new Reflection();
103
+ }
104
+ static decode(bytes, it) {
105
+ const context = new annotations_1.Context();
106
+ const reflection = new Reflection();
132
107
  reflection.decode(bytes, it);
133
- var schemaTypes = reflection.types.reduce(function (types, reflectionType) {
134
- var schema = /** @class */ (function (_super) {
135
- __extends(_, _super);
136
- function _() {
137
- return _super !== null && _super.apply(this, arguments) || this;
138
- }
139
- return _;
140
- }(Schema_1.Schema));
141
- var typeid = reflectionType.id;
108
+ const schemaTypes = reflection.types.reduce((types, reflectionType) => {
109
+ const schema = class _ extends Schema_1.Schema {
110
+ };
111
+ const typeid = reflectionType.id;
142
112
  types[typeid] = schema;
143
113
  context.add(schema, typeid);
144
114
  return types;
145
115
  }, {});
146
- reflection.types.forEach(function (reflectionType) {
147
- var schemaType = schemaTypes[reflectionType.id];
148
- reflectionType.fields.forEach(function (field) {
149
- var _a;
116
+ reflection.types.forEach((reflectionType) => {
117
+ const schemaType = schemaTypes[reflectionType.id];
118
+ reflectionType.fields.forEach(field => {
150
119
  if (field.referencedType !== undefined) {
151
- var fieldType = field.type;
152
- var refType = schemaTypes[field.referencedType];
120
+ let fieldType = field.type;
121
+ let refType = schemaTypes[field.referencedType];
153
122
  // map or array of primitive type (-1)
154
123
  if (!refType) {
155
- var typeInfo = field.type.split(":");
124
+ const typeInfo = field.type.split(":");
156
125
  fieldType = typeInfo[0];
157
126
  refType = typeInfo[1];
158
127
  }
159
128
  if (fieldType === "ref") {
160
- annotations_1.type(refType, { context: context })(schemaType.prototype, field.name);
129
+ (0, annotations_1.type)(refType, { context })(schemaType.prototype, field.name);
161
130
  }
162
131
  else {
163
- annotations_1.type((_a = {}, _a[fieldType] = refType, _a), { context: context })(schemaType.prototype, field.name);
132
+ (0, annotations_1.type)({ [fieldType]: refType }, { context })(schemaType.prototype, field.name);
164
133
  }
165
134
  }
166
135
  else {
167
- annotations_1.type(field.type, { context: context })(schemaType.prototype, field.name);
136
+ (0, annotations_1.type)(field.type, { context })(schemaType.prototype, field.name);
168
137
  }
169
138
  });
170
139
  });
171
- var rootType = schemaTypes[reflection.rootType];
172
- var rootInstance = new rootType();
140
+ const rootType = schemaTypes[reflection.rootType];
141
+ const rootInstance = new rootType();
173
142
  /**
174
143
  * auto-initialize referenced types on root type
175
144
  * to allow registering listeners immediatelly on client-side
176
145
  */
177
- for (var fieldName in rootType._definition.schema) {
178
- var fieldType = rootType._definition.schema[fieldName];
146
+ for (let fieldName in rootType._definition.schema) {
147
+ const fieldType = rootType._definition.schema[fieldName];
179
148
  if (typeof (fieldType) !== "string") {
180
149
  rootInstance[fieldName] = (typeof (fieldType) === "function")
181
150
  ? new fieldType() // is a schema reference
182
- : new (typeRegistry_1.getType(Object.keys(fieldType)[0])).constructor(); // is a "collection"
151
+ : new ((0, typeRegistry_1.getType)(Object.keys(fieldType)[0])).constructor(); // is a "collection"
183
152
  }
184
153
  }
185
154
  return rootInstance;
186
- };
187
- __decorate([
188
- annotations_1.type([ReflectionType], reflectionContext),
189
- __metadata("design:type", ArraySchema_1.ArraySchema)
190
- ], Reflection.prototype, "types", void 0);
191
- __decorate([
192
- annotations_1.type("number", reflectionContext),
193
- __metadata("design:type", Number)
194
- ], Reflection.prototype, "rootType", void 0);
195
- return Reflection;
196
- }(Schema_1.Schema));
155
+ }
156
+ }
157
+ __decorate([
158
+ (0, annotations_1.type)([ReflectionType], reflectionContext),
159
+ __metadata("design:type", ArraySchema_1.ArraySchema)
160
+ ], Reflection.prototype, "types", void 0);
161
+ __decorate([
162
+ (0, annotations_1.type)("number", reflectionContext),
163
+ __metadata("design:type", Number)
164
+ ], Reflection.prototype, "rootType", void 0);
197
165
  exports.Reflection = Reflection;
198
166
  //# sourceMappingURL=Reflection.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"Reflection.js","sourceRoot":"","sources":["../src/Reflection.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,6CAA6E;AAC7E,mCAAkC;AAClC,mDAAkD;AAClD,qDAA+C;AAG/C,IAAM,iBAAiB,GAAG,EAAE,OAAO,EAAE,IAAI,qBAAO,EAAE,EAAE,CAAC;AAErD;;GAEG;AACH;IAAqC,mCAAM;IAA3C;;IASA,CAAC;IAPG;QADC,kBAAI,CAAC,QAAQ,EAAE,iBAAiB,CAAC;;iDACrB;IAGb;QADC,kBAAI,CAAC,QAAQ,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,CAAC;;gDACjB;IA2HrB,iBAAC;CAAA,AAhID,CAAgC,eAAM,GAgIrC;AAhIY,gCAAU","sourcesContent":["import { type, PrimitiveType, Context, DefinitionType } from \"./annotations\";\nimport { Schema } from \"./Schema\";\nimport { ArraySchema } from \"./types/ArraySchema\";\nimport { getType } from \"./types/typeRegistry\";\nimport { Iterator } from \"./encoding/decode\";\n\nconst reflectionContext = { context: new Context() };\n\n/**\n * Reflection\n */\nexport class ReflectionField extends Schema {\n @type(\"string\", reflectionContext)\n name: string;\n\n @type(\"string\", reflectionContext)\n type: string;\n\n @type(\"number\", reflectionContext)\n referencedType: number;\n}\n\nexport class ReflectionType extends Schema {\n @type(\"number\", reflectionContext)\n id: number;\n\n @type([ ReflectionField ], reflectionContext)\n fields: ArraySchema<ReflectionField> = new ArraySchema<ReflectionField>();\n}\n\nexport class Reflection extends Schema {\n @type([ ReflectionType ], reflectionContext)\n types: ArraySchema<ReflectionType> = new ArraySchema<ReflectionType>();\n\n @type(\"number\", reflectionContext)\n rootType: number;\n\n static encode (instance: Schema) {\n const rootSchemaType = instance.constructor as typeof Schema;\n\n const reflection = new Reflection();\n reflection.rootType = rootSchemaType._typeid;\n\n const buildType = (currentType: ReflectionType, schema: any) => {\n for (let fieldName in schema) {\n const field = new ReflectionField();\n field.name = fieldName;\n\n let fieldType: string;\n\n if (typeof (schema[fieldName]) === \"string\") {\n fieldType = schema[fieldName];\n\n } else {\n const type = schema[fieldName];\n let childTypeSchema: typeof Schema;\n\n //\n // TODO: refactor below.\n //\n if (Schema.is(type)) {\n fieldType = \"ref\";\n childTypeSchema = schema[fieldName];\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 ? childTypeSchema._typeid\n : -1;\n }\n\n field.type = fieldType;\n currentType.fields.push(field);\n }\n\n reflection.types.push(currentType);\n }\n\n const types = rootSchemaType._context.types;\n for (let typeid in types) {\n const type = new ReflectionType();\n type.id = Number(typeid);\n buildType(type, types[typeid]._definition.schema);\n }\n\n return reflection.encodeAll();\n }\n\n static decode<T extends Schema = Schema>(bytes: number[], it?: Iterator): T {\n const context = new Context();\n\n const reflection = new Reflection();\n reflection.decode(bytes, it);\n\n const schemaTypes = reflection.types.reduce((types, reflectionType) => {\n const schema: typeof Schema = class _ extends Schema {};\n const typeid = reflectionType.id;\n types[typeid] = schema\n context.add(schema, typeid);\n return types;\n }, {});\n\n reflection.types.forEach((reflectionType) => {\n const schemaType = schemaTypes[reflectionType.id];\n\n reflectionType.fields.forEach(field => {\n if (field.referencedType !== undefined) {\n let fieldType = field.type;\n let refType = schemaTypes[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];\n }\n\n if (fieldType === \"ref\") {\n type(refType, { context })(schemaType.prototype, field.name);\n\n } else {\n type({ [fieldType]: refType } as DefinitionType, { context })(schemaType.prototype, field.name);\n }\n\n } else {\n type(field.type as PrimitiveType, { context })(schemaType.prototype, field.name);\n }\n });\n })\n\n const rootType: any = schemaTypes[reflection.rootType];\n const rootInstance = new rootType();\n\n /**\n * auto-initialize referenced types on root type\n * to allow registering listeners immediatelly on client-side\n */\n for (let fieldName in rootType._definition.schema) {\n const fieldType = rootType._definition.schema[fieldName];\n\n if (typeof(fieldType) !== \"string\") {\n rootInstance[fieldName] = (typeof (fieldType) === \"function\")\n ? new (fieldType as any)() // is a schema reference\n : new (getType(Object.keys(fieldType)[0])).constructor(); // is a \"collection\"\n }\n }\n\n return rootInstance;\n }\n}"]}
1
+ {"version":3,"file":"Reflection.js","sourceRoot":"","sources":["../src/Reflection.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,+CAA6E;AAC7E,qCAAkC;AAClC,qDAAkD;AAClD,uDAA+C;AAG/C,MAAM,iBAAiB,GAAG,EAAE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kBAAI,EAAC,CAAE,cAAc,CAAE,EAAE,iBAAiB,CAAC;8BACrC,yBAAW;yCAAqD;AAGvE;IADC,IAAA,kBAAI,EAAC,QAAQ,EAAE,iBAAiB,CAAC;;4CACjB;AALrB,gCAgIC","sourcesContent":["import { type, PrimitiveType, Context, DefinitionType } from \"./annotations\";\nimport { Schema } from \"./Schema\";\nimport { ArraySchema } from \"./types/ArraySchema\";\nimport { getType } from \"./types/typeRegistry\";\nimport { Iterator } from \"./encoding/decode\";\n\nconst reflectionContext = { context: new Context() };\n\n/**\n * Reflection\n */\nexport class ReflectionField extends Schema {\n @type(\"string\", reflectionContext)\n name: string;\n\n @type(\"string\", reflectionContext)\n type: string;\n\n @type(\"number\", reflectionContext)\n referencedType: number;\n}\n\nexport class ReflectionType extends Schema {\n @type(\"number\", reflectionContext)\n id: number;\n\n @type([ ReflectionField ], reflectionContext)\n fields: ArraySchema<ReflectionField> = new ArraySchema<ReflectionField>();\n}\n\nexport class Reflection extends Schema {\n @type([ ReflectionType ], reflectionContext)\n types: ArraySchema<ReflectionType> = new ArraySchema<ReflectionType>();\n\n @type(\"number\", reflectionContext)\n rootType: number;\n\n static encode (instance: Schema) {\n const rootSchemaType = instance.constructor as typeof Schema;\n\n const reflection = new Reflection();\n reflection.rootType = rootSchemaType._typeid;\n\n const buildType = (currentType: ReflectionType, schema: any) => {\n for (let fieldName in schema) {\n const field = new ReflectionField();\n field.name = fieldName;\n\n let fieldType: string;\n\n if (typeof (schema[fieldName]) === \"string\") {\n fieldType = schema[fieldName];\n\n } else {\n const type = schema[fieldName];\n let childTypeSchema: typeof Schema;\n\n //\n // TODO: refactor below.\n //\n if (Schema.is(type)) {\n fieldType = \"ref\";\n childTypeSchema = schema[fieldName];\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 ? childTypeSchema._typeid\n : -1;\n }\n\n field.type = fieldType;\n currentType.fields.push(field);\n }\n\n reflection.types.push(currentType);\n }\n\n const types = rootSchemaType._context.types;\n for (let typeid in types) {\n const type = new ReflectionType();\n type.id = Number(typeid);\n buildType(type, types[typeid]._definition.schema);\n }\n\n return reflection.encodeAll();\n }\n\n static decode<T extends Schema = Schema>(bytes: number[], it?: Iterator): T {\n const context = new Context();\n\n const reflection = new Reflection();\n reflection.decode(bytes, it);\n\n const schemaTypes = reflection.types.reduce((types, reflectionType) => {\n const schema: typeof Schema = class _ extends Schema {};\n const typeid = reflectionType.id;\n types[typeid] = schema\n context.add(schema, typeid);\n return types;\n }, {});\n\n reflection.types.forEach((reflectionType) => {\n const schemaType = schemaTypes[reflectionType.id];\n\n reflectionType.fields.forEach(field => {\n if (field.referencedType !== undefined) {\n let fieldType = field.type;\n let refType = schemaTypes[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];\n }\n\n if (fieldType === \"ref\") {\n type(refType, { context })(schemaType.prototype, field.name);\n\n } else {\n type({ [fieldType]: refType } as DefinitionType, { context })(schemaType.prototype, field.name);\n }\n\n } else {\n type(field.type as PrimitiveType, { context })(schemaType.prototype, field.name);\n }\n });\n })\n\n const rootType: any = schemaTypes[reflection.rootType];\n const rootInstance = new rootType();\n\n /**\n * auto-initialize referenced types on root type\n * to allow registering listeners immediatelly on client-side\n */\n for (let fieldName in rootType._definition.schema) {\n const fieldType = rootType._definition.schema[fieldName];\n\n if (typeof(fieldType) !== \"string\") {\n rootInstance[fieldName] = (typeof (fieldType) === \"function\")\n ? new (fieldType as any)() // is a schema reference\n : new (getType(Object.keys(fieldType)[0])).constructor(); // is a \"collection\"\n }\n }\n\n return rootInstance;\n }\n}"]}