@colyseus/schema 3.0.75 → 4.0.0

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 (157) hide show
  1. package/build/cjs/index.js +780 -429
  2. package/build/cjs/index.js.map +1 -1
  3. package/build/esm/index.mjs +778 -430
  4. package/build/esm/index.mjs.map +1 -1
  5. package/build/umd/index.js +780 -429
  6. package/lib/Reflection.d.ts +50 -17
  7. package/lib/Reflection.js +151 -202
  8. package/lib/Reflection.js.map +1 -1
  9. package/lib/Schema.d.ts +13 -1
  10. package/lib/Schema.js +73 -9
  11. package/lib/Schema.js.map +1 -1
  12. package/lib/annotations.d.ts +6 -1
  13. package/lib/annotations.js +8 -34
  14. package/lib/annotations.js.map +1 -1
  15. package/lib/bench_encode.js +34 -1
  16. package/lib/bench_encode.js.map +1 -1
  17. package/lib/codegen/api.js +35 -2
  18. package/lib/codegen/api.js.map +1 -1
  19. package/lib/codegen/cli.js +4 -1
  20. package/lib/codegen/cli.js.map +1 -1
  21. package/lib/codegen/parser.js +35 -2
  22. package/lib/codegen/parser.js.map +1 -1
  23. package/lib/codegen/types.js +34 -1
  24. package/lib/codegen/types.js.map +1 -1
  25. package/lib/decoder/DecodeOperation.d.ts +2 -2
  26. package/lib/decoder/DecodeOperation.js +3 -3
  27. package/lib/decoder/DecodeOperation.js.map +1 -1
  28. package/lib/decoder/Decoder.d.ts +3 -3
  29. package/lib/decoder/Decoder.js +2 -2
  30. package/lib/decoder/Decoder.js.map +1 -1
  31. package/lib/decoder/ReferenceTracker.d.ts +0 -1
  32. package/lib/decoder/ReferenceTracker.js +9 -7
  33. package/lib/decoder/ReferenceTracker.js.map +1 -1
  34. package/lib/decoder/strategy/Callbacks.d.ts +154 -0
  35. package/lib/decoder/strategy/Callbacks.js +340 -0
  36. package/lib/decoder/strategy/Callbacks.js.map +1 -0
  37. package/lib/decoder/strategy/{StateCallbacks.d.ts → getDecoderStateCallbacks.d.ts} +6 -0
  38. package/lib/decoder/strategy/{StateCallbacks.js → getDecoderStateCallbacks.js} +17 -10
  39. package/lib/decoder/strategy/getDecoderStateCallbacks.js.map +1 -0
  40. package/lib/encoder/ChangeTree.d.ts +2 -2
  41. package/lib/encoder/ChangeTree.js.map +1 -1
  42. package/lib/encoder/EncodeOperation.d.ts +2 -2
  43. package/lib/encoder/EncodeOperation.js +3 -3
  44. package/lib/encoder/EncodeOperation.js.map +1 -1
  45. package/lib/encoder/Encoder.d.ts +6 -6
  46. package/lib/encoder/Encoder.js +19 -18
  47. package/lib/encoder/Encoder.js.map +1 -1
  48. package/lib/encoder/Root.js +17 -14
  49. package/lib/encoder/Root.js.map +1 -1
  50. package/lib/encoder/StateView.js +13 -12
  51. package/lib/encoder/StateView.js.map +1 -1
  52. package/lib/encoding/decode.d.ts +2 -2
  53. package/lib/encoding/encode.d.ts +3 -1
  54. package/lib/encoding/encode.js.map +1 -1
  55. package/lib/index.d.ts +3 -2
  56. package/lib/index.js +7 -3
  57. package/lib/index.js.map +1 -1
  58. package/lib/types/HelperTypes.d.ts +7 -14
  59. package/lib/types/HelperTypes.js.map +1 -1
  60. package/lib/types/custom/ArraySchema.d.ts +2 -1
  61. package/lib/types/custom/ArraySchema.js.map +1 -1
  62. package/lib/types/custom/CollectionSchema.d.ts +2 -1
  63. package/lib/types/custom/CollectionSchema.js.map +1 -1
  64. package/lib/types/custom/MapSchema.d.ts +3 -2
  65. package/lib/types/custom/MapSchema.js.map +1 -1
  66. package/lib/types/custom/SetSchema.d.ts +2 -1
  67. package/lib/types/custom/SetSchema.js.map +1 -1
  68. package/lib/types/symbols.d.ts +1 -0
  69. package/lib/types/symbols.js +2 -1
  70. package/lib/types/symbols.js.map +1 -1
  71. package/lib/utils.js +1 -1
  72. package/lib/utils.js.map +1 -1
  73. package/package.json +12 -16
  74. package/src/Reflection.ts +185 -174
  75. package/src/Schema.ts +81 -13
  76. package/src/annotations.ts +14 -40
  77. package/src/codegen/parser.ts +1 -1
  78. package/src/decoder/DecodeOperation.ts +9 -9
  79. package/src/decoder/Decoder.ts +6 -6
  80. package/src/decoder/ReferenceTracker.ts +10 -8
  81. package/src/decoder/strategy/Callbacks.ts +547 -0
  82. package/src/decoder/strategy/{StateCallbacks.ts → getDecoderStateCallbacks.ts} +17 -11
  83. package/src/encoder/ChangeTree.ts +4 -7
  84. package/src/encoder/EncodeOperation.ts +9 -9
  85. package/src/encoder/Encoder.ts +26 -18
  86. package/src/encoder/Root.ts +20 -15
  87. package/src/encoder/StateView.ts +15 -13
  88. package/src/encoding/encode.ts +1 -1
  89. package/src/index.ts +3 -2
  90. package/src/types/HelperTypes.ts +13 -11
  91. package/src/types/custom/ArraySchema.ts +2 -1
  92. package/src/types/custom/CollectionSchema.ts +4 -2
  93. package/src/types/custom/MapSchema.ts +4 -2
  94. package/src/types/custom/SetSchema.ts +3 -1
  95. package/src/types/symbols.ts +1 -0
  96. package/src/utils.ts +2 -2
  97. package/lib/Decoder.d.ts +0 -16
  98. package/lib/Decoder.js +0 -182
  99. package/lib/Decoder.js.map +0 -1
  100. package/lib/Encoder.d.ts +0 -13
  101. package/lib/Encoder.js +0 -79
  102. package/lib/Encoder.js.map +0 -1
  103. package/lib/changes/ChangeSet.d.ts +0 -12
  104. package/lib/changes/ChangeSet.js +0 -35
  105. package/lib/changes/ChangeSet.js.map +0 -1
  106. package/lib/changes/ChangeTree.d.ts +0 -53
  107. package/lib/changes/ChangeTree.js +0 -202
  108. package/lib/changes/ChangeTree.js.map +0 -1
  109. package/lib/changes/DecodeOperation.d.ts +0 -15
  110. package/lib/changes/DecodeOperation.js +0 -186
  111. package/lib/changes/DecodeOperation.js.map +0 -1
  112. package/lib/changes/EncodeOperation.d.ts +0 -18
  113. package/lib/changes/EncodeOperation.js +0 -130
  114. package/lib/changes/EncodeOperation.js.map +0 -1
  115. package/lib/changes/ReferenceTracker.d.ts +0 -14
  116. package/lib/changes/ReferenceTracker.js +0 -83
  117. package/lib/changes/ReferenceTracker.js.map +0 -1
  118. package/lib/changes/consts.d.ts +0 -14
  119. package/lib/changes/consts.js +0 -18
  120. package/lib/changes/consts.js.map +0 -1
  121. package/lib/decoder/strategy/StateCallbacks.js.map +0 -1
  122. package/lib/decoding/decode.d.ts +0 -48
  123. package/lib/decoding/decode.js +0 -267
  124. package/lib/decoding/decode.js.map +0 -1
  125. package/lib/ecs.d.ts +0 -11
  126. package/lib/ecs.js +0 -160
  127. package/lib/ecs.js.map +0 -1
  128. package/lib/filters/index.d.ts +0 -8
  129. package/lib/filters/index.js +0 -24
  130. package/lib/filters/index.js.map +0 -1
  131. package/lib/spec.d.ts +0 -13
  132. package/lib/spec.js +0 -42
  133. package/lib/spec.js.map +0 -1
  134. package/lib/types/ArraySchema.d.ts +0 -238
  135. package/lib/types/ArraySchema.js +0 -555
  136. package/lib/types/ArraySchema.js.map +0 -1
  137. package/lib/types/CollectionSchema.d.ts +0 -35
  138. package/lib/types/CollectionSchema.js +0 -150
  139. package/lib/types/CollectionSchema.js.map +0 -1
  140. package/lib/types/MapSchema.d.ts +0 -38
  141. package/lib/types/MapSchema.js +0 -215
  142. package/lib/types/MapSchema.js.map +0 -1
  143. package/lib/types/SetSchema.d.ts +0 -32
  144. package/lib/types/SetSchema.js +0 -162
  145. package/lib/types/SetSchema.js.map +0 -1
  146. package/lib/types/typeRegistry.d.ts +0 -5
  147. package/lib/types/typeRegistry.js +0 -13
  148. package/lib/types/typeRegistry.js.map +0 -1
  149. package/lib/usage.d.ts +0 -1
  150. package/lib/usage.js +0 -22
  151. package/lib/usage.js.map +0 -1
  152. package/lib/v3.d.ts +0 -1
  153. package/lib/v3.js +0 -427
  154. package/lib/v3.js.map +0 -1
  155. package/lib/v3_experiment.d.ts +0 -1
  156. package/lib/v3_experiment.js +0 -407
  157. package/lib/v3_experiment.js.map +0 -1
@@ -1,24 +1,12 @@
1
- import { ArraySchema } from "./types/custom/ArraySchema";
1
+ import { schema, SchemaType } from "./annotations";
2
2
  import { Iterator } from "./encoding/decode";
3
3
  import { Encoder } from "./encoder/Encoder";
4
4
  import { Decoder } from "./decoder/Decoder";
5
5
  import { Schema } from "./Schema";
6
6
  /**
7
- * Reflection
7
+ * Static methods available on Reflection
8
8
  */
9
- export declare class ReflectionField extends Schema {
10
- name: string;
11
- type: string;
12
- referencedType: number;
13
- }
14
- export declare class ReflectionType extends Schema {
15
- id: number;
16
- extendsId: number;
17
- fields: ArraySchema<ReflectionField>;
18
- }
19
- export declare class Reflection extends Schema {
20
- types: ArraySchema<ReflectionType>;
21
- rootType: number;
9
+ interface ReflectionStatic {
22
10
  /**
23
11
  * Encodes the TypeContext of an Encoder into a buffer.
24
12
  *
@@ -26,7 +14,7 @@ export declare class Reflection extends Schema {
26
14
  * @param it
27
15
  * @returns
28
16
  */
29
- static encode(encoder: Encoder, it?: Iterator): Buffer;
17
+ encode: (encoder: Encoder, it?: Iterator) => Uint8Array;
30
18
  /**
31
19
  * Decodes the TypeContext from a buffer into a Decoder instance.
32
20
  *
@@ -34,5 +22,50 @@ export declare class Reflection extends Schema {
34
22
  * @param it
35
23
  * @returns Decoder instance
36
24
  */
37
- static decode<T extends Schema = Schema>(bytes: Buffer, it?: Iterator): Decoder<T>;
25
+ decode: <T extends Schema = Schema>(bytes: Uint8Array, it?: Iterator) => Decoder<T>;
38
26
  }
27
+ /**
28
+ * Reflection
29
+ */
30
+ export declare const ReflectionField: import("./annotations").SchemaWithExtendsConstructor<{
31
+ name: "string";
32
+ type: "string";
33
+ referencedType: "number";
34
+ }, import(".").AssignableProps<import(".").InferSchemaInstanceType<{
35
+ name: "string";
36
+ type: "string";
37
+ referencedType: "number";
38
+ }>>, typeof Schema>;
39
+ export type ReflectionField = SchemaType<typeof ReflectionField>;
40
+ export declare const ReflectionType: import("./annotations").SchemaWithExtendsConstructor<{
41
+ id: "number";
42
+ extendsId: "number";
43
+ fields: import("./annotations").SchemaWithExtendsConstructor<{
44
+ name: "string";
45
+ type: "string";
46
+ referencedType: "number";
47
+ }, import(".").AssignableProps<import(".").InferSchemaInstanceType<{
48
+ name: "string";
49
+ type: "string";
50
+ referencedType: "number";
51
+ }>>, typeof Schema>[];
52
+ }, import(".").AssignableProps<import(".").InferSchemaInstanceType<{
53
+ id: "number";
54
+ extendsId: "number";
55
+ fields: import("./annotations").SchemaWithExtendsConstructor<{
56
+ name: "string";
57
+ type: "string";
58
+ referencedType: "number";
59
+ }, import(".").AssignableProps<import(".").InferSchemaInstanceType<{
60
+ name: "string";
61
+ type: "string";
62
+ referencedType: "number";
63
+ }>>, typeof Schema>[];
64
+ }>>, typeof Schema>;
65
+ export type ReflectionType = SchemaType<typeof ReflectionType>;
66
+ export declare const Reflection: ReturnType<typeof schema<{
67
+ types: [typeof ReflectionType];
68
+ rootType: "number";
69
+ }>> & ReflectionStatic;
70
+ export type Reflection = SchemaType<typeof Reflection>;
71
+ export {};
package/lib/Reflection.js CHANGED
@@ -1,233 +1,182 @@
1
1
  "use strict";
2
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
- return c > 3 && r && Object.defineProperty(target, key, r), r;
7
- };
8
2
  Object.defineProperty(exports, "__esModule", { value: true });
9
3
  exports.Reflection = exports.ReflectionType = exports.ReflectionField = void 0;
10
4
  const annotations_1 = require("./annotations");
11
5
  const TypeContext_1 = require("./types/TypeContext");
12
6
  const Metadata_1 = require("./Metadata");
13
- const ArraySchema_1 = require("./types/custom/ArraySchema");
14
7
  const Encoder_1 = require("./encoder/Encoder");
15
8
  const Decoder_1 = require("./decoder/Decoder");
16
9
  const Schema_1 = require("./Schema");
17
10
  /**
18
11
  * Reflection
19
12
  */
20
- class ReflectionField extends Schema_1.Schema {
21
- }
22
- exports.ReflectionField = ReflectionField;
23
- __decorate([
24
- (0, annotations_1.type)("string")
25
- ], ReflectionField.prototype, "name", void 0);
26
- __decorate([
27
- (0, annotations_1.type)("string")
28
- ], ReflectionField.prototype, "type", void 0);
29
- __decorate([
30
- (0, annotations_1.type)("number")
31
- ], ReflectionField.prototype, "referencedType", void 0);
32
- class ReflectionType extends Schema_1.Schema {
33
- constructor() {
34
- super(...arguments);
35
- this.fields = new ArraySchema_1.ArraySchema();
36
- }
37
- }
38
- exports.ReflectionType = ReflectionType;
39
- __decorate([
40
- (0, annotations_1.type)("number")
41
- ], ReflectionType.prototype, "id", void 0);
42
- __decorate([
43
- (0, annotations_1.type)("number")
44
- ], ReflectionType.prototype, "extendsId", void 0);
45
- __decorate([
46
- (0, annotations_1.type)([ReflectionField])
47
- ], ReflectionType.prototype, "fields", void 0);
48
- class Reflection extends Schema_1.Schema {
49
- constructor() {
50
- super(...arguments);
51
- this.types = new ArraySchema_1.ArraySchema();
13
+ exports.ReflectionField = (0, annotations_1.schema)({
14
+ name: "string",
15
+ type: "string",
16
+ referencedType: "number",
17
+ });
18
+ exports.ReflectionType = (0, annotations_1.schema)({
19
+ id: "number",
20
+ extendsId: "number",
21
+ fields: [exports.ReflectionField],
22
+ });
23
+ exports.Reflection = (0, annotations_1.schema)({
24
+ types: [exports.ReflectionType],
25
+ rootType: "number",
26
+ });
27
+ exports.Reflection.encode = function (encoder, it = { offset: 0 }) {
28
+ const context = encoder.context;
29
+ const reflection = new exports.Reflection();
30
+ const reflectionEncoder = new Encoder_1.Encoder(reflection);
31
+ // rootType is usually the first schema passed to the Encoder
32
+ // (unless it inherits from another schema)
33
+ const rootType = context.schemas.get(encoder.state.constructor);
34
+ if (rootType > 0) {
35
+ reflection.rootType = rootType;
52
36
  }
53
- /**
54
- * Encodes the TypeContext of an Encoder into a buffer.
55
- *
56
- * @param encoder Encoder instance
57
- * @param it
58
- * @returns
59
- */
60
- static encode(encoder, it = { offset: 0 }) {
61
- const context = encoder.context;
62
- const reflection = new Reflection();
63
- const reflectionEncoder = new Encoder_1.Encoder(reflection);
64
- // rootType is usually the first schema passed to the Encoder
65
- // (unless it inherits from another schema)
66
- const rootType = context.schemas.get(encoder.state.constructor);
67
- if (rootType > 0) {
68
- reflection.rootType = rootType;
37
+ const includedTypeIds = new Set();
38
+ const pendingReflectionTypes = {};
39
+ // add type to reflection in a way that respects inheritance
40
+ // (parent types should be added before their children)
41
+ const addType = (type) => {
42
+ if (type.extendsId === undefined || includedTypeIds.has(type.extendsId)) {
43
+ includedTypeIds.add(type.id);
44
+ reflection.types.push(type);
45
+ const deps = pendingReflectionTypes[type.id];
46
+ if (deps !== undefined) {
47
+ delete pendingReflectionTypes[type.id];
48
+ deps.forEach((childType) => addType(childType));
49
+ }
69
50
  }
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));
82
- }
51
+ else {
52
+ if (pendingReflectionTypes[type.extendsId] === undefined) {
53
+ pendingReflectionTypes[type.extendsId] = [];
83
54
  }
84
- else {
85
- if (pendingReflectionTypes[type.extendsId] === undefined) {
86
- pendingReflectionTypes[type.extendsId] = [];
55
+ pendingReflectionTypes[type.extendsId].push(type);
56
+ }
57
+ };
58
+ context.schemas.forEach((typeid, klass) => {
59
+ const type = new exports.ReflectionType();
60
+ type.id = Number(typeid);
61
+ // support inheritance
62
+ const inheritFrom = Object.getPrototypeOf(klass);
63
+ if (inheritFrom !== Schema_1.Schema) {
64
+ type.extendsId = context.schemas.get(inheritFrom);
65
+ }
66
+ const metadata = klass[Symbol.metadata];
67
+ //
68
+ // FIXME: this is a workaround for inherited types without additional fields
69
+ // if metadata is the same reference as the parent class - it means the class has no own metadata
70
+ //
71
+ if (metadata !== inheritFrom[Symbol.metadata]) {
72
+ for (const fieldIndex in metadata) {
73
+ const index = Number(fieldIndex);
74
+ const fieldName = metadata[index].name;
75
+ // skip fields from parent classes
76
+ if (!Object.prototype.hasOwnProperty.call(metadata, fieldName)) {
77
+ continue;
87
78
  }
88
- pendingReflectionTypes[type.extendsId].push(type);
89
- }
90
- };
91
- context.schemas.forEach((typeid, klass) => {
92
- const type = new ReflectionType();
93
- type.id = Number(typeid);
94
- // support inheritance
95
- const inheritFrom = Object.getPrototypeOf(klass);
96
- if (inheritFrom !== Schema_1.Schema) {
97
- type.extendsId = context.schemas.get(inheritFrom);
98
- }
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;
79
+ const reflectionField = new exports.ReflectionField();
80
+ reflectionField.name = fieldName;
81
+ let fieldType;
82
+ const field = metadata[index];
83
+ if (typeof (field.type) === "string") {
84
+ fieldType = field.type;
85
+ }
86
+ else {
87
+ let childTypeSchema;
88
+ //
89
+ // TODO: refactor below.
90
+ //
91
+ if (Schema_1.Schema.is(field.type)) {
92
+ fieldType = "ref";
93
+ childTypeSchema = field.type;
118
94
  }
119
95
  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;
96
+ fieldType = Object.keys(field.type)[0];
97
+ if (typeof (field.type[fieldType]) === "string") {
98
+ fieldType += ":" + field.type[fieldType]; // array:string
127
99
  }
128
100
  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
- }
101
+ childTypeSchema = field.type[fieldType];
136
102
  }
137
- reflectionField.referencedType = (childTypeSchema)
138
- ? context.getTypeId(childTypeSchema)
139
- : -1;
140
103
  }
141
- reflectionField.type = fieldType;
142
- type.fields.push(reflectionField);
104
+ reflectionField.referencedType = (childTypeSchema)
105
+ ? context.getTypeId(childTypeSchema)
106
+ : -1;
143
107
  }
108
+ reflectionField.type = fieldType;
109
+ type.fields.push(reflectionField);
144
110
  }
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));
150
111
  }
151
- const buf = reflectionEncoder.encodeAll(it);
152
- return buf.slice(0, it.offset);
153
- // return Buffer.from(buf, 0, it.offset);
112
+ addType(type);
113
+ });
114
+ // in case there are types that were not added due to inheritance
115
+ for (const typeid in pendingReflectionTypes) {
116
+ pendingReflectionTypes[typeid].forEach((type) => reflection.types.push(type));
154
117
  }
155
- /**
156
- * Decodes the TypeContext from a buffer into a Decoder instance.
157
- *
158
- * @param bytes Reflection.encode() output
159
- * @param it
160
- * @returns Decoder instance
161
- */
162
- static decode(bytes, it) {
163
- const reflection = new Reflection();
164
- const reflectionDecoder = new Decoder_1.Decoder(reflection);
165
- reflectionDecoder.decode(bytes, it);
166
- const typeContext = new TypeContext_1.TypeContext();
167
- // 1st pass, initialize metadata + inheritance
168
- reflection.types.forEach((reflectionType) => {
169
- const parentClass = typeContext.get(reflectionType.extendsId) ?? Schema_1.Schema;
170
- const schema = class _ extends parentClass {
171
- };
172
- // register for inheritance support
173
- TypeContext_1.TypeContext.register(schema);
174
- // // for inheritance support
175
- // Metadata.initialize(schema);
176
- typeContext.add(schema, reflectionType.id);
177
- }, {});
178
- // define fields
179
- const addFields = (metadata, reflectionType, parentFieldIndex) => {
180
- reflectionType.fields.forEach((field, i) => {
181
- const fieldIndex = parentFieldIndex + i;
182
- if (field.referencedType !== undefined) {
183
- let fieldType = field.type;
184
- let refType = typeContext.get(field.referencedType);
185
- // map or array of primitive type (-1)
186
- if (!refType) {
187
- const typeInfo = field.type.split(":");
188
- fieldType = typeInfo[0];
189
- refType = typeInfo[1]; // string
190
- }
191
- if (fieldType === "ref") {
192
- Metadata_1.Metadata.addField(metadata, fieldIndex, field.name, refType);
193
- }
194
- else {
195
- Metadata_1.Metadata.addField(metadata, fieldIndex, field.name, { [fieldType]: refType });
196
- }
118
+ const buf = reflectionEncoder.encodeAll(it);
119
+ return buf.slice(0, it.offset);
120
+ };
121
+ exports.Reflection.decode = function (bytes, it) {
122
+ const reflection = new exports.Reflection();
123
+ const reflectionDecoder = new Decoder_1.Decoder(reflection);
124
+ reflectionDecoder.decode(bytes, it);
125
+ const typeContext = new TypeContext_1.TypeContext();
126
+ // 1st pass, initialize metadata + inheritance
127
+ reflection.types.forEach((reflectionType) => {
128
+ const parentClass = typeContext.get(reflectionType.extendsId) ?? Schema_1.Schema;
129
+ const schema = class _ extends parentClass {
130
+ };
131
+ // register for inheritance support
132
+ TypeContext_1.TypeContext.register(schema);
133
+ typeContext.add(schema, reflectionType.id);
134
+ }, {});
135
+ // define fields
136
+ const addFields = (metadata, reflectionType, parentFieldIndex) => {
137
+ reflectionType.fields.forEach((field, i) => {
138
+ const fieldIndex = parentFieldIndex + i;
139
+ if (field.referencedType !== undefined) {
140
+ let fieldType = field.type;
141
+ let refType = typeContext.get(field.referencedType);
142
+ // map or array of primitive type (-1)
143
+ if (!refType) {
144
+ const typeInfo = field.type.split(":");
145
+ fieldType = typeInfo[0];
146
+ refType = typeInfo[1]; // string
147
+ }
148
+ if (fieldType === "ref") {
149
+ Metadata_1.Metadata.addField(metadata, fieldIndex, field.name, refType);
197
150
  }
198
151
  else {
199
- Metadata_1.Metadata.addField(metadata, fieldIndex, field.name, field.type);
152
+ Metadata_1.Metadata.addField(metadata, fieldIndex, field.name, { [fieldType]: refType });
200
153
  }
201
- });
202
- };
203
- // 2nd pass, set fields
204
- reflection.types.forEach((reflectionType) => {
205
- const schema = typeContext.get(reflectionType.id);
206
- // for inheritance support
207
- const metadata = Metadata_1.Metadata.initialize(schema);
208
- const inheritedTypes = [];
209
- let parentType = reflectionType;
210
- do {
211
- inheritedTypes.push(parentType);
212
- parentType = reflection.types.find((t) => t.id === parentType.extendsId);
213
- } while (parentType);
214
- let parentFieldIndex = 0;
215
- inheritedTypes.reverse().forEach((reflectionType) => {
216
- // add fields from all inherited classes
217
- // TODO: refactor this to avoid adding fields from parent classes
218
- addFields(metadata, reflectionType, parentFieldIndex);
219
- parentFieldIndex += reflectionType.fields.length;
220
- });
154
+ }
155
+ else {
156
+ Metadata_1.Metadata.addField(metadata, fieldIndex, field.name, field.type);
157
+ }
221
158
  });
222
- const state = new (typeContext.get(reflection.rootType || 0))();
223
- return new Decoder_1.Decoder(state, typeContext);
224
- }
225
- }
226
- exports.Reflection = Reflection;
227
- __decorate([
228
- (0, annotations_1.type)([ReflectionType])
229
- ], Reflection.prototype, "types", void 0);
230
- __decorate([
231
- (0, annotations_1.type)("number")
232
- ], Reflection.prototype, "rootType", void 0);
159
+ };
160
+ // 2nd pass, set fields
161
+ reflection.types.forEach((reflectionType) => {
162
+ const schema = typeContext.get(reflectionType.id);
163
+ // for inheritance support
164
+ const metadata = Metadata_1.Metadata.initialize(schema);
165
+ const inheritedTypes = [];
166
+ let parentType = reflectionType;
167
+ do {
168
+ inheritedTypes.push(parentType);
169
+ parentType = reflection.types.find((t) => t.id === parentType.extendsId);
170
+ } while (parentType);
171
+ let parentFieldIndex = 0;
172
+ inheritedTypes.reverse().forEach((reflectionType) => {
173
+ // add fields from all inherited classes
174
+ // TODO: refactor this to avoid adding fields from parent classes
175
+ addFields(metadata, reflectionType, parentFieldIndex);
176
+ parentFieldIndex += reflectionType.fields.length;
177
+ });
178
+ });
179
+ const state = new (typeContext.get(reflection.rootType || 0))();
180
+ return new Decoder_1.Decoder(state, typeContext);
181
+ };
233
182
  //# sourceMappingURL=Reflection.js.map
@@ -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;IAqNnG,CAAC;IAlNG;;;;;;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,iBAA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AAC,UAAU,CAAC,CAAC;gBAChC,UAAU,GAAG,UAAU,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,KAAK,UAAU,CAAC,SAAS,CAAC,CAAC;YAC7E,CAAC,QAAQ,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;AAtND,gCAsNC;AArN2B;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 as keyof typeof field.type]) === \"string\") {\n fieldType += \":\" + field.type[fieldType as keyof typeof field.type]; // array:string\n\n } else {\n childTypeSchema = field.type[fieldType as keyof typeof field.type];\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 buf.slice(0, it.offset)\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"]}
1
+ {"version":3,"file":"Reflection.js","sourceRoot":"","sources":["../src/Reflection.ts"],"names":[],"mappings":";;;AAAA,+CAAkE;AAClE,qDAAkD;AAClD,yCAAsC;AAEtC,+CAA4C;AAC5C,+CAA4C;AAC5C,qCAAkC;AAyBlC;;GAEG;AACU,QAAA,eAAe,GAAG,IAAA,oBAAM,EAAC;IAClC,IAAI,EAAE,QAAQ;IACd,IAAI,EAAE,QAAQ;IACd,cAAc,EAAE,QAAQ;CAC3B,CAAC,CAAA;AAGW,QAAA,cAAc,GAAG,IAAA,oBAAM,EAAC;IACjC,EAAE,EAAE,QAAQ;IACZ,SAAS,E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C,KAAK,CAAC,OAAO,CAAC,CAAC,cAAc,EAAE,EAAE;QACxC,MAAM,MAAM,GAAG,WAAW,CAAC,GAAG,CAAC,cAAc,CAAC,EAAE,CAAC,CAAC;QAElD,0BAA0B;QAC1B,MAAM,QAAQ,GAAG,mBAAQ,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC;QAE7C,MAAM,cAAc,GAAqB,EAAE,CAAC;QAE5C,IAAI,UAAU,GAAmB,cAAc,CAAC;QAChD,GAAG,CAAC;YACA,cAAc,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;YAChC,UAAU,GAAG,UAAU,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,KAAK,UAAU,CAAC,SAAS,CAAC,CAAC;QAC7E,CAAC,QAAQ,UAAU,EAAE;QAErB,IAAI,gBAAgB,GAAG,CAAC,CAAC;QAEzB,cAAc,CAAC,OAAO,EAAE,CAAC,OAAO,CAAC,CAAC,cAAc,EAAE,EAAE;YAChD,wCAAwC;YACxC,iEAAiE;YACjE,SAAS,CAAC,QAAQ,EAAE,cAAc,EAAE,gBAAgB,CAAC,CAAC;YACtD,gBAAgB,IAAI,cAAc,CAAC,MAAM,CAAC,MAAM,CAAC;QACrD,CAAC,CAAC,CAAC;IACP,CAAC,CAAC,CAAC;IAEH,MAAM,KAAK,GAAM,KAAK,WAAW,CAAC,GAAG,CAAC,UAAU,CAAC,QAAQ,IAAI,CAAC,CAAoB,GAAE,CAAC;IAErF,OAAO,IAAI,iBAAO,CAAI,KAAK,EAAE,WAAW,CAAC,CAAC;AAC9C,CAAC,CAAA","sourcesContent":["import { PrimitiveType, schema, SchemaType } from \"./annotations\";\nimport { TypeContext } from \"./types/TypeContext\";\nimport { Metadata } from \"./Metadata\";\nimport { Iterator } from \"./encoding/decode\";\nimport { Encoder } from \"./encoder/Encoder\";\nimport { Decoder } from \"./decoder/Decoder\";\nimport { Schema } from \"./Schema\";\n\n/**\n * Static methods available on Reflection\n */\ninterface ReflectionStatic {\n /**\n * Encodes the TypeContext of an Encoder into a buffer.\n *\n * @param encoder Encoder instance\n * @param it\n * @returns\n */\n encode: (encoder: Encoder, it?: Iterator) => Uint8Array;\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 decode: <T extends Schema = Schema>(bytes: Uint8Array, it?: Iterator) => Decoder<T>;\n}\n\n/**\n * Reflection\n */\nexport const ReflectionField = schema({\n name: \"string\",\n type: \"string\",\n referencedType: \"number\",\n})\nexport type ReflectionField = SchemaType<typeof ReflectionField>;\n\nexport const ReflectionType = schema({\n id: \"number\",\n extendsId: \"number\",\n fields: [ ReflectionField ],\n})\nexport type ReflectionType = SchemaType<typeof ReflectionType>;\n\nexport const Reflection = schema({\n types: [ ReflectionType ],\n rootType: \"number\",\n}) as ReturnType<typeof schema<{\n types: [typeof ReflectionType];\n rootType: \"number\";\n}>> & ReflectionStatic;\n\nexport type Reflection = SchemaType<typeof Reflection>;\n\nReflection.encode = function (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 as keyof typeof field.type]) === \"string\") {\n fieldType += \":\" + field.type[fieldType as keyof typeof field.type]; // array:string\n\n } else {\n childTypeSchema = field.type[fieldType as keyof typeof field.type];\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 buf.slice(0, it.offset);\n};\n\nReflection.decode = function <T extends Schema = Schema>(bytes: Uint8Array, 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 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}"]}
package/lib/Schema.d.ts CHANGED
@@ -2,7 +2,7 @@ import { OPERATION } from './encoding/spec';
2
2
  import { type DefinitionType } from "./annotations";
3
3
  import { AssignableProps, NonFunctionPropNames, ToJSON } from './types/HelperTypes';
4
4
  import { ChangeSetName, ChangeTree, IRef, Ref } from './encoder/ChangeTree';
5
- import { $decoder, $deleteByIndex, $encoder, $filter, $getByIndex, $track } from './types/symbols';
5
+ import { $decoder, $deleteByIndex, $encoder, $filter, $getByIndex, $refId, $track } from './types/symbols';
6
6
  import { StateView } from './encoder/StateView';
7
7
  import type { Decoder } from './decoder/Decoder';
8
8
  import type { Metadata } from './Metadata';
@@ -13,6 +13,7 @@ export declare class Schema<C = any> implements IRef {
13
13
  static [Symbol.metadata]: Metadata;
14
14
  static [$encoder]: import("./encoder/EncodeOperation").EncodeOperation<any>;
15
15
  static [$decoder]: import("./decoder/DecodeOperation").DecodeOperation<any>;
16
+ [$refId]?: number;
16
17
  /**
17
18
  * Assign the property descriptors required to track changes on this instance.
18
19
  * @param instance
@@ -34,7 +35,18 @@ export declare class Schema<C = any> implements IRef {
34
35
  */
35
36
  static [$filter](ref: Schema, index: number, view: StateView): boolean;
36
37
  constructor(arg?: C);
38
+ /**
39
+ * Assign properties to the instance.
40
+ * @param props Properties to assign to the instance
41
+ * @returns
42
+ */
37
43
  assign<T extends Partial<this>>(props: AssignableProps<T>): this;
44
+ /**
45
+ * Restore the instance from JSON data.
46
+ * @param jsonData JSON data to restore the instance from
47
+ * @returns
48
+ */
49
+ restore(jsonData: ToJSON<this>): this;
38
50
  /**
39
51
  * (Server-side): Flag a property to be encoded for the next patch.
40
52
  * @param instance Schema instance