@colyseus/schema 3.0.0-alpha.27 → 3.0.0-alpha.29
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 +114 -88
- package/build/cjs/index.js.map +1 -1
- package/build/esm/index.mjs +114 -88
- package/build/esm/index.mjs.map +1 -1
- package/build/umd/index.js +114 -88
- package/lib/Metadata.d.ts +3 -0
- package/lib/Metadata.js.map +1 -1
- package/lib/Reflection.d.ts +1 -1
- package/lib/Reflection.js +4 -3
- package/lib/Reflection.js.map +1 -1
- package/lib/annotations.d.ts +0 -19
- package/lib/annotations.js +4 -106
- package/lib/annotations.js.map +1 -1
- package/lib/bench_encode.js +54 -27
- package/lib/bench_encode.js.map +1 -1
- package/lib/decoder/Decoder.d.ts +1 -1
- package/lib/decoder/Decoder.js +2 -2
- package/lib/decoder/Decoder.js.map +1 -1
- package/lib/decoder/strategy/StateCallbacks.js +5 -4
- package/lib/decoder/strategy/StateCallbacks.js.map +1 -1
- package/lib/encoder/ChangeTree.d.ts +1 -12
- package/lib/encoder/ChangeTree.js +24 -51
- package/lib/encoder/ChangeTree.js.map +1 -1
- package/lib/encoder/Encoder.d.ts +6 -5
- package/lib/encoder/Encoder.js +28 -19
- package/lib/encoder/Encoder.js.map +1 -1
- package/lib/encoder/Root.d.ts +17 -0
- package/lib/encoder/Root.js +44 -0
- package/lib/encoder/Root.js.map +1 -0
- package/lib/index.d.ts +2 -1
- package/lib/index.js +3 -3
- package/lib/index.js.map +1 -1
- package/lib/types/TypeContext.d.ts +23 -0
- package/lib/types/TypeContext.js +109 -0
- package/lib/types/TypeContext.js.map +1 -0
- package/lib/types/custom/ArraySchema.js +0 -1
- package/lib/types/custom/ArraySchema.js.map +1 -1
- package/package.json +1 -1
- package/src/Metadata.ts +1 -0
- package/src/Reflection.ts +2 -1
- package/src/annotations.ts +1 -126
- package/src/bench_encode.ts +47 -16
- package/src/decoder/Decoder.ts +1 -1
- package/src/decoder/strategy/StateCallbacks.ts +5 -4
- package/src/encoder/ChangeTree.ts +30 -59
- package/src/encoder/Encoder.ts +36 -23
- package/src/encoder/Root.ts +51 -0
- package/src/index.ts +3 -11
- package/src/types/TypeContext.ts +127 -0
- package/src/types/custom/ArraySchema.ts +0 -1
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { Metadata } from "../Metadata";
|
|
2
|
+
import { Schema } from "../Schema";
|
|
3
|
+
|
|
4
|
+
export class TypeContext {
|
|
5
|
+
types: { [id: number]: typeof Schema; } = {};
|
|
6
|
+
schemas = new Map<typeof Schema, number>();
|
|
7
|
+
|
|
8
|
+
hasFilters: boolean = false;
|
|
9
|
+
parentFiltered: {[typeIdAndParentIndex: string]: boolean} = {};
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* For inheritance support
|
|
13
|
+
* Keeps track of which classes extends which. (parent -> children)
|
|
14
|
+
*/
|
|
15
|
+
static inheritedTypes = new Map<typeof Schema, Set<typeof Schema>>();
|
|
16
|
+
|
|
17
|
+
static register(target: typeof Schema) {
|
|
18
|
+
const parent = Object.getPrototypeOf(target);
|
|
19
|
+
if (parent !== Schema) {
|
|
20
|
+
let inherits = TypeContext.inheritedTypes.get(parent);
|
|
21
|
+
if (!inherits) {
|
|
22
|
+
inherits = new Set<typeof Schema>();
|
|
23
|
+
TypeContext.inheritedTypes.set(parent, inherits);
|
|
24
|
+
}
|
|
25
|
+
inherits.add(target);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
constructor(rootClass?: typeof Schema) {
|
|
30
|
+
if (rootClass) {
|
|
31
|
+
this.discoverTypes(rootClass);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
has(schema: typeof Schema) {
|
|
36
|
+
return this.schemas.has(schema);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
get(typeid: number) {
|
|
40
|
+
return this.types[typeid];
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
add(schema: typeof Schema, typeid: number = this.schemas.size) {
|
|
44
|
+
// skip if already registered
|
|
45
|
+
if (this.schemas.has(schema)) {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
this.types[typeid] = schema;
|
|
50
|
+
|
|
51
|
+
//
|
|
52
|
+
// Workaround to allow using an empty Schema (with no `@type()` fields)
|
|
53
|
+
//
|
|
54
|
+
if (schema[Symbol.metadata] === undefined) {
|
|
55
|
+
Metadata.init(schema);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
this.schemas.set(schema, typeid);
|
|
59
|
+
return true;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
getTypeId(klass: typeof Schema) {
|
|
63
|
+
return this.schemas.get(klass);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
private discoverTypes(klass: typeof Schema, parentIndex?: number, parentFieldViewTag?: number) {
|
|
67
|
+
if (!this.add(klass)) {
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// add classes inherited from this base class
|
|
72
|
+
TypeContext.inheritedTypes.get(klass)?.forEach((child) => {
|
|
73
|
+
this.discoverTypes(child, parentIndex, parentFieldViewTag);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
const metadata: Metadata = (klass[Symbol.metadata] ??= {});
|
|
77
|
+
|
|
78
|
+
// if any schema/field has filters, mark "context" as having filters.
|
|
79
|
+
if (metadata[-2]) {
|
|
80
|
+
this.hasFilters = true;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (parentFieldViewTag !== undefined) {
|
|
84
|
+
this.parentFiltered[`${this.schemas.get(klass)}-${parentIndex}`] = true;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
for (const field in metadata) {
|
|
88
|
+
// //
|
|
89
|
+
// // Modify the field's metadata to include the parent field's view tag
|
|
90
|
+
// //
|
|
91
|
+
// if (
|
|
92
|
+
// parentFieldViewTag !== undefined &&
|
|
93
|
+
// metadata[field].tag === undefined
|
|
94
|
+
// ) {
|
|
95
|
+
// metadata[field].tag = parentFieldViewTag;
|
|
96
|
+
// }
|
|
97
|
+
|
|
98
|
+
const fieldType = metadata[field].type;
|
|
99
|
+
const viewTag = metadata[field].tag;
|
|
100
|
+
|
|
101
|
+
if (typeof (fieldType) === "string") {
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (Array.isArray(fieldType)) {
|
|
106
|
+
const type = fieldType[0];
|
|
107
|
+
if (type === "string") {
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
this.discoverTypes(type as typeof Schema, metadata[field].index, viewTag);
|
|
111
|
+
|
|
112
|
+
} else if (typeof (fieldType) === "function") {
|
|
113
|
+
this.discoverTypes(fieldType as typeof Schema, viewTag);
|
|
114
|
+
|
|
115
|
+
} else {
|
|
116
|
+
const type = Object.values(fieldType)[0];
|
|
117
|
+
|
|
118
|
+
// skip primitive types
|
|
119
|
+
if (typeof (type) === "string") {
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
this.discoverTypes(type as typeof Schema, metadata[field].index, viewTag);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
@@ -163,7 +163,6 @@ export class ArraySchema<V = any> implements Array<V>, Collection<number, V> {
|
|
|
163
163
|
|
|
164
164
|
const changeTree = this[$changes];
|
|
165
165
|
changeTree.indexedOperation(length, OPERATION.ADD, this.items.length);
|
|
166
|
-
// changeTree.indexes[length] = length;
|
|
167
166
|
|
|
168
167
|
this.items.push(value);
|
|
169
168
|
this.tmpItems.push(value);
|