@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.
Files changed (116) hide show
  1. package/README.md +131 -61
  2. package/build/cjs/index.js +644 -283
  3. package/build/cjs/index.js.map +1 -1
  4. package/build/esm/index.mjs +643 -282
  5. package/build/esm/index.mjs.map +1 -1
  6. package/build/umd/index.js +644 -283
  7. package/lib/Metadata.d.ts +2 -0
  8. package/lib/Metadata.js +39 -0
  9. package/lib/Metadata.js.map +1 -1
  10. package/lib/Reflection.d.ts +2 -3
  11. package/lib/Reflection.js +31 -26
  12. package/lib/Reflection.js.map +1 -1
  13. package/lib/Schema.d.ts +2 -2
  14. package/lib/Schema.js +2 -2
  15. package/lib/Schema.js.map +1 -1
  16. package/lib/annotations.d.ts +0 -19
  17. package/lib/annotations.js +15 -101
  18. package/lib/annotations.js.map +1 -1
  19. package/lib/bench_encode.d.ts +1 -0
  20. package/lib/bench_encode.js +120 -0
  21. package/lib/bench_encode.js.map +1 -0
  22. package/lib/codegen/api.js +1 -2
  23. package/lib/codegen/api.js.map +1 -1
  24. package/lib/codegen/languages/cpp.js +1 -2
  25. package/lib/codegen/languages/cpp.js.map +1 -1
  26. package/lib/codegen/languages/csharp.js +1 -2
  27. package/lib/codegen/languages/csharp.js.map +1 -1
  28. package/lib/codegen/languages/haxe.js +1 -2
  29. package/lib/codegen/languages/haxe.js.map +1 -1
  30. package/lib/codegen/languages/java.js +1 -2
  31. package/lib/codegen/languages/java.js.map +1 -1
  32. package/lib/codegen/languages/js.js +1 -2
  33. package/lib/codegen/languages/js.js.map +1 -1
  34. package/lib/codegen/languages/lua.js +1 -2
  35. package/lib/codegen/languages/lua.js.map +1 -1
  36. package/lib/codegen/languages/ts.js +1 -2
  37. package/lib/codegen/languages/ts.js.map +1 -1
  38. package/lib/codegen/parser.js +2 -3
  39. package/lib/codegen/parser.js.map +1 -1
  40. package/lib/codegen/types.js +3 -3
  41. package/lib/codegen/types.js.map +1 -1
  42. package/lib/debug.d.ts +1 -0
  43. package/lib/debug.js +52 -0
  44. package/lib/debug.js.map +1 -0
  45. package/lib/decoder/DecodeOperation.d.ts +0 -1
  46. package/lib/decoder/DecodeOperation.js +23 -7
  47. package/lib/decoder/DecodeOperation.js.map +1 -1
  48. package/lib/decoder/Decoder.d.ts +6 -7
  49. package/lib/decoder/Decoder.js +8 -8
  50. package/lib/decoder/Decoder.js.map +1 -1
  51. package/lib/decoder/strategy/RawChanges.js +1 -2
  52. package/lib/decoder/strategy/RawChanges.js.map +1 -1
  53. package/lib/decoder/strategy/StateCallbacks.d.ts +44 -11
  54. package/lib/decoder/strategy/StateCallbacks.js +72 -63
  55. package/lib/decoder/strategy/StateCallbacks.js.map +1 -1
  56. package/lib/encoder/ChangeTree.d.ts +1 -12
  57. package/lib/encoder/ChangeTree.js +29 -58
  58. package/lib/encoder/ChangeTree.js.map +1 -1
  59. package/lib/encoder/EncodeOperation.d.ts +0 -1
  60. package/lib/encoder/EncodeOperation.js +29 -13
  61. package/lib/encoder/EncodeOperation.js.map +1 -1
  62. package/lib/encoder/Encoder.d.ts +10 -8
  63. package/lib/encoder/Encoder.js +78 -53
  64. package/lib/encoder/Encoder.js.map +1 -1
  65. package/lib/encoder/Root.d.ts +17 -0
  66. package/lib/encoder/Root.js +44 -0
  67. package/lib/encoder/Root.js.map +1 -0
  68. package/lib/encoder/StateView.d.ts +2 -2
  69. package/lib/encoder/StateView.js +48 -58
  70. package/lib/encoder/StateView.js.map +1 -1
  71. package/lib/encoding/assert.js +3 -3
  72. package/lib/encoding/assert.js.map +1 -1
  73. package/lib/encoding/decode.js +21 -22
  74. package/lib/encoding/decode.js.map +1 -1
  75. package/lib/encoding/encode.d.ts +2 -2
  76. package/lib/encoding/encode.js +40 -39
  77. package/lib/encoding/encode.js.map +1 -1
  78. package/lib/encoding/spec.d.ts +2 -1
  79. package/lib/encoding/spec.js +1 -0
  80. package/lib/encoding/spec.js.map +1 -1
  81. package/lib/index.d.ts +5 -1
  82. package/lib/index.js +8 -4
  83. package/lib/index.js.map +1 -1
  84. package/lib/types/TypeContext.d.ts +23 -0
  85. package/lib/types/TypeContext.js +109 -0
  86. package/lib/types/TypeContext.js.map +1 -0
  87. package/lib/types/custom/ArraySchema.d.ts +2 -2
  88. package/lib/types/custom/ArraySchema.js +0 -9
  89. package/lib/types/custom/ArraySchema.js.map +1 -1
  90. package/lib/types/registry.js +3 -4
  91. package/lib/types/registry.js.map +1 -1
  92. package/lib/types/utils.js +1 -2
  93. package/lib/types/utils.js.map +1 -1
  94. package/lib/utils.js +3 -4
  95. package/lib/utils.js.map +1 -1
  96. package/package.json +5 -5
  97. package/src/Metadata.ts +47 -0
  98. package/src/Reflection.ts +34 -26
  99. package/src/Schema.ts +2 -2
  100. package/src/annotations.ts +7 -109
  101. package/src/bench_encode.ts +97 -0
  102. package/src/debug.ts +56 -0
  103. package/src/decoder/DecodeOperation.ts +30 -7
  104. package/src/decoder/Decoder.ts +13 -11
  105. package/src/decoder/strategy/StateCallbacks.ts +149 -79
  106. package/src/encoder/ChangeTree.ts +36 -66
  107. package/src/encoder/EncodeOperation.ts +29 -12
  108. package/src/encoder/Encoder.ts +95 -61
  109. package/src/encoder/Root.ts +51 -0
  110. package/src/encoder/StateView.ts +51 -67
  111. package/src/encoding/decode.ts +1 -2
  112. package/src/encoding/encode.ts +25 -22
  113. package/src/encoding/spec.ts +1 -0
  114. package/src/index.ts +8 -11
  115. package/src/types/TypeContext.ts +127 -0
  116. 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);