@colyseus/schema 2.0.31 → 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.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} +345 -251
- 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/src/Reflection.ts
CHANGED
|
@@ -1,58 +1,55 @@
|
|
|
1
|
-
import { type, PrimitiveType,
|
|
2
|
-
import {
|
|
3
|
-
import { ArraySchema } from "./types/ArraySchema";
|
|
4
|
-
import { getType } from "./types/typeRegistry";
|
|
1
|
+
import { type, PrimitiveType, DefinitionType, TypeContext } from "./annotations";
|
|
2
|
+
import { Metadata } from "./Metadata";
|
|
3
|
+
import { ArraySchema } from "./types/custom/ArraySchema";
|
|
5
4
|
import { Iterator } from "./encoding/decode";
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
import { Encoder } from "./encoder/Encoder";
|
|
6
|
+
import { Decoder } from "./decoder/Decoder";
|
|
7
|
+
import { Schema } from "./Schema";
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
10
|
* Reflection
|
|
11
11
|
*/
|
|
12
12
|
export class ReflectionField extends Schema {
|
|
13
|
-
@type("string"
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
@type("string", reflectionContext)
|
|
17
|
-
type: string;
|
|
18
|
-
|
|
19
|
-
@type("number", reflectionContext)
|
|
20
|
-
referencedType: number;
|
|
13
|
+
@type("string") name: string;
|
|
14
|
+
@type("string") type: string;
|
|
15
|
+
@type("number") referencedType: number;
|
|
21
16
|
}
|
|
22
17
|
|
|
23
18
|
export class ReflectionType extends Schema {
|
|
24
|
-
@type("number"
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
@type([ ReflectionField ], reflectionContext)
|
|
28
|
-
fields: ArraySchema<ReflectionField> = new ArraySchema<ReflectionField>();
|
|
19
|
+
@type("number") id: number;
|
|
20
|
+
@type("number") extendsId: number;
|
|
21
|
+
@type([ ReflectionField ]) fields = new ArraySchema<ReflectionField>();
|
|
29
22
|
}
|
|
30
23
|
|
|
31
24
|
export class Reflection extends Schema {
|
|
32
|
-
@type([ ReflectionType ]
|
|
33
|
-
types: ArraySchema<ReflectionType> = new ArraySchema<ReflectionType>();
|
|
25
|
+
@type([ ReflectionType ]) types: ArraySchema<ReflectionType> = new ArraySchema<ReflectionType>();
|
|
34
26
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
const rootSchemaType = instance.constructor as typeof Schema;
|
|
27
|
+
static encode (instance: Schema, context?: TypeContext) {
|
|
28
|
+
if (!context) {
|
|
29
|
+
context = new TypeContext(instance.constructor as typeof Schema);
|
|
30
|
+
}
|
|
40
31
|
|
|
41
32
|
const reflection = new Reflection();
|
|
42
|
-
|
|
33
|
+
const encoder = new Encoder(reflection);
|
|
34
|
+
|
|
35
|
+
const buildType = (currentType: ReflectionType, metadata: Metadata) => {
|
|
36
|
+
for (const fieldName in metadata) {
|
|
37
|
+
// skip fields from parent classes
|
|
38
|
+
if (!Object.prototype.hasOwnProperty.call(metadata, fieldName)) {
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
43
41
|
|
|
44
|
-
const buildType = (currentType: ReflectionType, schema: any) => {
|
|
45
|
-
for (let fieldName in schema) {
|
|
46
42
|
const field = new ReflectionField();
|
|
47
43
|
field.name = fieldName;
|
|
48
44
|
|
|
49
45
|
let fieldType: string;
|
|
50
46
|
|
|
51
|
-
|
|
52
|
-
|
|
47
|
+
const type = metadata[fieldName].type;
|
|
48
|
+
|
|
49
|
+
if (typeof (type) === "string") {
|
|
50
|
+
fieldType = type;
|
|
53
51
|
|
|
54
52
|
} else {
|
|
55
|
-
const type = schema[fieldName];
|
|
56
53
|
let childTypeSchema: typeof Schema;
|
|
57
54
|
|
|
58
55
|
//
|
|
@@ -60,7 +57,7 @@ export class Reflection extends Schema {
|
|
|
60
57
|
//
|
|
61
58
|
if (Schema.is(type)) {
|
|
62
59
|
fieldType = "ref";
|
|
63
|
-
childTypeSchema =
|
|
60
|
+
childTypeSchema = type as typeof Schema;
|
|
64
61
|
|
|
65
62
|
} else {
|
|
66
63
|
fieldType = Object.keys(type)[0];
|
|
@@ -74,7 +71,7 @@ export class Reflection extends Schema {
|
|
|
74
71
|
}
|
|
75
72
|
|
|
76
73
|
field.referencedType = (childTypeSchema)
|
|
77
|
-
? childTypeSchema
|
|
74
|
+
? context.getTypeId(childTypeSchema)
|
|
78
75
|
: -1;
|
|
79
76
|
}
|
|
80
77
|
|
|
@@ -85,24 +82,44 @@ export class Reflection extends Schema {
|
|
|
85
82
|
reflection.types.push(currentType);
|
|
86
83
|
}
|
|
87
84
|
|
|
88
|
-
|
|
89
|
-
|
|
85
|
+
for (let typeid in context.types) {
|
|
86
|
+
const klass = context.types[typeid];
|
|
90
87
|
const type = new ReflectionType();
|
|
91
88
|
type.id = Number(typeid);
|
|
92
|
-
|
|
89
|
+
|
|
90
|
+
// support inheritance
|
|
91
|
+
const inheritFrom = Object.getPrototypeOf(klass);
|
|
92
|
+
if (inheritFrom !== Schema) {
|
|
93
|
+
type.extendsId = context.schemas.get(inheritFrom);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
buildType(type, klass[Symbol.metadata]);
|
|
93
97
|
}
|
|
94
98
|
|
|
95
|
-
|
|
99
|
+
const it = { offset: 0 };
|
|
100
|
+
const buf = encoder.encodeAll(it);
|
|
101
|
+
return Buffer.from(buf, 0, it.offset);
|
|
96
102
|
}
|
|
97
103
|
|
|
98
|
-
static decode<T extends Schema = Schema>(bytes:
|
|
99
|
-
const context = new Context();
|
|
100
|
-
|
|
104
|
+
static decode<T extends Schema = Schema>(bytes: Buffer, it?: Iterator): T {
|
|
101
105
|
const reflection = new Reflection();
|
|
102
|
-
|
|
106
|
+
|
|
107
|
+
const reflectionDecoder = new Decoder(reflection);
|
|
108
|
+
reflectionDecoder.decode(bytes, it);
|
|
109
|
+
|
|
110
|
+
const context = new TypeContext();
|
|
103
111
|
|
|
104
112
|
const schemaTypes = reflection.types.reduce((types, reflectionType) => {
|
|
105
|
-
const
|
|
113
|
+
const parentKlass: typeof Schema = types[reflectionType.extendsId] || Schema;
|
|
114
|
+
const schema: typeof Schema = class _ extends parentKlass {};
|
|
115
|
+
|
|
116
|
+
// const _metadata = Object.create(_classSuper[Symbol.metadata] ?? null);
|
|
117
|
+
const _metadata = parentKlass && parentKlass[Symbol.metadata] || Object.create(null);
|
|
118
|
+
Object.defineProperty(schema, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata })
|
|
119
|
+
|
|
120
|
+
// register for inheritance support
|
|
121
|
+
TypeContext.register(schema);
|
|
122
|
+
|
|
106
123
|
const typeid = reflectionType.id;
|
|
107
124
|
types[typeid] = schema
|
|
108
125
|
context.add(schema, typeid);
|
|
@@ -111,8 +128,14 @@ export class Reflection extends Schema {
|
|
|
111
128
|
|
|
112
129
|
reflection.types.forEach((reflectionType) => {
|
|
113
130
|
const schemaType = schemaTypes[reflectionType.id];
|
|
131
|
+
const metadata = schemaType[Symbol.metadata];
|
|
132
|
+
|
|
133
|
+
const parentKlass = reflection.types[reflectionType.extendsId];
|
|
134
|
+
const parentFieldIndex = parentKlass && parentKlass.fields.length || 0;
|
|
135
|
+
|
|
136
|
+
reflectionType.fields.forEach((field, i) => {
|
|
137
|
+
const fieldIndex = parentFieldIndex + i;
|
|
114
138
|
|
|
115
|
-
reflectionType.fields.forEach(field => {
|
|
116
139
|
if (field.referencedType !== undefined) {
|
|
117
140
|
let fieldType = field.type;
|
|
118
141
|
let refType = schemaTypes[field.referencedType];
|
|
@@ -125,35 +148,21 @@ export class Reflection extends Schema {
|
|
|
125
148
|
}
|
|
126
149
|
|
|
127
150
|
if (fieldType === "ref") {
|
|
128
|
-
type(refType
|
|
151
|
+
// type(refType)(schemaType.prototype, field.name);
|
|
152
|
+
Metadata.addField(metadata, fieldIndex, field.name, refType);
|
|
129
153
|
|
|
130
154
|
} else {
|
|
131
|
-
type({ [fieldType]: refType } as DefinitionType
|
|
155
|
+
// type({ [fieldType]: refType } as DefinitionType)(schemaType.prototype, field.name);
|
|
156
|
+
Metadata.addField(metadata, fieldIndex, field.name, { [fieldType]: refType } as DefinitionType);
|
|
132
157
|
}
|
|
133
158
|
|
|
134
159
|
} else {
|
|
135
|
-
type(field.type as PrimitiveType
|
|
160
|
+
// type(field.type as PrimitiveType)(schemaType.prototype, field.name);
|
|
161
|
+
Metadata.addField(metadata, fieldIndex, field.name, field.type as PrimitiveType);
|
|
136
162
|
}
|
|
137
163
|
});
|
|
138
|
-
})
|
|
139
|
-
|
|
140
|
-
const rootType: any = schemaTypes[reflection.rootType];
|
|
141
|
-
const rootInstance = new rootType();
|
|
142
|
-
|
|
143
|
-
/**
|
|
144
|
-
* auto-initialize referenced types on root type
|
|
145
|
-
* to allow registering listeners immediatelly on client-side
|
|
146
|
-
*/
|
|
147
|
-
for (let fieldName in rootType._definition.schema) {
|
|
148
|
-
const fieldType = rootType._definition.schema[fieldName];
|
|
149
|
-
|
|
150
|
-
if (typeof(fieldType) !== "string") {
|
|
151
|
-
rootInstance[fieldName] = (typeof (fieldType) === "function")
|
|
152
|
-
? new (fieldType as any)() // is a schema reference
|
|
153
|
-
: new (getType(Object.keys(fieldType)[0])).constructor(); // is a "collection"
|
|
154
|
-
}
|
|
155
|
-
}
|
|
164
|
+
});
|
|
156
165
|
|
|
157
|
-
return
|
|
166
|
+
return new (schemaTypes[0])();
|
|
158
167
|
}
|
|
159
168
|
}
|