@colyseus/schema 2.0.4 → 2.0.6

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 (107) hide show
  1. package/README.md +0 -4
  2. package/build/cjs/index.js +48 -48
  3. package/build/cjs/index.js.map +1 -1
  4. package/build/esm/index.mjs +130 -104
  5. package/build/esm/index.mjs.map +1 -1
  6. package/build/umd/index.js +50 -50
  7. package/lib/Reflection.js +87 -119
  8. package/lib/Reflection.js.map +1 -1
  9. package/lib/Schema.js +195 -257
  10. package/lib/Schema.js.map +1 -1
  11. package/lib/annotations.d.ts +6 -6
  12. package/lib/annotations.js +64 -92
  13. package/lib/annotations.js.map +1 -1
  14. package/lib/changes/ChangeTree.d.ts +1 -1
  15. package/lib/changes/ChangeTree.js +63 -70
  16. package/lib/changes/ChangeTree.js.map +1 -1
  17. package/lib/changes/ReferenceTracker.js +24 -27
  18. package/lib/changes/ReferenceTracker.js.map +1 -1
  19. package/lib/codegen/api.js +9 -9
  20. package/lib/codegen/api.js.map +1 -1
  21. package/lib/codegen/argv.d.ts +1 -1
  22. package/lib/codegen/argv.js +11 -11
  23. package/lib/codegen/argv.js.map +1 -1
  24. package/lib/codegen/cli.js +21 -10
  25. package/lib/codegen/cli.js.map +1 -1
  26. package/lib/codegen/languages/cpp.js +126 -77
  27. package/lib/codegen/languages/cpp.js.map +1 -1
  28. package/lib/codegen/languages/csharp.js +121 -62
  29. package/lib/codegen/languages/csharp.js.map +1 -1
  30. package/lib/codegen/languages/haxe.js +34 -26
  31. package/lib/codegen/languages/haxe.js.map +1 -1
  32. package/lib/codegen/languages/java.js +39 -27
  33. package/lib/codegen/languages/java.js.map +1 -1
  34. package/lib/codegen/languages/js.js +48 -32
  35. package/lib/codegen/languages/js.js.map +1 -1
  36. package/lib/codegen/languages/lua.js +35 -24
  37. package/lib/codegen/languages/lua.js.map +1 -1
  38. package/lib/codegen/languages/ts.js +63 -68
  39. package/lib/codegen/languages/ts.js.map +1 -1
  40. package/lib/codegen/parser.d.ts +9 -1
  41. package/lib/codegen/parser.js +88 -46
  42. package/lib/codegen/parser.js.map +1 -1
  43. package/lib/codegen/types.d.ts +8 -0
  44. package/lib/codegen/types.js +64 -54
  45. package/lib/codegen/types.js.map +1 -1
  46. package/lib/encoding/decode.js +15 -15
  47. package/lib/encoding/decode.js.map +1 -1
  48. package/lib/encoding/encode.js +14 -14
  49. package/lib/encoding/encode.js.map +1 -1
  50. package/lib/events/EventEmitter.d.ts +1 -1
  51. package/lib/events/EventEmitter.js +16 -47
  52. package/lib/events/EventEmitter.js.map +1 -1
  53. package/lib/filters/index.js +7 -8
  54. package/lib/filters/index.js.map +1 -1
  55. package/lib/index.js +11 -11
  56. package/lib/index.js.map +1 -1
  57. package/lib/types/ArraySchema.d.ts +1 -1
  58. package/lib/types/ArraySchema.js +161 -219
  59. package/lib/types/ArraySchema.js.map +1 -1
  60. package/lib/types/CollectionSchema.d.ts +1 -1
  61. package/lib/types/CollectionSchema.js +63 -71
  62. package/lib/types/CollectionSchema.js.map +1 -1
  63. package/lib/types/HelperTypes.d.ts +9 -9
  64. package/lib/types/MapSchema.d.ts +16 -16
  65. package/lib/types/MapSchema.js +68 -78
  66. package/lib/types/MapSchema.js.map +1 -1
  67. package/lib/types/SetSchema.js +62 -71
  68. package/lib/types/SetSchema.js.map +1 -1
  69. package/lib/types/index.js +1 -1
  70. package/lib/types/index.js.map +1 -1
  71. package/lib/types/typeRegistry.js +1 -1
  72. package/lib/types/typeRegistry.js.map +1 -1
  73. package/lib/types/utils.js +9 -10
  74. package/lib/types/utils.js.map +1 -1
  75. package/lib/utils.js +10 -13
  76. package/lib/utils.js.map +1 -1
  77. package/package.json +18 -15
  78. package/src/Reflection.ts +159 -0
  79. package/src/Schema.ts +1024 -0
  80. package/src/annotations.ts +400 -0
  81. package/src/changes/ChangeTree.ts +295 -0
  82. package/src/changes/ReferenceTracker.ts +81 -0
  83. package/src/codegen/api.ts +46 -0
  84. package/src/codegen/argv.ts +40 -0
  85. package/src/codegen/cli.ts +65 -0
  86. package/src/codegen/languages/cpp.ts +297 -0
  87. package/src/codegen/languages/csharp.ts +208 -0
  88. package/src/codegen/languages/haxe.ts +110 -0
  89. package/src/codegen/languages/java.ts +115 -0
  90. package/src/codegen/languages/js.ts +115 -0
  91. package/src/codegen/languages/lua.ts +125 -0
  92. package/src/codegen/languages/ts.ts +129 -0
  93. package/src/codegen/parser.ts +299 -0
  94. package/src/codegen/types.ts +177 -0
  95. package/src/encoding/decode.ts +278 -0
  96. package/src/encoding/encode.ts +283 -0
  97. package/src/filters/index.ts +23 -0
  98. package/src/index.ts +59 -0
  99. package/src/spec.ts +49 -0
  100. package/src/types/ArraySchema.ts +612 -0
  101. package/src/types/CollectionSchema.ts +199 -0
  102. package/src/types/HelperTypes.ts +34 -0
  103. package/src/types/MapSchema.ts +268 -0
  104. package/src/types/SetSchema.ts +208 -0
  105. package/src/types/typeRegistry.ts +19 -0
  106. package/src/types/utils.ts +62 -0
  107. package/src/utils.ts +28 -0
@@ -0,0 +1,19 @@
1
+ export interface TypeDefinition {
2
+ constructor: any,
3
+
4
+ // //
5
+ // // TODO: deprecate proxy on next version
6
+ // // the proxy is used for compatibility with versions <1.0.0 of @colyseus/schema
7
+ // //
8
+ // getProxy?: any,
9
+ }
10
+
11
+ const registeredTypes: {[identifier: string] : TypeDefinition} = {};
12
+
13
+ export function registerType(identifier: string, definition: TypeDefinition) {
14
+ registeredTypes[identifier] = definition;
15
+ }
16
+
17
+ export function getType(identifier: string): TypeDefinition {
18
+ return registeredTypes[identifier];
19
+ }
@@ -0,0 +1,62 @@
1
+ import { CollectionSchema, DataChange } from "..";
2
+ import { OPERATION } from "../spec";
3
+
4
+ export function addCallback(
5
+ $callbacks: { [op: number]: Function[] },
6
+ op: OPERATION,
7
+ callback: (item: any, key: any) => void,
8
+ existing?: { forEach(callback: (item: any, key: any) => void): void; }
9
+ ) {
10
+ // initialize list of callbacks
11
+ if (!$callbacks[op]) {
12
+ $callbacks[op] = [];
13
+ }
14
+
15
+ $callbacks[op].push(callback);
16
+
17
+ //
18
+ // Trigger callback for existing elements
19
+ // - OPERATION.ADD
20
+ // - OPERATION.REPLACE
21
+ //
22
+ existing?.forEach((item, key) => callback(item, key));
23
+
24
+ return () => spliceOne($callbacks[op], $callbacks[op].indexOf(callback));
25
+ }
26
+
27
+
28
+ export function removeChildRefs(this: CollectionSchema, changes: DataChange[]) {
29
+ const needRemoveRef = (typeof (this.$changes.getType()) !== "string");
30
+
31
+ this.$items.forEach((item: any, key: any) => {
32
+ changes.push({
33
+ refId: this.$changes.refId,
34
+ op: OPERATION.DELETE,
35
+ field: key,
36
+ value: undefined,
37
+ previousValue: item
38
+ });
39
+
40
+ if (needRemoveRef) {
41
+ this.$changes.root.removeRef(item['$changes'].refId);
42
+ }
43
+ });
44
+ }
45
+
46
+
47
+ export function spliceOne(arr: any[], index: number): boolean {
48
+ // manually splice an array
49
+ if (index === -1 || index >= arr.length) {
50
+ return false;
51
+ }
52
+
53
+ const len = arr.length - 1;
54
+
55
+ for (let i = index; i < len; i++) {
56
+ arr[i] = arr[i + 1];
57
+ }
58
+
59
+ arr.length = len;
60
+
61
+ return true;
62
+ }
package/src/utils.ts ADDED
@@ -0,0 +1,28 @@
1
+ import { Schema } from "./";
2
+ import { ChangeTree } from "./changes/ChangeTree";
3
+
4
+ export function dumpChanges(schema: Schema) {
5
+ const changeTrees: ChangeTree[] = [schema['$changes']];
6
+ let numChangeTrees = 1;
7
+
8
+ const dump = {};
9
+ let currentStructure = dump;
10
+
11
+ for (let i = 0; i < numChangeTrees; i++) {
12
+ const changeTree = changeTrees[i];
13
+
14
+ changeTree.changes.forEach((change) => {
15
+ const ref = changeTree.ref;
16
+ const fieldIndex = change.index;
17
+
18
+ const field = ((ref as Schema)['_definition'])
19
+ ? ref['_definition'].fieldsByIndex[fieldIndex]
20
+ : ref['$indexes'].get(fieldIndex);
21
+
22
+ currentStructure[field] = changeTree.getValue(fieldIndex);
23
+ });
24
+
25
+ }
26
+
27
+ return dump;
28
+ }