@colyseus/schema 3.0.0-alpha.3 → 3.0.0-alpha.30
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/README.md +131 -61
- package/build/cjs/index.js +644 -283
- package/build/cjs/index.js.map +1 -1
- package/build/esm/index.mjs +643 -282
- package/build/esm/index.mjs.map +1 -1
- package/build/umd/index.js +644 -283
- package/lib/Metadata.d.ts +2 -0
- package/lib/Metadata.js +39 -0
- package/lib/Metadata.js.map +1 -1
- package/lib/Reflection.d.ts +2 -3
- package/lib/Reflection.js +31 -26
- package/lib/Reflection.js.map +1 -1
- package/lib/Schema.d.ts +2 -2
- package/lib/Schema.js +2 -2
- package/lib/Schema.js.map +1 -1
- package/lib/annotations.d.ts +0 -19
- package/lib/annotations.js +15 -101
- package/lib/annotations.js.map +1 -1
- package/lib/bench_encode.d.ts +1 -0
- package/lib/bench_encode.js +120 -0
- package/lib/bench_encode.js.map +1 -0
- package/lib/codegen/api.js +1 -2
- package/lib/codegen/api.js.map +1 -1
- package/lib/codegen/languages/cpp.js +1 -2
- package/lib/codegen/languages/cpp.js.map +1 -1
- package/lib/codegen/languages/csharp.js +1 -2
- package/lib/codegen/languages/csharp.js.map +1 -1
- package/lib/codegen/languages/haxe.js +1 -2
- package/lib/codegen/languages/haxe.js.map +1 -1
- package/lib/codegen/languages/java.js +1 -2
- package/lib/codegen/languages/java.js.map +1 -1
- package/lib/codegen/languages/js.js +1 -2
- package/lib/codegen/languages/js.js.map +1 -1
- package/lib/codegen/languages/lua.js +1 -2
- package/lib/codegen/languages/lua.js.map +1 -1
- package/lib/codegen/languages/ts.js +1 -2
- package/lib/codegen/languages/ts.js.map +1 -1
- package/lib/codegen/parser.js +2 -3
- package/lib/codegen/parser.js.map +1 -1
- package/lib/codegen/types.js +3 -3
- package/lib/codegen/types.js.map +1 -1
- package/lib/debug.d.ts +1 -0
- package/lib/debug.js +52 -0
- package/lib/debug.js.map +1 -0
- package/lib/decoder/DecodeOperation.d.ts +0 -1
- package/lib/decoder/DecodeOperation.js +23 -7
- package/lib/decoder/DecodeOperation.js.map +1 -1
- package/lib/decoder/Decoder.d.ts +6 -7
- package/lib/decoder/Decoder.js +8 -8
- package/lib/decoder/Decoder.js.map +1 -1
- package/lib/decoder/strategy/RawChanges.js +1 -2
- package/lib/decoder/strategy/RawChanges.js.map +1 -1
- package/lib/decoder/strategy/StateCallbacks.d.ts +44 -11
- package/lib/decoder/strategy/StateCallbacks.js +72 -63
- package/lib/decoder/strategy/StateCallbacks.js.map +1 -1
- package/lib/encoder/ChangeTree.d.ts +1 -12
- package/lib/encoder/ChangeTree.js +29 -58
- package/lib/encoder/ChangeTree.js.map +1 -1
- package/lib/encoder/EncodeOperation.d.ts +0 -1
- package/lib/encoder/EncodeOperation.js +29 -13
- package/lib/encoder/EncodeOperation.js.map +1 -1
- package/lib/encoder/Encoder.d.ts +10 -8
- package/lib/encoder/Encoder.js +78 -53
- 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/encoder/StateView.d.ts +2 -2
- package/lib/encoder/StateView.js +48 -58
- package/lib/encoder/StateView.js.map +1 -1
- package/lib/encoding/assert.js +3 -3
- package/lib/encoding/assert.js.map +1 -1
- package/lib/encoding/decode.js +21 -22
- package/lib/encoding/decode.js.map +1 -1
- package/lib/encoding/encode.d.ts +2 -2
- package/lib/encoding/encode.js +40 -39
- package/lib/encoding/encode.js.map +1 -1
- package/lib/encoding/spec.d.ts +2 -1
- package/lib/encoding/spec.js +1 -0
- package/lib/encoding/spec.js.map +1 -1
- package/lib/index.d.ts +5 -1
- package/lib/index.js +8 -4
- 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.d.ts +2 -2
- package/lib/types/custom/ArraySchema.js +0 -9
- package/lib/types/custom/ArraySchema.js.map +1 -1
- package/lib/types/registry.js +3 -4
- package/lib/types/registry.js.map +1 -1
- package/lib/types/utils.js +1 -2
- package/lib/types/utils.js.map +1 -1
- package/lib/utils.js +3 -4
- package/lib/utils.js.map +1 -1
- package/package.json +5 -5
- package/src/Metadata.ts +47 -0
- package/src/Reflection.ts +34 -26
- package/src/Schema.ts +2 -2
- package/src/annotations.ts +7 -109
- package/src/bench_encode.ts +97 -0
- package/src/debug.ts +56 -0
- package/src/decoder/DecodeOperation.ts +30 -7
- package/src/decoder/Decoder.ts +13 -11
- package/src/decoder/strategy/StateCallbacks.ts +149 -79
- package/src/encoder/ChangeTree.ts +36 -66
- package/src/encoder/EncodeOperation.ts +29 -12
- package/src/encoder/Encoder.ts +95 -61
- package/src/encoder/Root.ts +51 -0
- package/src/encoder/StateView.ts +51 -67
- package/src/encoding/decode.ts +1 -2
- package/src/encoding/encode.ts +25 -22
- package/src/encoding/spec.ts +1 -0
- package/src/index.ts +8 -11
- package/src/types/TypeContext.ts +127 -0
- package/src/types/custom/ArraySchema.ts +2 -2
|
@@ -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);
|
|
@@ -471,6 +470,7 @@ export class ArraySchema<V = any> implements Array<V>, Collection<number, V> {
|
|
|
471
470
|
* @param thisArg An object to which the this keyword can refer in the callbackfn function.
|
|
472
471
|
* If thisArg is omitted, undefined is used as the this value.
|
|
473
472
|
*/
|
|
473
|
+
every<S extends V>(predicate: (value: V, index: number, array: V[]) => value is S, thisArg?: any): this is S[];
|
|
474
474
|
every(callbackfn: (value: V, index: number, array: V[]) => unknown, thisArg?: any): boolean {
|
|
475
475
|
return this.items.every(callbackfn, thisArg);
|
|
476
476
|
}
|
|
@@ -690,7 +690,7 @@ export class ArraySchema<V = any> implements Array<V>, Collection<number, V> {
|
|
|
690
690
|
//
|
|
691
691
|
// ES2023
|
|
692
692
|
//
|
|
693
|
-
with(index: number, value: V): V
|
|
693
|
+
with(index: number, value: V): ArraySchema<V> {
|
|
694
694
|
const copy = this.items.slice();
|
|
695
695
|
copy[index] = value;
|
|
696
696
|
return new ArraySchema(...copy);
|