@colyseus/schema 1.0.33 → 1.0.36

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 (76) hide show
  1. package/README.md +0 -4
  2. package/build/cjs/index.js +36 -35
  3. package/build/cjs/index.js.map +1 -1
  4. package/build/esm/index.mjs +114 -85
  5. package/build/esm/index.mjs.map +1 -1
  6. package/build/umd/index.js +38 -37
  7. package/lib/Reflection.js +11 -11
  8. package/lib/Reflection.js.map +1 -1
  9. package/lib/Schema.js +14 -13
  10. package/lib/Schema.js.map +1 -1
  11. package/lib/annotations.js +17 -13
  12. package/lib/annotations.js.map +1 -1
  13. package/lib/changes/ChangeTree.js +2 -2
  14. package/lib/changes/ChangeTree.js.map +1 -1
  15. package/lib/codegen/api.js +1 -1
  16. package/lib/codegen/api.js.map +1 -1
  17. package/lib/codegen/argv.d.ts +1 -1
  18. package/lib/codegen/cli.js +5 -5
  19. package/lib/codegen/cli.js.map +1 -1
  20. package/lib/codegen/languages/cpp.js +38 -38
  21. package/lib/codegen/languages/cpp.js.map +1 -1
  22. package/lib/codegen/languages/csharp.js +25 -21
  23. package/lib/codegen/languages/csharp.js.map +1 -1
  24. package/lib/codegen/languages/haxe.js +13 -13
  25. package/lib/codegen/languages/haxe.js.map +1 -1
  26. package/lib/codegen/languages/java.js +11 -11
  27. package/lib/codegen/languages/java.js.map +1 -1
  28. package/lib/codegen/languages/js.js +16 -16
  29. package/lib/codegen/languages/js.js.map +1 -1
  30. package/lib/codegen/languages/lua.js +12 -12
  31. package/lib/codegen/languages/lua.js.map +1 -1
  32. package/lib/codegen/languages/ts.js +37 -33
  33. package/lib/codegen/languages/ts.js.map +1 -1
  34. package/lib/codegen/parser.js +3 -3
  35. package/lib/codegen/parser.js.map +1 -1
  36. package/lib/codegen/types.js +2 -2
  37. package/lib/codegen/types.js.map +1 -1
  38. package/lib/events/EventEmitter.js +10 -6
  39. package/lib/events/EventEmitter.js.map +1 -1
  40. package/lib/index.js +4 -4
  41. package/lib/index.js.map +1 -1
  42. package/lib/types/ArraySchema.js +14 -10
  43. package/lib/types/ArraySchema.js.map +1 -1
  44. package/lib/types/MapSchema.d.ts +19 -19
  45. package/lib/types/MapSchema.js +2 -2
  46. package/lib/types/MapSchema.js.map +1 -1
  47. package/package.json +2 -1
  48. package/src/Reflection.ts +159 -0
  49. package/src/Schema.ts +1084 -0
  50. package/src/annotations.ts +357 -0
  51. package/src/changes/ChangeTree.ts +373 -0
  52. package/src/codegen/api.ts +46 -0
  53. package/src/codegen/argv.ts +40 -0
  54. package/src/codegen/cli.ts +65 -0
  55. package/src/codegen/languages/cpp.ts +297 -0
  56. package/src/codegen/languages/csharp.ts +119 -0
  57. package/src/codegen/languages/haxe.ts +110 -0
  58. package/src/codegen/languages/java.ts +115 -0
  59. package/src/codegen/languages/js.ts +115 -0
  60. package/src/codegen/languages/lua.ts +125 -0
  61. package/src/codegen/languages/ts.ts +129 -0
  62. package/src/codegen/parser.ts +251 -0
  63. package/src/codegen/types.ts +164 -0
  64. package/src/encoding/decode.ts +278 -0
  65. package/src/encoding/encode.ts +283 -0
  66. package/src/events/EventEmitter.ts +32 -0
  67. package/src/filters/index.ts +23 -0
  68. package/src/index.ts +59 -0
  69. package/src/spec.ts +49 -0
  70. package/src/types/ArraySchema.ts +608 -0
  71. package/src/types/CollectionSchema.ts +188 -0
  72. package/src/types/HelperTypes.ts +34 -0
  73. package/src/types/MapSchema.ts +255 -0
  74. package/src/types/SetSchema.ts +197 -0
  75. package/src/types/index.ts +19 -0
  76. package/src/utils.ts +28 -0
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Extracted from https://www.npmjs.com/package/strong-events
3
+ */
4
+
5
+ type ExtractFunctionParameters<T extends (...args: any[]) => any> = T extends (...args: infer P) => any ? P : never;
6
+
7
+ export class EventEmitter_<CallbackSignature extends (...args: any[]) => any> {
8
+ handlers: Array<CallbackSignature> = [];
9
+
10
+ register(cb: CallbackSignature, once: boolean = false) {
11
+ this.handlers.push(cb);
12
+ return this;
13
+ }
14
+
15
+ invoke(...args: ExtractFunctionParameters<CallbackSignature>) {
16
+ this.handlers.forEach((handler) => handler(...args));
17
+ }
18
+
19
+ invokeAsync(...args: ExtractFunctionParameters<CallbackSignature>) {
20
+ return Promise.all(this.handlers.map((handler) => handler(...args)));
21
+ }
22
+
23
+ remove (cb: CallbackSignature) {
24
+ const index = this.handlers.indexOf(cb);
25
+ this.handlers[index] = this.handlers[this.handlers.length - 1];
26
+ this.handlers.pop();
27
+ }
28
+
29
+ clear() {
30
+ this.handlers = [];
31
+ }
32
+ }
@@ -0,0 +1,23 @@
1
+ import { ChangeTree } from "../changes/ChangeTree";
2
+ import { ClientWithSessionId } from "../annotations";
3
+
4
+ export class ClientState {
5
+ refIds = new WeakSet<ChangeTree>();
6
+ containerIndexes = new WeakMap<ChangeTree, Set<number>>();
7
+ // containerIndexes = new Map<ChangeTree, Set<number>>();
8
+
9
+ addRefId(changeTree: ChangeTree) {
10
+ if (!this.refIds.has(changeTree)) {
11
+ this.refIds.add(changeTree);
12
+ this.containerIndexes.set(changeTree, new Set());
13
+ }
14
+ }
15
+
16
+ static get(client: ClientWithSessionId) {
17
+ if (client.$filterState === undefined) {
18
+ client.$filterState = new ClientState();
19
+ }
20
+
21
+ return client.$filterState as ClientState;
22
+ }
23
+ }
package/src/index.ts ADDED
@@ -0,0 +1,59 @@
1
+ export { Schema, DataChange } from "./Schema";
2
+
3
+ import { MapSchema, getMapProxy } from "./types/MapSchema"
4
+ export { MapSchema };
5
+
6
+ import { ArraySchema, getArrayProxy } from "./types/ArraySchema";
7
+ export { ArraySchema };
8
+
9
+ import { CollectionSchema } from "./types/CollectionSchema";
10
+ export { CollectionSchema };
11
+
12
+ import { SetSchema } from "./types/SetSchema";
13
+ export { SetSchema };
14
+
15
+ import { registerType } from "./types";
16
+ export { registerType };
17
+
18
+ registerType("map", { constructor: MapSchema, getProxy: getMapProxy });
19
+ registerType("array", { constructor: ArraySchema, getProxy: getArrayProxy });
20
+ registerType("set", { constructor: SetSchema });
21
+ registerType("collection", { constructor: CollectionSchema, });
22
+
23
+ // Utils
24
+ export { dumpChanges } from "./utils";
25
+
26
+ // Encoder / Decoder
27
+ export { Iterator } from "./encoding/decode";
28
+ import * as encode from "./encoding/encode";
29
+ import * as decode from "./encoding/decode";
30
+ export { encode, decode };
31
+
32
+ // Reflection
33
+ export {
34
+ Reflection,
35
+ ReflectionType,
36
+ ReflectionField,
37
+ } from "./Reflection";
38
+
39
+ export {
40
+ // Annotations
41
+ type,
42
+ deprecated,
43
+ filter,
44
+ filterChildren,
45
+ defineTypes,
46
+ hasFilter,
47
+
48
+ // Internals
49
+ SchemaDefinition,
50
+
51
+ // Types
52
+ Context,
53
+ PrimitiveType,
54
+ Definition,
55
+ DefinitionType,
56
+ FilterCallback,
57
+ } from "./annotations";
58
+
59
+ export { OPERATION } from "./spec";
package/src/spec.ts ADDED
@@ -0,0 +1,49 @@
1
+ // export const SWITCH_TO_STRUCTURE = 193; (easily collides with DELETE_AND_ADD + fieldIndex = 2)
2
+ export const SWITCH_TO_STRUCTURE = 255; // (decoding collides with DELETE_AND_ADD + fieldIndex = 63)
3
+ export const TYPE_ID = 213;
4
+
5
+ /**
6
+ * Encoding Schema field operations.
7
+ */
8
+ export enum OPERATION {
9
+ // add new structure/primitive
10
+ ADD = 128,
11
+
12
+ // replace structure/primitive
13
+ REPLACE = 0,
14
+
15
+ // delete field
16
+ DELETE = 64,
17
+
18
+ // DELETE field, followed by an ADD
19
+ DELETE_AND_ADD = 192, // 11100000
20
+
21
+ // TOUCH is used to determine hierarchy of nested Schema structures during serialization.
22
+ // touches are NOT encoded.
23
+ TOUCH = 1, // 00000000
24
+
25
+ // MapSchema Operations
26
+ CLEAR = 10,
27
+ }
28
+
29
+ // export enum OPERATION {
30
+ // // add new structure/primitive
31
+ // // (128)
32
+ // ADD = 128, // 10000000,
33
+
34
+ // // replace structure/primitive
35
+ // REPLACE = 1,// 00000001
36
+
37
+ // // delete field
38
+ // DELETE = 192, // 11000000
39
+
40
+ // // DELETE field, followed by an ADD
41
+ // DELETE_AND_ADD = 224, // 11100000
42
+
43
+ // // TOUCH is used to determine hierarchy of nested Schema structures during serialization.
44
+ // // touches are NOT encoded.
45
+ // TOUCH = 0, // 00000000
46
+
47
+ // // MapSchema Operations
48
+ // CLEAR = 10,
49
+ // }