@colyseus/schema 1.0.27 → 1.0.31
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/lib/changes/ReferenceTracker.d.ts +14 -0
- package/lib/changes/ReferenceTracker.js +77 -0
- package/lib/changes/ReferenceTracker.js.map +1 -0
- package/lib/codegen/languages/ts.js +1 -1
- package/lib/codegen/languages/ts.js.map +1 -1
- package/lib/codegen/types.js +14 -1
- package/lib/codegen/types.js.map +1 -1
- package/lib/types/typeRegistry.d.ts +5 -0
- package/lib/types/typeRegistry.js +13 -0
- package/lib/types/typeRegistry.js.map +1 -0
- package/lib/types/utils.d.ts +9 -0
- package/lib/types/utils.js +50 -0
- package/lib/types/utils.js.map +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Ref } from "./ChangeTree";
|
|
2
|
+
export declare class ReferenceTracker {
|
|
3
|
+
refs: Map<number, Ref>;
|
|
4
|
+
refCounts: {
|
|
5
|
+
[refId: number]: number;
|
|
6
|
+
};
|
|
7
|
+
deletedRefs: Set<number>;
|
|
8
|
+
protected nextUniqueId: number;
|
|
9
|
+
getNextUniqueId(): number;
|
|
10
|
+
addRef(refId: number, ref: Ref, incrementCount?: boolean): void;
|
|
11
|
+
removeRef(refId: any): void;
|
|
12
|
+
clearRefs(): void;
|
|
13
|
+
garbageCollectDeletedRefs(): void;
|
|
14
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ReferenceTracker = void 0;
|
|
4
|
+
var Schema_1 = require("../Schema");
|
|
5
|
+
var ReferenceTracker = /** @class */ (function () {
|
|
6
|
+
function ReferenceTracker() {
|
|
7
|
+
//
|
|
8
|
+
// Relation of refId => Schema structure
|
|
9
|
+
// For direct access of structures during decoding time.
|
|
10
|
+
//
|
|
11
|
+
this.refs = new Map();
|
|
12
|
+
this.refCounts = {};
|
|
13
|
+
this.deletedRefs = new Set();
|
|
14
|
+
this.nextUniqueId = 0;
|
|
15
|
+
}
|
|
16
|
+
ReferenceTracker.prototype.getNextUniqueId = function () {
|
|
17
|
+
return this.nextUniqueId++;
|
|
18
|
+
};
|
|
19
|
+
// for decoding
|
|
20
|
+
ReferenceTracker.prototype.addRef = function (refId, ref, incrementCount) {
|
|
21
|
+
if (incrementCount === void 0) { incrementCount = true; }
|
|
22
|
+
this.refs.set(refId, ref);
|
|
23
|
+
if (incrementCount) {
|
|
24
|
+
this.refCounts[refId] = (this.refCounts[refId] || 0) + 1;
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
// for decoding
|
|
28
|
+
ReferenceTracker.prototype.removeRef = function (refId) {
|
|
29
|
+
this.refCounts[refId] = this.refCounts[refId] - 1;
|
|
30
|
+
this.deletedRefs.add(refId);
|
|
31
|
+
};
|
|
32
|
+
ReferenceTracker.prototype.clearRefs = function () {
|
|
33
|
+
this.refs.clear();
|
|
34
|
+
this.deletedRefs.clear();
|
|
35
|
+
this.refCounts = {};
|
|
36
|
+
};
|
|
37
|
+
// for decoding
|
|
38
|
+
ReferenceTracker.prototype.garbageCollectDeletedRefs = function () {
|
|
39
|
+
var _this = this;
|
|
40
|
+
this.deletedRefs.forEach(function (refId) {
|
|
41
|
+
//
|
|
42
|
+
// Skip active references.
|
|
43
|
+
//
|
|
44
|
+
if (_this.refCounts[refId] > 0) {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
var ref = _this.refs.get(refId);
|
|
48
|
+
//
|
|
49
|
+
// Ensure child schema instances have their references removed as well.
|
|
50
|
+
//
|
|
51
|
+
if (ref instanceof Schema_1.Schema) {
|
|
52
|
+
for (var fieldName in ref['_definition'].schema) {
|
|
53
|
+
if (typeof (ref['_definition'].schema[fieldName]) !== "string" &&
|
|
54
|
+
ref[fieldName] &&
|
|
55
|
+
ref[fieldName]['$changes']) {
|
|
56
|
+
_this.removeRef(ref[fieldName]['$changes'].refId);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
var definition = ref['$changes'].parent._definition;
|
|
62
|
+
var type = definition.schema[definition.fieldsByIndex[ref['$changes'].parentIndex]];
|
|
63
|
+
if (typeof (Object.values(type)[0]) === "function") {
|
|
64
|
+
Array.from(ref.values())
|
|
65
|
+
.forEach(function (child) { return _this.removeRef(child['$changes'].refId); });
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
_this.refs.delete(refId);
|
|
69
|
+
delete _this.refCounts[refId];
|
|
70
|
+
});
|
|
71
|
+
// clear deleted refs.
|
|
72
|
+
this.deletedRefs.clear();
|
|
73
|
+
};
|
|
74
|
+
return ReferenceTracker;
|
|
75
|
+
}());
|
|
76
|
+
exports.ReferenceTracker = ReferenceTracker;
|
|
77
|
+
//# sourceMappingURL=ReferenceTracker.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ReferenceTracker.js","sourceRoot":"","sources":["../../src/changes/ReferenceTracker.ts"],"names":[],"mappings":";;;AAAA,oCAAmC;AAInC;IAAA;QACI,EAAE;QACF,wCAAwC;QACxC,wDAAwD;QACxD,EAAE;QACK,SAAI,GAAG,IAAI,GAAG,EAAe,CAAC;QAC9B,cAAS,GAAiC,EAAE,CAAC;QAC7C,gBAAW,GAAG,IAAI,GAAG,EAAU,CAAC;QAE7B,iBAAY,GAAW,CAAC,CAAC;IAmEvC,CAAC;IAjEG,0CAAe,GAAf;QACI,OAAO,IAAI,CAAC,YAAY,EAAE,CAAC;IAC/B,CAAC;IAED,eAAe;IACf,iCAAM,GAAN,UAAO,KAAa,EAAE,GAAQ,EAAE,cAA8B;QAA9B,+BAAA,EAAA,qBAA8B;QAC1D,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC;QAE1B,IAAI,cAAc,EAAE;YAChB,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,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;aAEJ;iBAAM;gBACH,IAAM,UAAU,GAAqB,GAAG,CAAC,UAAU,CAAC,CAAC,MAAM,CAAC,WAAW,CAAC;gBACxE,IAAM,IAAI,GAAG,UAAU,CAAC,MAAM,CAAC,UAAU,CAAC,aAAa,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC;gBAEtF,IAAI,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,UAAU,EAAE;oBAChD,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,MAAM,EAAE,CAAC;yBACnB,OAAO,CAAC,UAAC,KAAK,IAAK,OAAA,KAAI,CAAC,SAAS,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC,KAAK,CAAC,EAAvC,CAAuC,CAAC,CAAC;iBACpE;aACJ;YAED,KAAI,CAAC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;YACxB,OAAO,KAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;QACjC,CAAC,CAAC,CAAC;QAEH,sBAAsB;QACtB,IAAI,CAAC,WAAW,CAAC,KAAK,EAAE,CAAC;IAC7B,CAAC;IAEL,uBAAC;AAAD,CAAC,AA5ED,IA4EC;AA5EY,4CAAgB","sourcesContent":["import { Schema } from \"../Schema\";\nimport { Ref } from \"./ChangeTree\";\nimport type { SchemaDefinition } from \"../annotations\";\n\nexport class ReferenceTracker {\n //\n // Relation of refId => Schema structure\n // For direct access of structures during decoding time.\n //\n public refs = new Map<number, Ref>();\n public refCounts: { [refId: number]: number; } = {};\n public deletedRefs = new Set<number>();\n\n protected nextUniqueId: number = 0;\n\n getNextUniqueId() {\n return this.nextUniqueId++;\n }\n\n // for decoding\n addRef(refId: number, ref: Ref, incrementCount: boolean = true) {\n this.refs.set(refId, ref);\n\n if (incrementCount) {\n this.refCounts[refId] = (this.refCounts[refId] || 0) + 1;\n }\n }\n\n // for decoding\n removeRef(refId) {\n this.refCounts[refId] = this.refCounts[refId] - 1;\n this.deletedRefs.add(refId);\n }\n\n clearRefs() {\n this.refs.clear();\n this.deletedRefs.clear();\n this.refCounts = {};\n }\n\n // for decoding\n garbageCollectDeletedRefs() {\n this.deletedRefs.forEach((refId) => {\n //\n // Skip active references.\n //\n if (this.refCounts[refId] > 0) { return; }\n\n const ref = this.refs.get(refId);\n\n //\n // Ensure child schema instances have their references removed as well.\n //\n if (ref instanceof Schema) {\n for (const fieldName in ref['_definition'].schema) {\n if (typeof (ref['_definition'].schema[fieldName]) !== \"string\" &&\n ref[fieldName] &&\n ref[fieldName]['$changes']) {\n this.removeRef(ref[fieldName]['$changes'].refId);\n }\n }\n\n } else {\n const definition: SchemaDefinition = ref['$changes'].parent._definition;\n const type = definition.schema[definition.fieldsByIndex[ref['$changes'].parentIndex]];\n\n if (typeof (Object.values(type)[0]) === \"function\") {\n Array.from(ref.values())\n .forEach((child) => this.removeRef(child['$changes'].refId));\n }\n }\n\n this.refs.delete(refId);\n delete this.refCounts[refId];\n });\n\n // clear deleted refs.\n this.deletedRefs.clear();\n }\n\n}\n"]}
|
|
@@ -54,7 +54,7 @@ function generateClass(klass, namespace, allClasses) {
|
|
|
54
54
|
klass.properties.forEach(function (property) {
|
|
55
55
|
var type = property.type;
|
|
56
56
|
// keep all refs list
|
|
57
|
-
if ((type === "ref" || type === "array" || type === "map")) {
|
|
57
|
+
if ((type === "ref" || type === "array" || type === "map" || type === "set")) {
|
|
58
58
|
allRefs.push(property);
|
|
59
59
|
}
|
|
60
60
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ts.js","sourceRoot":"","sources":["../../../src/codegen/languages/ts.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAAA,kCAA2G;AAG3G,IAAM,QAAQ,GAAG;IACb,QAAQ,EAAE,QAAQ;IAClB,QAAQ,EAAE,QAAQ;IAClB,SAAS,EAAE,SAAS;IACpB,MAAM,EAAE,QAAQ;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IAAI,CAAC,IAAI,CAAC,UAE/E,CAAC;AACF,CAAC","sourcesContent":["import { Class, Property, File, getCommentHeader, getInheritanceTree, Context, Interface } from \"../types\";\nimport { GenerateOptions } from \"../api\";\n\nconst typeMaps = {\n \"string\": \"string\",\n \"number\": \"number\",\n \"boolean\": \"boolean\",\n \"int8\": \"number\",\n \"uint8\": \"number\",\n \"int16\": \"number\",\n \"uint16\": \"number\",\n \"int32\": \"number\",\n \"uint32\": \"number\",\n \"int64\": \"number\",\n \"uint64\": \"number\",\n \"float32\": \"number\",\n \"float64\": \"number\",\n}\n\nconst distinct = (value, index, self) => self.indexOf(value) === index;\n\nexport function generate (context: Context, options: GenerateOptions): File[] {\n return [\n ...context.classes.map(structure => ({\n name: structure.name + \".ts\",\n content: generateClass(structure, options.namespace, context.classes)\n })),\n ...context.interfaces.map(structure => ({\n name: structure.name + \".ts\",\n content: generateInterface(structure, options.namespace, context.classes),\n }))\n ];\n}\n\nfunction generateClass(klass: Class, namespace: string, allClasses: Class[]) {\n const allRefs: Property[] = [];\n klass.properties.forEach(property => {\n let type = property.type;\n\n // keep all refs list\n if ((type === \"ref\" || type === \"array\" || type === \"map\" || type === \"set\")) {\n allRefs.push(property);\n }\n });\n\n return `${getCommentHeader()}\n\nimport { Schema, type, ArraySchema, MapSchema, SetSchema, DataChange } from '@colyseus/schema';\n${allRefs.\n filter(ref => ref.childType && typeMaps[ref.childType] === undefined).\n map(ref => ref.childType).\n concat(getInheritanceTree(klass, allClasses, false).map(klass => klass.name)).\n filter(distinct).\n map(childType => `import { ${childType} } from './${childType}'`).\n join(\"\\n\")}\n\nexport class ${klass.name} extends ${klass.extends} {\n${klass.properties.map(prop => ` ${generateProperty(prop)}`).join(\"\\n\")}\n}\n`;\n}\n\nfunction generateProperty(prop: Property) {\n let langType: string;\n let initializer = \"\";\n let typeArgs: string;\n\n if (prop.childType) {\n const isUpcaseFirst = prop.childType.match(/^[A-Z]/);\n\n if (isUpcaseFirst) {\n typeArgs += `, ${prop.childType}`;\n\n } else {\n typeArgs += `, \"${prop.childType}\"`;\n }\n\n if(prop.type === \"ref\") {\n langType = `${prop.childType}`;\n initializer = `new ${prop.childType}()`;\n typeArgs = `${prop.childType}`;\n\n } else if(prop.type === \"array\") {\n langType = (isUpcaseFirst)\n ? `ArraySchema<${prop.childType}>`\n : `ArraySchema<${typeMaps[prop.childType]}>`;\n initializer = `new ${langType}()`;\n typeArgs = (isUpcaseFirst)\n ? `[ ${prop.childType} ]`\n : `[ \"${prop.childType}\" ]`;\n\n } else if(prop.type === \"map\") {\n langType = (isUpcaseFirst)\n ? `MapSchema<${prop.childType}>`\n : `MapSchema<${typeMaps[prop.childType]}>`;\n initializer = `new ${langType}()`;\n typeArgs = (isUpcaseFirst)\n ? `{ map: ${prop.childType} }`\n : `{ map: \"${prop.childType}\" }`;\n } else if (prop.type === \"set\") {\n langType = (isUpcaseFirst)\n ? `SetSchema<${prop.childType}>`\n : `SetSchema<${typeMaps[prop.childType]}>`;\n initializer = `new ${langType}()`;\n typeArgs = (isUpcaseFirst)\n ? `{ set: ${prop.childType} }`\n : `{ set: \"${prop.childType}\" }`;\n }\n\n } else {\n langType = typeMaps[prop.type];\n typeArgs = `\"${prop.type}\"`;\n }\n\n // TS1263: \"Declarations with initializers cannot also have definite assignment assertions\"\n const definiteAssertion = initializer ? \"\" : \"!\";\n\n return `@type(${typeArgs}) public ${prop.name}${definiteAssertion}: ${langType}${(initializer) ? ` = ${initializer}` : \"\"};`\n}\n\n\nfunction generateInterface(structure: Interface, namespace: string, allClasses: Class[]) {\n return `${getCommentHeader()}\n\nexport interface ${structure.name} {\n${structure.properties.map(prop => ` ${prop.name}: ${prop.type};`).join(\"\\n\")}\n}\n`;\n}\n"]}
|
package/lib/codegen/types.js
CHANGED
|
@@ -19,6 +19,13 @@ var Context = /** @class */ (function () {
|
|
|
19
19
|
return {
|
|
20
20
|
classes: this.classes.filter(function (klass) {
|
|
21
21
|
if (_this.isSchemaClass(klass)) {
|
|
22
|
+
//
|
|
23
|
+
// FIXME: see "this.isSchemaClass()" comments.
|
|
24
|
+
//
|
|
25
|
+
// When extending from `schema.Schema`, it is required to
|
|
26
|
+
// normalize as "Schema" for code generation.
|
|
27
|
+
//
|
|
28
|
+
klass.extends = "Schema";
|
|
22
29
|
return true;
|
|
23
30
|
}
|
|
24
31
|
else {
|
|
@@ -50,7 +57,13 @@ var Context = /** @class */ (function () {
|
|
|
50
57
|
var isSchema = false;
|
|
51
58
|
var currentClass = klass;
|
|
52
59
|
while (!isSchema && currentClass) {
|
|
53
|
-
|
|
60
|
+
//
|
|
61
|
+
// TODO: ideally we should check for actual @colsyeus/schema module
|
|
62
|
+
// reference rather than arbitrary strings.
|
|
63
|
+
//
|
|
64
|
+
isSchema = (currentClass.extends === "Schema" ||
|
|
65
|
+
currentClass.extends === "schema.Schema" ||
|
|
66
|
+
currentClass.extends === "Schema.Schema");
|
|
54
67
|
currentClass = this.getParentClass(currentClass);
|
|
55
68
|
}
|
|
56
69
|
return isSchema;
|
package/lib/codegen/types.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/codegen/types.ts"],"names":[],"mappings":";;;AAAA,uBAAyB;AAEzB,IAAM,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC,YAAY,CAAC,SAAS,GAAG,qBAAqB,CAAC,CAAC,QAAQ,EAAE,CAAC,CAAC,OAAO,CAAC;AAClG,IAAM,cAAc,GAAG,mJAIY,OAAO,OACzC,CAAC;AAEF,SAAgB,gBAAgB,CAAC,iBAAgC;IAAhC,kCAAA,EAAA,wBAAgC;IAC7D,OAAO,KAAG,cAAc,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,UAAA,IAAI,IAAI,OAAG,iBAAiB,SAAI,IAAM,EAA9B,CAA8B,CAAC,CAAC,IAAI,CAAC,IAAI,CAAG,CAAC;AAClG,CAAC;AAFD,4CAEC;AAED;IAAA;Q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,4BAAA,EAAA,kBAA2B;IAC7F,IAAI,YAAY,GAAG,KAAK,CAAC;IACzB,IAAI,eAAe,GAAY,EAAE,CAAC;IAElC,IAAI,WAAW,EAAE;QACb,eAAe,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;KACtC;IAED,OAAO,YAAY,CAAC,OAAO,KAAK,QAAQ,EAAE;QACtC,YAAY,GAAG,UAAU,CAAC,IAAI,CAAC,UAAA,KAAK,IAAI,OAAA,KAAK,CAAC,IAAI,IAAI,YAAY,CAAC,OAAO,EAAlC,CAAkC,CAAC,CAAC;QAC5E,eAAe,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;KACtC;IAED,OAAO,eAAe,CAAC;AAC3B,CAAC;AAdD,gDAcC","sourcesContent":["import * as fs from \"fs\";\n\nconst VERSION = JSON.parse(fs.readFileSync(__dirname + \"/../../package.json\").toString()).version;\nconst COMMENT_HEADER = `\nTHIS FILE HAS BEEN GENERATED AUTOMATICALLY\nDO NOT CHANGE IT MANUALLY UNLESS YOU KNOW WHAT YOU'RE DOING\n\nGENERATED USING @colyseus/schema ${VERSION}\n`;\n\nexport function getCommentHeader(singleLineComment: string = \"//\") {\n return `${COMMENT_HEADER.split(\"\\n\").map(line => `${singleLineComment} ${line}`).join(\"\\n\")}`;\n}\n\nexport class Context {\n classes: Class[] = [];\n interfaces: Interface[] = [];\n\n getStructures() {\n return {\n classes: this.classes.filter(klass => {\n if (this.isSchemaClass(klass)) {\n //\n // FIXME: see \"this.isSchemaClass()\" comments.\n //\n // When extending from `schema.Schema`, it is required to\n // normalize as \"Schema\" for code generation.\n //\n klass.extends = \"Schema\";\n return true;\n\n } else {\n let parentClass = klass;\n while (parentClass = this.getParentClass(parentClass)) {\n if (this.isSchemaClass(parentClass)) {\n return true;\n }\n }\n }\n return false;\n }),\n interfaces: this.interfaces,\n };\n }\n\n addStructure(structure: IStructure) {\n structure.context = this;\n\n if (structure instanceof Class) {\n this.classes.push(structure);\n\n } else if (structure instanceof Interface) {\n this.interfaces.push(structure);\n }\n }\n\n private getParentClass(klass: Class) {\n return this.classes.find(c => c.name === klass.extends);\n }\n\n private isSchemaClass(klass: Class) {\n let isSchema: boolean = false;\n\n let currentClass = klass;\n while (!isSchema && currentClass) {\n //\n // TODO: ideally we should check for actual @colsyeus/schema module\n // reference rather than arbitrary strings.\n //\n isSchema = (\n currentClass.extends === \"Schema\" ||\n currentClass.extends === \"schema.Schema\" ||\n currentClass.extends === \"Schema.Schema\"\n );\n\n currentClass = this.getParentClass(currentClass);\n }\n\n return isSchema\n }\n}\n\nexport interface IStructure {\n context: Context;\n name: string;\n properties: Property[];\n addProperty(property: Property);\n}\n\nexport class Interface implements IStructure {\n context: Context;\n name: string;\n properties: Property[] = [];\n\n addProperty(property: Property) {\n if (property.type.indexOf(\"[]\") >= 0) {\n // is array!\n property.childType = property.type.match(/([^\\[]+)/i)[1];\n property.type = \"array\";\n this.properties.push(property);\n\n } else {\n this.properties.push(property);\n }\n }\n}\n\nexport class Class implements IStructure {\n context: Context;\n name: string;\n properties: Property[] = [];\n extends: string;\n\n addProperty(property: Property) {\n property.index = this.properties.length;\n this.properties.push(property);\n }\n\n postProcessing() {\n /**\n * Ensure the proprierties `index` are correct using inheritance\n */\n let parentKlass: Class = this;\n\n while (\n parentKlass &&\n (parentKlass = this.context.classes.find(k => k.name === parentKlass.extends))\n ) {\n this.properties.forEach(prop => {\n prop.index += parentKlass.properties.length;\n });\n }\n }\n}\n\nexport class Property {\n index: number;\n name: string;\n type: string;\n childType: string;\n deprecated?: boolean;\n}\n\nexport interface File {\n name: string\n content: string;\n}\n\nexport function getInheritanceTree(klass: Class, allClasses: Class[], includeSelf: boolean = true) {\n let currentClass = klass;\n let inheritanceTree: Class[] = [];\n\n if (includeSelf) {\n inheritanceTree.push(currentClass);\n }\n\n while (currentClass.extends !== \"Schema\") {\n currentClass = allClasses.find(klass => klass.name == currentClass.extends);\n inheritanceTree.push(currentClass);\n }\n\n return inheritanceTree;\n}\n"]}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getType = exports.registerType = void 0;
|
|
4
|
+
var registeredTypes = {};
|
|
5
|
+
function registerType(identifier, definition) {
|
|
6
|
+
registeredTypes[identifier] = definition;
|
|
7
|
+
}
|
|
8
|
+
exports.registerType = registerType;
|
|
9
|
+
function getType(identifier) {
|
|
10
|
+
return registeredTypes[identifier];
|
|
11
|
+
}
|
|
12
|
+
exports.getType = getType;
|
|
13
|
+
//# sourceMappingURL=typeRegistry.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"typeRegistry.js","sourceRoot":"","sources":["../../src/types/typeRegistry.ts"],"names":[],"mappings":";;;AAUA,IAAM,eAAe,GAA4C,EAAE,CAAC;AAEpE,SAAgB,YAAY,CAAC,UAAkB,EAAE,UAA0B;IACvE,eAAe,CAAC,UAAU,CAAC,GAAG,UAAU,CAAC;AAC7C,CAAC;AAFD,oCAEC;AAED,SAAgB,OAAO,CAAC,UAAkB;IACtC,OAAO,eAAe,CAAC,UAAU,CAAC,CAAC;AACvC,CAAC;AAFD,0BAEC","sourcesContent":["export interface TypeDefinition {\n constructor: any,\n\n // //\n // // TODO: deprecate proxy on next version\n // // the proxy is used for compatibility with versions <1.0.0 of @colyseus/schema\n // //\n // getProxy?: any,\n}\n\nconst registeredTypes: {[identifier: string] : TypeDefinition} = {};\n\nexport function registerType(identifier: string, definition: TypeDefinition) {\n registeredTypes[identifier] = definition;\n}\n\nexport function getType(identifier: string): TypeDefinition {\n return registeredTypes[identifier];\n}\n"]}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { CollectionSchema, DataChange } from "..";
|
|
2
|
+
import { OPERATION } from "../spec";
|
|
3
|
+
export declare function addCallback($callbacks: {
|
|
4
|
+
[op: number]: Function[];
|
|
5
|
+
}, op: OPERATION, callback: (item: any, key: any) => void, existing?: {
|
|
6
|
+
forEach(callback: (item: any, key: any) => void): void;
|
|
7
|
+
}): () => boolean;
|
|
8
|
+
export declare function removeChildRefs(this: CollectionSchema, changes: DataChange[]): void;
|
|
9
|
+
export declare function spliceOne(arr: any[], index: number): boolean;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.spliceOne = exports.removeChildRefs = exports.addCallback = void 0;
|
|
4
|
+
var spec_1 = require("../spec");
|
|
5
|
+
function addCallback($callbacks, op, callback, existing) {
|
|
6
|
+
// initialize list of callbacks
|
|
7
|
+
if (!$callbacks[op]) {
|
|
8
|
+
$callbacks[op] = [];
|
|
9
|
+
}
|
|
10
|
+
$callbacks[op].push(callback);
|
|
11
|
+
//
|
|
12
|
+
// Trigger callback for existing elements
|
|
13
|
+
// - OPERATION.ADD
|
|
14
|
+
// - OPERATION.REPLACE
|
|
15
|
+
//
|
|
16
|
+
existing === null || existing === void 0 ? void 0 : existing.forEach(function (item, key) { return callback(item, key); });
|
|
17
|
+
return function () { return spliceOne($callbacks[op], $callbacks[op].indexOf(callback)); };
|
|
18
|
+
}
|
|
19
|
+
exports.addCallback = addCallback;
|
|
20
|
+
function removeChildRefs(changes) {
|
|
21
|
+
var _this = this;
|
|
22
|
+
var needRemoveRef = (typeof (this.$changes.getType()) !== "string");
|
|
23
|
+
this.$items.forEach(function (item, key) {
|
|
24
|
+
changes.push({
|
|
25
|
+
refId: _this.$changes.refId,
|
|
26
|
+
op: spec_1.OPERATION.DELETE,
|
|
27
|
+
field: key,
|
|
28
|
+
value: undefined,
|
|
29
|
+
previousValue: item
|
|
30
|
+
});
|
|
31
|
+
if (needRemoveRef) {
|
|
32
|
+
_this.$changes.root.removeRef(item['$changes'].refId);
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
exports.removeChildRefs = removeChildRefs;
|
|
37
|
+
function spliceOne(arr, index) {
|
|
38
|
+
// manually splice an array
|
|
39
|
+
if (index === -1 || index >= arr.length) {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
var len = arr.length - 1;
|
|
43
|
+
for (var i = index; i < len; i++) {
|
|
44
|
+
arr[i] = arr[i + 1];
|
|
45
|
+
}
|
|
46
|
+
arr.length = len;
|
|
47
|
+
return true;
|
|
48
|
+
}
|
|
49
|
+
exports.spliceOne = spliceOne;
|
|
50
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/types/utils.ts"],"names":[],"mappings":";;;AACA,gCAAoC;AAEpC,SAAgB,WAAW,CACvB,UAAwC,EACxC,EAAa,EACb,QAAuC,EACvC,QAAsE;IAEtE,+BAA+B;IAC/B,IAAI,CAAC,UAAU,CAAC,EAAE,CAAC,EAAE;QACjB,UAAU,CAAC,EAAE,CAAC,GAAG,EAAE,CAAC;KACvB;IAED,UAAU,CAAC,EAAE,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;IAE9B,EAAE;IACF,yCAAyC;IACzC,kBAAkB;IAClB,sBAAsB;IACtB,EAAE;IACF,QAAQ,aAAR,QAAQ,uBAAR,QAAQ,CAAE,OAAO,CAAC,UAAC,IAAI,EAAE,GAAG,IAAK,OAAA,QAAQ,CAAC,IAAI,EAAE,GAAG,CAAC,EAAnB,CAAmB,CAAC,CAAC;IAEtD,OAAO,cAAM,OAAA,SAAS,CAAC,UAAU,CAAC,EAAE,CAAC,EAAE,UAAU,CAAC,EAAE,CAAC,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,EAA3D,CAA2D,CAAC;AAC7E,CAAC;AArBD,kCAqBC;AAGD,SAAgB,eAAe,CAAyB,OAAqB;IAA7E,iBAgBC;IAfG,IAAM,aAAa,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,QAAQ,CAAC,OAAO,EAAE,CAAC,KAAK,QAAQ,CAAC,CAAC;IAEtE,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,UAAC,IAAS,EAAE,GAAQ;QACpC,OAAO,CAAC,IAAI,CAAC;YACT,KAAK,EAAE,KAAI,CAAC,QAAQ,CAAC,KAAK;YAC1B,EAAE,EAAE,gBAAS,CAAC,MAAM;YACpB,KAAK,EAAE,GAAG;YACV,KAAK,EAAE,SAAS;YAChB,aAAa,EAAE,IAAI;SACtB,CAAC,CAAC;QAEH,IAAI,aAAa,EAAE;YACf,KAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC,KAAK,CAAC,CAAC;SACxD;IACL,CAAC,CAAC,CAAC;AACP,CAAC;AAhBD,0CAgBC;AAGD,SAAgB,SAAS,CAAC,GAAU,EAAE,KAAa;IAC/C,2BAA2B;IAC3B,IAAI,KAAK,KAAK,CAAC,CAAC,IAAI,KAAK,IAAI,GAAG,CAAC,MAAM,EAAE;QACrC,OAAO,KAAK,CAAC;KAChB;IAED,IAAM,GAAG,GAAG,GAAG,CAAC,MAAM,GAAG,CAAC,CAAC;IAE3B,KAAK,IAAI,CAAC,GAAG,KAAK,EAAE,CAAC,GAAG,GAAG,EAAE,CAAC,EAAE,EAAE;QAC9B,GAAG,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;KACvB;IAED,GAAG,CAAC,MAAM,GAAG,GAAG,CAAC;IAEjB,OAAO,IAAI,CAAC;AAChB,CAAC;AAfD,8BAeC","sourcesContent":["import { CollectionSchema, DataChange } from \"..\";\nimport { OPERATION } from \"../spec\";\n\nexport function addCallback(\n $callbacks: { [op: number]: Function[] },\n op: OPERATION,\n callback: (item: any, key: any) => void,\n existing?: { forEach(callback: (item: any, key: any) => void): void; }\n) {\n // initialize list of callbacks\n if (!$callbacks[op]) {\n $callbacks[op] = [];\n }\n\n $callbacks[op].push(callback);\n\n //\n // Trigger callback for existing elements\n // - OPERATION.ADD\n // - OPERATION.REPLACE\n //\n existing?.forEach((item, key) => callback(item, key));\n\n return () => spliceOne($callbacks[op], $callbacks[op].indexOf(callback));\n}\n\n\nexport function removeChildRefs(this: CollectionSchema, changes: DataChange[]) {\n const needRemoveRef = (typeof (this.$changes.getType()) !== \"string\");\n\n this.$items.forEach((item: any, key: any) => {\n changes.push({\n refId: this.$changes.refId,\n op: OPERATION.DELETE,\n field: key,\n value: undefined,\n previousValue: item\n });\n\n if (needRemoveRef) {\n this.$changes.root.removeRef(item['$changes'].refId);\n }\n });\n}\n\n\nexport function spliceOne(arr: any[], index: number): boolean {\n // manually splice an array\n if (index === -1 || index >= arr.length) {\n return false;\n }\n\n const len = arr.length - 1;\n\n for (let i = index; i < len; i++) {\n arr[i] = arr[i + 1];\n }\n\n arr.length = len;\n\n return true;\n}"]}
|