@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.
- package/README.md +0 -4
- package/build/cjs/index.js +48 -48
- package/build/cjs/index.js.map +1 -1
- package/build/esm/index.mjs +130 -104
- package/build/esm/index.mjs.map +1 -1
- package/build/umd/index.js +50 -50
- package/lib/Reflection.js +87 -119
- package/lib/Reflection.js.map +1 -1
- package/lib/Schema.js +195 -257
- package/lib/Schema.js.map +1 -1
- package/lib/annotations.d.ts +6 -6
- package/lib/annotations.js +64 -92
- package/lib/annotations.js.map +1 -1
- package/lib/changes/ChangeTree.d.ts +1 -1
- package/lib/changes/ChangeTree.js +63 -70
- package/lib/changes/ChangeTree.js.map +1 -1
- package/lib/changes/ReferenceTracker.js +24 -27
- package/lib/changes/ReferenceTracker.js.map +1 -1
- package/lib/codegen/api.js +9 -9
- package/lib/codegen/api.js.map +1 -1
- package/lib/codegen/argv.d.ts +1 -1
- package/lib/codegen/argv.js +11 -11
- package/lib/codegen/argv.js.map +1 -1
- package/lib/codegen/cli.js +21 -10
- package/lib/codegen/cli.js.map +1 -1
- package/lib/codegen/languages/cpp.js +126 -77
- package/lib/codegen/languages/cpp.js.map +1 -1
- package/lib/codegen/languages/csharp.js +121 -62
- package/lib/codegen/languages/csharp.js.map +1 -1
- package/lib/codegen/languages/haxe.js +34 -26
- package/lib/codegen/languages/haxe.js.map +1 -1
- package/lib/codegen/languages/java.js +39 -27
- package/lib/codegen/languages/java.js.map +1 -1
- package/lib/codegen/languages/js.js +48 -32
- package/lib/codegen/languages/js.js.map +1 -1
- package/lib/codegen/languages/lua.js +35 -24
- package/lib/codegen/languages/lua.js.map +1 -1
- package/lib/codegen/languages/ts.js +63 -68
- package/lib/codegen/languages/ts.js.map +1 -1
- package/lib/codegen/parser.d.ts +9 -1
- package/lib/codegen/parser.js +88 -46
- package/lib/codegen/parser.js.map +1 -1
- package/lib/codegen/types.d.ts +8 -0
- package/lib/codegen/types.js +64 -54
- package/lib/codegen/types.js.map +1 -1
- package/lib/encoding/decode.js +15 -15
- package/lib/encoding/decode.js.map +1 -1
- package/lib/encoding/encode.js +14 -14
- package/lib/encoding/encode.js.map +1 -1
- package/lib/events/EventEmitter.d.ts +1 -1
- package/lib/events/EventEmitter.js +16 -47
- package/lib/events/EventEmitter.js.map +1 -1
- package/lib/filters/index.js +7 -8
- package/lib/filters/index.js.map +1 -1
- package/lib/index.js +11 -11
- package/lib/index.js.map +1 -1
- package/lib/types/ArraySchema.d.ts +1 -1
- package/lib/types/ArraySchema.js +161 -219
- package/lib/types/ArraySchema.js.map +1 -1
- package/lib/types/CollectionSchema.d.ts +1 -1
- package/lib/types/CollectionSchema.js +63 -71
- package/lib/types/CollectionSchema.js.map +1 -1
- package/lib/types/HelperTypes.d.ts +9 -9
- package/lib/types/MapSchema.d.ts +16 -16
- package/lib/types/MapSchema.js +68 -78
- package/lib/types/MapSchema.js.map +1 -1
- package/lib/types/SetSchema.js +62 -71
- package/lib/types/SetSchema.js.map +1 -1
- package/lib/types/index.js +1 -1
- package/lib/types/index.js.map +1 -1
- package/lib/types/typeRegistry.js +1 -1
- package/lib/types/typeRegistry.js.map +1 -1
- package/lib/types/utils.js +9 -10
- package/lib/types/utils.js.map +1 -1
- package/lib/utils.js +10 -13
- package/lib/utils.js.map +1 -1
- package/package.json +18 -15
- package/src/Reflection.ts +159 -0
- package/src/Schema.ts +1024 -0
- package/src/annotations.ts +400 -0
- package/src/changes/ChangeTree.ts +295 -0
- package/src/changes/ReferenceTracker.ts +81 -0
- package/src/codegen/api.ts +46 -0
- package/src/codegen/argv.ts +40 -0
- package/src/codegen/cli.ts +65 -0
- package/src/codegen/languages/cpp.ts +297 -0
- package/src/codegen/languages/csharp.ts +208 -0
- package/src/codegen/languages/haxe.ts +110 -0
- package/src/codegen/languages/java.ts +115 -0
- package/src/codegen/languages/js.ts +115 -0
- package/src/codegen/languages/lua.ts +125 -0
- package/src/codegen/languages/ts.ts +129 -0
- package/src/codegen/parser.ts +299 -0
- package/src/codegen/types.ts +177 -0
- package/src/encoding/decode.ts +278 -0
- package/src/encoding/encode.ts +283 -0
- package/src/filters/index.ts +23 -0
- package/src/index.ts +59 -0
- package/src/spec.ts +49 -0
- package/src/types/ArraySchema.ts +612 -0
- package/src/types/CollectionSchema.ts +199 -0
- package/src/types/HelperTypes.ts +34 -0
- package/src/types/MapSchema.ts +268 -0
- package/src/types/SetSchema.ts +208 -0
- package/src/types/typeRegistry.ts +19 -0
- package/src/types/utils.ts +62 -0
- 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
|
+
}
|