@colyseus/schema 2.0.32 → 3.0.0-alpha.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.
- package/build/cjs/index.js +3614 -2634
- package/build/cjs/index.js.map +1 -1
- package/build/esm/index.mjs +3324 -2445
- package/build/esm/index.mjs.map +1 -1
- package/build/umd/index.js +3614 -2634
- package/lib/Decoder.d.ts +16 -0
- package/lib/Decoder.js +182 -0
- package/lib/Decoder.js.map +1 -0
- package/lib/Encoder.d.ts +13 -0
- package/lib/Encoder.js +79 -0
- package/lib/Encoder.js.map +1 -0
- package/lib/Metadata.d.ts +36 -0
- package/lib/Metadata.js +91 -0
- package/lib/Metadata.js.map +1 -0
- package/lib/Reflection.d.ts +7 -5
- package/lib/Reflection.js +62 -58
- package/lib/Reflection.js.map +1 -1
- package/lib/Schema.d.ts +39 -51
- package/lib/Schema.js +189 -731
- package/lib/Schema.js.map +1 -1
- package/lib/annotations.d.ts +26 -45
- package/lib/annotations.js +363 -194
- package/lib/annotations.js.map +1 -1
- package/lib/changes/ChangeSet.d.ts +12 -0
- package/lib/changes/ChangeSet.js +35 -0
- package/lib/changes/ChangeSet.js.map +1 -0
- package/lib/changes/DecodeOperation.d.ts +15 -0
- package/lib/changes/DecodeOperation.js +186 -0
- package/lib/changes/DecodeOperation.js.map +1 -0
- package/lib/changes/EncodeOperation.d.ts +18 -0
- package/lib/changes/EncodeOperation.js +130 -0
- package/lib/changes/EncodeOperation.js.map +1 -0
- package/lib/changes/consts.d.ts +14 -0
- package/lib/changes/consts.js +18 -0
- package/lib/changes/consts.js.map +1 -0
- package/lib/decoder/DecodeOperation.d.ts +24 -0
- package/lib/decoder/DecodeOperation.js +256 -0
- package/lib/decoder/DecodeOperation.js.map +1 -0
- package/lib/decoder/Decoder.d.ts +21 -0
- package/lib/decoder/Decoder.js +114 -0
- package/lib/decoder/Decoder.js.map +1 -0
- package/lib/decoder/ReferenceTracker.d.ts +26 -0
- package/lib/decoder/ReferenceTracker.js +131 -0
- package/lib/decoder/ReferenceTracker.js.map +1 -0
- package/lib/decoder/strategy/RawChanges.d.ts +3 -0
- package/lib/decoder/strategy/RawChanges.js +8 -0
- package/lib/decoder/strategy/RawChanges.js.map +1 -0
- package/lib/decoder/strategy/StateCallbacks.d.ts +20 -0
- package/lib/decoder/strategy/StateCallbacks.js +240 -0
- package/lib/decoder/strategy/StateCallbacks.js.map +1 -0
- package/lib/decoding/decode.d.ts +48 -0
- package/lib/decoding/decode.js +267 -0
- package/lib/decoding/decode.js.map +1 -0
- package/lib/ecs.d.ts +11 -0
- package/lib/ecs.js +160 -0
- package/lib/ecs.js.map +1 -0
- package/lib/encoder/ChangeTree.d.ts +72 -0
- package/lib/encoder/ChangeTree.js +384 -0
- package/lib/encoder/ChangeTree.js.map +1 -0
- package/lib/encoder/EncodeOperation.d.ts +25 -0
- package/lib/encoder/EncodeOperation.js +156 -0
- package/lib/encoder/EncodeOperation.js.map +1 -0
- package/lib/encoder/Encoder.d.ts +23 -0
- package/lib/encoder/Encoder.js +192 -0
- package/lib/encoder/Encoder.js.map +1 -0
- package/lib/encoder/StateView.d.ts +21 -0
- package/lib/encoder/StateView.js +196 -0
- package/lib/encoder/StateView.js.map +1 -0
- package/lib/encoding/assert.d.ts +9 -0
- package/lib/encoding/assert.js +47 -0
- package/lib/encoding/assert.js.map +1 -0
- package/lib/encoding/decode.js +1 -1
- package/lib/encoding/decode.js.map +1 -1
- package/lib/encoding/encode.d.ts +17 -16
- package/lib/encoding/encode.js +88 -81
- package/lib/encoding/encode.js.map +1 -1
- package/lib/encoding/spec.d.ts +25 -0
- package/lib/encoding/spec.js +30 -0
- package/lib/encoding/spec.js.map +1 -0
- package/lib/index.d.ts +18 -10
- package/lib/index.js +39 -17
- package/lib/index.js.map +1 -1
- package/lib/symbol.shim.d.ts +6 -0
- package/lib/symbol.shim.js +4 -0
- package/lib/symbol.shim.js.map +1 -0
- package/lib/types/ArraySchema.d.ts +1 -1
- package/lib/types/ArraySchema.js +0 -7
- package/lib/types/ArraySchema.js.map +1 -1
- package/lib/types/HelperTypes.d.ts +10 -2
- package/lib/types/HelperTypes.js.map +1 -1
- package/lib/types/custom/ArraySchema.d.ts +245 -0
- package/lib/types/custom/ArraySchema.js +659 -0
- package/lib/types/custom/ArraySchema.js.map +1 -0
- package/lib/types/custom/CollectionSchema.d.ts +42 -0
- package/lib/types/custom/CollectionSchema.js +165 -0
- package/lib/types/custom/CollectionSchema.js.map +1 -0
- package/lib/types/custom/MapSchema.d.ts +43 -0
- package/lib/types/custom/MapSchema.js +200 -0
- package/lib/types/custom/MapSchema.js.map +1 -0
- package/lib/types/custom/SetSchema.d.ts +39 -0
- package/lib/types/custom/SetSchema.js +177 -0
- package/lib/types/custom/SetSchema.js.map +1 -0
- package/lib/types/registry.d.ts +6 -0
- package/lib/types/registry.js +19 -0
- package/lib/types/registry.js.map +1 -0
- package/lib/types/symbols.d.ts +29 -0
- package/lib/types/symbols.js +33 -0
- package/lib/types/symbols.js.map +1 -0
- package/lib/types/utils.d.ts +0 -8
- package/lib/types/utils.js +1 -33
- package/lib/types/utils.js.map +1 -1
- package/lib/usage.d.ts +1 -0
- package/lib/usage.js +22 -0
- package/lib/usage.js.map +1 -0
- package/lib/utils.d.ts +13 -2
- package/lib/utils.js +36 -15
- package/lib/utils.js.map +1 -1
- package/lib/v3.d.ts +1 -0
- package/lib/v3.js +427 -0
- package/lib/v3.js.map +1 -0
- package/lib/v3_bench.d.ts +1 -0
- package/lib/v3_bench.js +130 -0
- package/lib/v3_bench.js.map +1 -0
- package/lib/v3_experiment.d.ts +1 -0
- package/lib/v3_experiment.js +407 -0
- package/lib/v3_experiment.js.map +1 -0
- package/package.json +5 -5
- package/src/Metadata.ts +135 -0
- package/src/Reflection.ts +75 -66
- package/src/Schema.ts +213 -931
- package/src/annotations.ts +430 -243
- package/src/decoder/DecodeOperation.ts +372 -0
- package/src/decoder/Decoder.ts +155 -0
- package/src/decoder/ReferenceTracker.ts +151 -0
- package/src/decoder/strategy/RawChanges.ts +9 -0
- package/src/decoder/strategy/StateCallbacks.ts +326 -0
- package/src/encoder/ChangeTree.ts +492 -0
- package/src/encoder/EncodeOperation.ts +237 -0
- package/src/encoder/Encoder.ts +246 -0
- package/src/encoder/StateView.ts +229 -0
- package/src/encoding/assert.ts +58 -0
- package/src/encoding/decode.ts +1 -1
- package/src/encoding/encode.ts +88 -82
- package/src/encoding/spec.ts +29 -0
- package/src/index.ts +22 -19
- package/src/symbol.shim.ts +12 -0
- package/src/types/HelperTypes.ts +16 -2
- package/src/types/{ArraySchema.ts → custom/ArraySchema.ts} +342 -248
- package/src/types/{CollectionSchema.ts → custom/CollectionSchema.ts} +56 -46
- package/src/types/{MapSchema.ts → custom/MapSchema.ts} +88 -115
- package/src/types/{SetSchema.ts → custom/SetSchema.ts} +58 -47
- package/src/types/{typeRegistry.ts → registry.ts} +6 -6
- package/src/types/symbols.ts +36 -0
- package/src/types/utils.ts +0 -46
- package/src/utils.ts +50 -21
- package/src/v3_bench.ts +107 -0
- package/src/changes/ChangeTree.ts +0 -295
- package/src/changes/ReferenceTracker.ts +0 -91
- package/src/filters/index.ts +0 -23
- package/src/spec.ts +0 -49
package/lib/Reflection.js
CHANGED
|
@@ -5,16 +5,14 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
|
|
|
5
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
6
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
7
|
};
|
|
8
|
-
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
-
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
-
};
|
|
11
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
9
|
exports.Reflection = exports.ReflectionType = exports.ReflectionField = void 0;
|
|
13
10
|
const annotations_1 = require("./annotations");
|
|
11
|
+
const Metadata_1 = require("./Metadata");
|
|
12
|
+
const ArraySchema_1 = require("./types/custom/ArraySchema");
|
|
13
|
+
const Encoder_1 = require("./encoder/Encoder");
|
|
14
|
+
const Decoder_1 = require("./decoder/Decoder");
|
|
14
15
|
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() };
|
|
18
16
|
/**
|
|
19
17
|
* Reflection
|
|
20
18
|
*/
|
|
@@ -22,16 +20,13 @@ class ReflectionField extends Schema_1.Schema {
|
|
|
22
20
|
}
|
|
23
21
|
exports.ReflectionField = ReflectionField;
|
|
24
22
|
__decorate([
|
|
25
|
-
(0, annotations_1.type)("string"
|
|
26
|
-
__metadata("design:type", String)
|
|
23
|
+
(0, annotations_1.type)("string")
|
|
27
24
|
], ReflectionField.prototype, "name", void 0);
|
|
28
25
|
__decorate([
|
|
29
|
-
(0, annotations_1.type)("string"
|
|
30
|
-
__metadata("design:type", String)
|
|
26
|
+
(0, annotations_1.type)("string")
|
|
31
27
|
], ReflectionField.prototype, "type", void 0);
|
|
32
28
|
__decorate([
|
|
33
|
-
(0, annotations_1.type)("number"
|
|
34
|
-
__metadata("design:type", Number)
|
|
29
|
+
(0, annotations_1.type)("number")
|
|
35
30
|
], ReflectionField.prototype, "referencedType", void 0);
|
|
36
31
|
class ReflectionType extends Schema_1.Schema {
|
|
37
32
|
constructor() {
|
|
@@ -41,39 +36,46 @@ class ReflectionType extends Schema_1.Schema {
|
|
|
41
36
|
}
|
|
42
37
|
exports.ReflectionType = ReflectionType;
|
|
43
38
|
__decorate([
|
|
44
|
-
(0, annotations_1.type)("number"
|
|
45
|
-
__metadata("design:type", Number)
|
|
39
|
+
(0, annotations_1.type)("number")
|
|
46
40
|
], ReflectionType.prototype, "id", void 0);
|
|
47
41
|
__decorate([
|
|
48
|
-
(0, annotations_1.type)(
|
|
49
|
-
|
|
42
|
+
(0, annotations_1.type)("number")
|
|
43
|
+
], ReflectionType.prototype, "extendsId", void 0);
|
|
44
|
+
__decorate([
|
|
45
|
+
(0, annotations_1.type)([ReflectionField])
|
|
50
46
|
], ReflectionType.prototype, "fields", void 0);
|
|
51
47
|
class Reflection extends Schema_1.Schema {
|
|
52
48
|
constructor() {
|
|
53
49
|
super(...arguments);
|
|
54
50
|
this.types = new ArraySchema_1.ArraySchema();
|
|
55
51
|
}
|
|
56
|
-
static encode(instance) {
|
|
57
|
-
|
|
52
|
+
static encode(instance, context) {
|
|
53
|
+
if (!context) {
|
|
54
|
+
context = new annotations_1.TypeContext(instance.constructor);
|
|
55
|
+
}
|
|
58
56
|
const reflection = new Reflection();
|
|
59
|
-
|
|
60
|
-
const buildType = (currentType,
|
|
61
|
-
for (
|
|
57
|
+
const encoder = new Encoder_1.Encoder(reflection);
|
|
58
|
+
const buildType = (currentType, metadata) => {
|
|
59
|
+
for (const fieldName in metadata) {
|
|
60
|
+
// skip fields from parent classes
|
|
61
|
+
if (!Object.prototype.hasOwnProperty.call(metadata, fieldName)) {
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
62
64
|
const field = new ReflectionField();
|
|
63
65
|
field.name = fieldName;
|
|
64
66
|
let fieldType;
|
|
65
|
-
|
|
66
|
-
|
|
67
|
+
const type = metadata[fieldName].type;
|
|
68
|
+
if (typeof (type) === "string") {
|
|
69
|
+
fieldType = type;
|
|
67
70
|
}
|
|
68
71
|
else {
|
|
69
|
-
const type = schema[fieldName];
|
|
70
72
|
let childTypeSchema;
|
|
71
73
|
//
|
|
72
74
|
// TODO: refactor below.
|
|
73
75
|
//
|
|
74
76
|
if (Schema_1.Schema.is(type)) {
|
|
75
77
|
fieldType = "ref";
|
|
76
|
-
childTypeSchema =
|
|
78
|
+
childTypeSchema = type;
|
|
77
79
|
}
|
|
78
80
|
else {
|
|
79
81
|
fieldType = Object.keys(type)[0];
|
|
@@ -85,7 +87,7 @@ class Reflection extends Schema_1.Schema {
|
|
|
85
87
|
}
|
|
86
88
|
}
|
|
87
89
|
field.referencedType = (childTypeSchema)
|
|
88
|
-
? childTypeSchema
|
|
90
|
+
? context.getTypeId(childTypeSchema)
|
|
89
91
|
: -1;
|
|
90
92
|
}
|
|
91
93
|
field.type = fieldType;
|
|
@@ -93,21 +95,35 @@ class Reflection extends Schema_1.Schema {
|
|
|
93
95
|
}
|
|
94
96
|
reflection.types.push(currentType);
|
|
95
97
|
};
|
|
96
|
-
|
|
97
|
-
|
|
98
|
+
for (let typeid in context.types) {
|
|
99
|
+
const klass = context.types[typeid];
|
|
98
100
|
const type = new ReflectionType();
|
|
99
101
|
type.id = Number(typeid);
|
|
100
|
-
|
|
102
|
+
// support inheritance
|
|
103
|
+
const inheritFrom = Object.getPrototypeOf(klass);
|
|
104
|
+
if (inheritFrom !== Schema_1.Schema) {
|
|
105
|
+
type.extendsId = context.schemas.get(inheritFrom);
|
|
106
|
+
}
|
|
107
|
+
buildType(type, klass[Symbol.metadata]);
|
|
101
108
|
}
|
|
102
|
-
|
|
109
|
+
const it = { offset: 0 };
|
|
110
|
+
const buf = encoder.encodeAll(it);
|
|
111
|
+
return Buffer.from(buf, 0, it.offset);
|
|
103
112
|
}
|
|
104
113
|
static decode(bytes, it) {
|
|
105
|
-
const context = new annotations_1.Context();
|
|
106
114
|
const reflection = new Reflection();
|
|
107
|
-
|
|
115
|
+
const reflectionDecoder = new Decoder_1.Decoder(reflection);
|
|
116
|
+
reflectionDecoder.decode(bytes, it);
|
|
117
|
+
const context = new annotations_1.TypeContext();
|
|
108
118
|
const schemaTypes = reflection.types.reduce((types, reflectionType) => {
|
|
109
|
-
const
|
|
119
|
+
const parentKlass = types[reflectionType.extendsId] || Schema_1.Schema;
|
|
120
|
+
const schema = class _ extends parentKlass {
|
|
110
121
|
};
|
|
122
|
+
// const _metadata = Object.create(_classSuper[Symbol.metadata] ?? null);
|
|
123
|
+
const _metadata = parentKlass && parentKlass[Symbol.metadata] || Object.create(null);
|
|
124
|
+
Object.defineProperty(schema, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
|
125
|
+
// register for inheritance support
|
|
126
|
+
annotations_1.TypeContext.register(schema);
|
|
111
127
|
const typeid = reflectionType.id;
|
|
112
128
|
types[typeid] = schema;
|
|
113
129
|
context.add(schema, typeid);
|
|
@@ -115,7 +131,11 @@ class Reflection extends Schema_1.Schema {
|
|
|
115
131
|
}, {});
|
|
116
132
|
reflection.types.forEach((reflectionType) => {
|
|
117
133
|
const schemaType = schemaTypes[reflectionType.id];
|
|
118
|
-
|
|
134
|
+
const metadata = schemaType[Symbol.metadata];
|
|
135
|
+
const parentKlass = reflection.types[reflectionType.extendsId];
|
|
136
|
+
const parentFieldIndex = parentKlass && parentKlass.fields.length || 0;
|
|
137
|
+
reflectionType.fields.forEach((field, i) => {
|
|
138
|
+
const fieldIndex = parentFieldIndex + i;
|
|
119
139
|
if (field.referencedType !== undefined) {
|
|
120
140
|
let fieldType = field.type;
|
|
121
141
|
let refType = schemaTypes[field.referencedType];
|
|
@@ -126,41 +146,25 @@ class Reflection extends Schema_1.Schema {
|
|
|
126
146
|
refType = typeInfo[1];
|
|
127
147
|
}
|
|
128
148
|
if (fieldType === "ref") {
|
|
129
|
-
|
|
149
|
+
// type(refType)(schemaType.prototype, field.name);
|
|
150
|
+
Metadata_1.Metadata.addField(metadata, fieldIndex, field.name, refType);
|
|
130
151
|
}
|
|
131
152
|
else {
|
|
132
|
-
|
|
153
|
+
// type({ [fieldType]: refType } as DefinitionType)(schemaType.prototype, field.name);
|
|
154
|
+
Metadata_1.Metadata.addField(metadata, fieldIndex, field.name, { [fieldType]: refType });
|
|
133
155
|
}
|
|
134
156
|
}
|
|
135
157
|
else {
|
|
136
|
-
|
|
158
|
+
// type(field.type as PrimitiveType)(schemaType.prototype, field.name);
|
|
159
|
+
Metadata_1.Metadata.addField(metadata, fieldIndex, field.name, field.type);
|
|
137
160
|
}
|
|
138
161
|
});
|
|
139
162
|
});
|
|
140
|
-
|
|
141
|
-
const rootInstance = new rootType();
|
|
142
|
-
/**
|
|
143
|
-
* auto-initialize referenced types on root type
|
|
144
|
-
* to allow registering listeners immediatelly on client-side
|
|
145
|
-
*/
|
|
146
|
-
for (let fieldName in rootType._definition.schema) {
|
|
147
|
-
const fieldType = rootType._definition.schema[fieldName];
|
|
148
|
-
if (typeof (fieldType) !== "string") {
|
|
149
|
-
rootInstance[fieldName] = (typeof (fieldType) === "function")
|
|
150
|
-
? new fieldType() // is a schema reference
|
|
151
|
-
: new ((0, typeRegistry_1.getType)(Object.keys(fieldType)[0])).constructor(); // is a "collection"
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
return rootInstance;
|
|
163
|
+
return new (schemaTypes[0])();
|
|
155
164
|
}
|
|
156
165
|
}
|
|
157
166
|
exports.Reflection = Reflection;
|
|
158
167
|
__decorate([
|
|
159
|
-
(0, annotations_1.type)([ReflectionType]
|
|
160
|
-
__metadata("design:type", ArraySchema_1.ArraySchema)
|
|
168
|
+
(0, annotations_1.type)([ReflectionType])
|
|
161
169
|
], Reflection.prototype, "types", void 0);
|
|
162
|
-
__decorate([
|
|
163
|
-
(0, annotations_1.type)("number", reflectionContext),
|
|
164
|
-
__metadata("design:type", Number)
|
|
165
|
-
], Reflection.prototype, "rootType", void 0);
|
|
166
170
|
//# sourceMappingURL=Reflection.js.map
|
package/lib/Reflection.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
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,OAAO,EAAE,IAAI,qBAAO,EAAE,EAAE,CAAC;AAErD;;GAEG;AACH,MAAa,eAAgB,SAAQ,eAAM;CAS1C;AATD,0CASC;AAPG;IADC,IAAA,kBAAI,EAAC,QAAQ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;IACxB,CAAC;CACJ;AAhID,gCAgIC;AA9HG;IADC,IAAA,kBAAI,EAAC,CAAE,cAAc,CAAE,EAAE,iBAAiB,CAAC;8BACrC,yBAAW;yCAAqD;AAGvE;IADC,IAAA,kBAAI,EAAC,QAAQ,EAAE,iBAAiB,CAAC;;4CACjB","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}\n"]}
|
|
1
|
+
{"version":3,"file":"Reflection.js","sourceRoot":"","sources":["../src/Refl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C;oBACrC,IAAI,SAAS,GAAG,KAAK,CAAC,IAAI,CAAC;oBAC3B,IAAI,OAAO,GAAG,WAAW,CAAC,KAAK,CAAC,cAAc,CAAC,CAAC;oBAEhD,sCAAsC;oBACtC,IAAI,CAAC,OAAO,EAAE,CAAC;wBACX,MAAM,QAAQ,GAAG,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;wBACvC,SAAS,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAC;wBACxB,OAAO,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAC;oBAC1B,CAAC;oBAED,IAAI,SAAS,KAAK,KAAK,EAAE,CAAC;wBACtB,mDAAmD;wBACnD,mBAAQ,CAAC,QAAQ,CAAC,QAAQ,EAAE,UAAU,EAAE,KAAK,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;oBAEjE,CAAC;yBAAM,CAAC;wBACJ,sFAAsF;wBACtF,mBAAQ,CAAC,QAAQ,CAAC,QAAQ,EAAE,UAAU,EAAE,KAAK,CAAC,IAAI,EAAE,EAAE,CAAC,SAAS,CAAC,EAAE,OAAO,EAAoB,CAAC,CAAC;oBACpG,CAAC;gBAEL,CAAC;qBAAM,CAAC;oBACJ,uEAAuE;oBACvE,mBAAQ,CAAC,QAAQ,CAAC,QAAQ,EAAE,UAAU,EAAE,KAAK,CAAC,IAAI,EAAE,KAAK,CAAC,IAAqB,CAAC,CAAC;gBACrF,CAAC;YACL,CAAC,CAAC,CAAC;QACP,CAAC,CAAC,CAAC;QAEH,OAAO,IAAI,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;IAClC,CAAC;CACJ;AAhJD,gCAgJC;AA/I6B;IAAzB,IAAA,kBAAI,EAAC,CAAE,cAAc,CAAE,CAAC;yCAAwE","sourcesContent":["import { type, PrimitiveType, DefinitionType, TypeContext } from \"./annotations\";\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\n static encode (instance: Schema, context?: TypeContext) {\n if (!context) {\n context = new TypeContext(instance.constructor as typeof Schema);\n }\n\n const reflection = new Reflection();\n const encoder = new Encoder(reflection);\n\n const buildType = (currentType: ReflectionType, metadata: Metadata) => {\n for (const fieldName in metadata) {\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[fieldName].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 it = { offset: 0 };\n const buf = encoder.encodeAll(it);\n return Buffer.from(buf, 0, it.offset);\n }\n\n static decode<T extends Schema = Schema>(bytes: Buffer, it?: Iterator): T {\n const reflection = new Reflection();\n\n const reflectionDecoder = new Decoder(reflection);\n reflectionDecoder.decode(bytes, it);\n\n const context = new TypeContext();\n\n const schemaTypes = reflection.types.reduce((types, reflectionType) => {\n const parentKlass: typeof Schema = types[reflectionType.extendsId] || Schema;\n const schema: typeof Schema = class _ extends parentKlass {};\n\n // const _metadata = Object.create(_classSuper[Symbol.metadata] ?? null);\n const _metadata = parentKlass && parentKlass[Symbol.metadata] || Object.create(null);\n Object.defineProperty(schema, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata })\n\n // register for inheritance support\n TypeContext.register(schema);\n\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 const metadata = schemaType[Symbol.metadata];\n\n const parentKlass = reflection.types[reflectionType.extendsId];\n const parentFieldIndex = parentKlass && parentKlass.fields.length || 0;\n\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 = 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)(schemaType.prototype, field.name);\n Metadata.addField(metadata, fieldIndex, field.name, refType);\n\n } else {\n // type({ [fieldType]: refType } as DefinitionType)(schemaType.prototype, field.name);\n Metadata.addField(metadata, fieldIndex, field.name, { [fieldType]: refType } as DefinitionType);\n }\n\n } else {\n // type(field.type as PrimitiveType)(schemaType.prototype, field.name);\n Metadata.addField(metadata, fieldIndex, field.name, field.type as PrimitiveType);\n }\n });\n });\n\n return new (schemaTypes[0])();\n }\n}\n"]}
|
package/lib/Schema.d.ts
CHANGED
|
@@ -1,47 +1,39 @@
|
|
|
1
|
-
import { OPERATION } from './spec';
|
|
2
|
-
import {
|
|
3
|
-
import type { Iterator } from "./encoding/decode";
|
|
4
|
-
import { ChangeTree, Ref } from "./changes/ChangeTree";
|
|
1
|
+
import { OPERATION } from './encoding/spec';
|
|
2
|
+
import { DefinitionType } from "./annotations";
|
|
5
3
|
import { NonFunctionPropNames, ToJSON } from './types/HelperTypes';
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
field: F;
|
|
10
|
-
dynamicIndex?: number | string;
|
|
11
|
-
value: T;
|
|
12
|
-
previousValue: T;
|
|
13
|
-
}
|
|
14
|
-
export interface SchemaDecoderCallbacks<TValue = any, TKey = any> {
|
|
15
|
-
$callbacks: {
|
|
16
|
-
[operation: number]: Array<(item: TValue, key: TKey) => void>;
|
|
17
|
-
};
|
|
18
|
-
onAdd(callback: (item: any, key: any) => void, ignoreExisting?: boolean): () => void;
|
|
19
|
-
onRemove(callback: (item: any, key: any) => void): () => void;
|
|
20
|
-
onChange(callback: (item: any, key: any) => void): () => void;
|
|
21
|
-
clone(decoding?: boolean): SchemaDecoderCallbacks;
|
|
22
|
-
clear(changes?: DataChange[]): any;
|
|
23
|
-
decode?(byte: any, it: Iterator): any;
|
|
24
|
-
}
|
|
4
|
+
import { ChangeTree, Ref } from './encoder/ChangeTree';
|
|
5
|
+
import { $decoder, $deleteByIndex, $encoder, $filter, $getByIndex, $track } from './types/symbols';
|
|
6
|
+
import { StateView } from './encoder/StateView';
|
|
25
7
|
/**
|
|
26
8
|
* Schema encoder / decoder
|
|
27
9
|
*/
|
|
28
10
|
export declare abstract class Schema {
|
|
29
|
-
static
|
|
30
|
-
static
|
|
31
|
-
|
|
32
|
-
|
|
11
|
+
static [$encoder]: import("./encoder/EncodeOperation").EncodeOperation<any>;
|
|
12
|
+
static [$decoder]: import("./decoder/DecodeOperation").DecodeOperation<any>;
|
|
13
|
+
/**
|
|
14
|
+
* Assign the property descriptors required to track changes on this instance.
|
|
15
|
+
* @param instance
|
|
16
|
+
*/
|
|
17
|
+
static initialize(instance: any): void;
|
|
33
18
|
static is(type: DefinitionType): boolean;
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
19
|
+
/**
|
|
20
|
+
* Track property changes
|
|
21
|
+
*/
|
|
22
|
+
static [$track](changeTree: ChangeTree, index: number, operation?: OPERATION): void;
|
|
23
|
+
/**
|
|
24
|
+
* Determine if a property must be filtered.
|
|
25
|
+
* - If returns false, the property is NOT going to be encoded.
|
|
26
|
+
* - If returns true, the property is going to be encoded.
|
|
27
|
+
*
|
|
28
|
+
* Encoding with "filters" happens in two steps:
|
|
29
|
+
* - First, the encoder iterates over all "not owned" properties and encodes them.
|
|
30
|
+
* - Then, the encoder iterates over all "owned" properties per instance and encodes them.
|
|
31
|
+
*/
|
|
32
|
+
static [$filter](ref: Schema, index: number, view: StateView): boolean;
|
|
40
33
|
constructor(...args: any[]);
|
|
41
34
|
assign(props: {
|
|
42
35
|
[prop in NonFunctionPropNames<this>]?: this[prop];
|
|
43
36
|
} | ToJSON<this>): this;
|
|
44
|
-
protected get _definition(): SchemaDefinition;
|
|
45
37
|
/**
|
|
46
38
|
* (Server-side): Flag a property to be encoded for the next patch.
|
|
47
39
|
* @param instance Schema instance
|
|
@@ -49,24 +41,20 @@ export declare abstract class Schema {
|
|
|
49
41
|
* @param operation OPERATION to perform (detected automatically)
|
|
50
42
|
*/
|
|
51
43
|
setDirty<K extends NonFunctionPropNames<this>>(property: K | number, operation?: OPERATION): void;
|
|
52
|
-
/**
|
|
53
|
-
* Client-side: listen for changes on property.
|
|
54
|
-
* @param prop the property name
|
|
55
|
-
* @param callback callback to be triggered on property change
|
|
56
|
-
* @param immediate trigger immediatelly if property has been already set.
|
|
57
|
-
*/
|
|
58
|
-
listen<K extends NonFunctionPropNames<this>>(prop: K, callback: (value: this[K], previousValue: this[K]) => void, immediate?: boolean): () => boolean;
|
|
59
|
-
decode(bytes: number[], it?: Iterator, ref?: Ref): DataChange<any, string>[];
|
|
60
|
-
encode(encodeAll?: boolean, bytes?: number[], useFilters?: boolean): number[];
|
|
61
|
-
encodeAll(useFilters?: boolean): number[];
|
|
62
|
-
applyFilters(client: ClientWithSessionId, encodeAll?: boolean): number[];
|
|
63
44
|
clone(): this;
|
|
64
45
|
toJSON(): ToJSON<this>;
|
|
65
46
|
discardAllChanges(): void;
|
|
66
|
-
protected getByIndex(index: number): any;
|
|
67
|
-
protected deleteByIndex(index: number): void;
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
47
|
+
protected [$getByIndex](index: number): any;
|
|
48
|
+
protected [$deleteByIndex](index: number): void;
|
|
49
|
+
static debugRefIds(instance: Ref, jsonContents?: boolean, level?: number): string;
|
|
50
|
+
/**
|
|
51
|
+
* Return a string representation of the changes on a Schema instance.
|
|
52
|
+
* The list of changes is cleared after each encode.
|
|
53
|
+
*
|
|
54
|
+
* @param instance Schema instance
|
|
55
|
+
* @param isEncodeAll Return "full encode" instead of current change set.
|
|
56
|
+
* @returns
|
|
57
|
+
*/
|
|
58
|
+
static debugChanges(instance: Ref, isEncodeAll?: boolean): string;
|
|
59
|
+
static debugChangesDeep(ref: Ref): string;
|
|
72
60
|
}
|