@colyseus/schema 1.1.0-alpha.3 → 2.0.4
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 +1 -4
- package/build/cjs/index.js +1200 -1002
- package/build/cjs/index.js.map +1 -1
- package/build/esm/index.mjs +355 -311
- package/build/esm/index.mjs.map +1 -1
- package/build/umd/index.js +376 -335
- package/lib/Reflection.js +23 -13
- package/lib/Reflection.js.map +1 -1
- package/lib/Schema.d.ts +24 -16
- package/lib/Schema.js +123 -165
- package/lib/Schema.js.map +1 -1
- package/lib/annotations.d.ts +23 -5
- package/lib/annotations.js +62 -14
- package/lib/annotations.js.map +1 -1
- package/lib/changes/ChangeTree.d.ts +4 -16
- package/lib/changes/ChangeTree.js +1 -72
- package/lib/changes/ChangeTree.js.map +1 -1
- 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/csharp.js +39 -17
- package/lib/codegen/languages/csharp.js.map +1 -1
- package/lib/codegen/languages/ts.js +11 -2
- package/lib/codegen/languages/ts.js.map +1 -1
- package/lib/codegen/parser.js +3 -1
- package/lib/codegen/parser.js.map +1 -1
- package/lib/codegen/types.js +14 -1
- package/lib/codegen/types.js.map +1 -1
- package/lib/index.d.ts +1 -1
- package/lib/index.js +6 -6
- package/lib/index.js.map +1 -1
- package/lib/types/ArraySchema.d.ts +8 -5
- package/lib/types/ArraySchema.js +22 -19
- package/lib/types/ArraySchema.js.map +1 -1
- package/lib/types/CollectionSchema.d.ts +8 -5
- package/lib/types/CollectionSchema.js +17 -11
- package/lib/types/CollectionSchema.js.map +1 -1
- package/lib/types/MapSchema.d.ts +8 -5
- package/lib/types/MapSchema.js +20 -11
- package/lib/types/MapSchema.js.map +1 -1
- package/lib/types/SetSchema.d.ts +8 -5
- package/lib/types/SetSchema.js +22 -14
- package/lib/types/SetSchema.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 +3 -8
package/lib/Reflection.js
CHANGED
|
@@ -20,13 +20,16 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
|
|
|
20
20
|
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
21
21
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
22
22
|
};
|
|
23
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
24
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
25
|
+
};
|
|
23
26
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
24
27
|
exports.Reflection = exports.ReflectionType = exports.ReflectionField = void 0;
|
|
25
28
|
var annotations_1 = require("./annotations");
|
|
26
29
|
var Schema_1 = require("./Schema");
|
|
27
30
|
var ArraySchema_1 = require("./types/ArraySchema");
|
|
28
|
-
var
|
|
29
|
-
var reflectionContext = new annotations_1.Context();
|
|
31
|
+
var typeRegistry_1 = require("./types/typeRegistry");
|
|
32
|
+
var reflectionContext = { context: new annotations_1.Context() };
|
|
30
33
|
/**
|
|
31
34
|
* Reflection
|
|
32
35
|
*/
|
|
@@ -36,13 +39,16 @@ var ReflectionField = /** @class */ (function (_super) {
|
|
|
36
39
|
return _super !== null && _super.apply(this, arguments) || this;
|
|
37
40
|
}
|
|
38
41
|
__decorate([
|
|
39
|
-
annotations_1.type("string", reflectionContext)
|
|
42
|
+
annotations_1.type("string", reflectionContext),
|
|
43
|
+
__metadata("design:type", String)
|
|
40
44
|
], ReflectionField.prototype, "name", void 0);
|
|
41
45
|
__decorate([
|
|
42
|
-
annotations_1.type("string", reflectionContext)
|
|
46
|
+
annotations_1.type("string", reflectionContext),
|
|
47
|
+
__metadata("design:type", String)
|
|
43
48
|
], ReflectionField.prototype, "type", void 0);
|
|
44
49
|
__decorate([
|
|
45
|
-
annotations_1.type("number", reflectionContext)
|
|
50
|
+
annotations_1.type("number", reflectionContext),
|
|
51
|
+
__metadata("design:type", Number)
|
|
46
52
|
], ReflectionField.prototype, "referencedType", void 0);
|
|
47
53
|
return ReflectionField;
|
|
48
54
|
}(Schema_1.Schema));
|
|
@@ -55,10 +61,12 @@ var ReflectionType = /** @class */ (function (_super) {
|
|
|
55
61
|
return _this;
|
|
56
62
|
}
|
|
57
63
|
__decorate([
|
|
58
|
-
annotations_1.type("number", reflectionContext)
|
|
64
|
+
annotations_1.type("number", reflectionContext),
|
|
65
|
+
__metadata("design:type", Number)
|
|
59
66
|
], ReflectionType.prototype, "id", void 0);
|
|
60
67
|
__decorate([
|
|
61
|
-
annotations_1.type([ReflectionField], reflectionContext)
|
|
68
|
+
annotations_1.type([ReflectionField], reflectionContext),
|
|
69
|
+
__metadata("design:type", ArraySchema_1.ArraySchema)
|
|
62
70
|
], ReflectionType.prototype, "fields", void 0);
|
|
63
71
|
return ReflectionType;
|
|
64
72
|
}(Schema_1.Schema));
|
|
@@ -149,14 +157,14 @@ var Reflection = /** @class */ (function (_super) {
|
|
|
149
157
|
refType = typeInfo[1];
|
|
150
158
|
}
|
|
151
159
|
if (fieldType === "ref") {
|
|
152
|
-
annotations_1.type(refType, context)(schemaType.prototype, field.name);
|
|
160
|
+
annotations_1.type(refType, { context: context })(schemaType.prototype, field.name);
|
|
153
161
|
}
|
|
154
162
|
else {
|
|
155
|
-
annotations_1.type((_a = {}, _a[fieldType] = refType, _a), context)(schemaType.prototype, field.name);
|
|
163
|
+
annotations_1.type((_a = {}, _a[fieldType] = refType, _a), { context: context })(schemaType.prototype, field.name);
|
|
156
164
|
}
|
|
157
165
|
}
|
|
158
166
|
else {
|
|
159
|
-
annotations_1.type(field.type, context)(schemaType.prototype, field.name);
|
|
167
|
+
annotations_1.type(field.type, { context: context })(schemaType.prototype, field.name);
|
|
160
168
|
}
|
|
161
169
|
});
|
|
162
170
|
});
|
|
@@ -171,16 +179,18 @@ var Reflection = /** @class */ (function (_super) {
|
|
|
171
179
|
if (typeof (fieldType) !== "string") {
|
|
172
180
|
rootInstance[fieldName] = (typeof (fieldType) === "function")
|
|
173
181
|
? new fieldType() // is a schema reference
|
|
174
|
-
: new (
|
|
182
|
+
: new (typeRegistry_1.getType(Object.keys(fieldType)[0])).constructor(); // is a "collection"
|
|
175
183
|
}
|
|
176
184
|
}
|
|
177
185
|
return rootInstance;
|
|
178
186
|
};
|
|
179
187
|
__decorate([
|
|
180
|
-
annotations_1.type([ReflectionType], reflectionContext)
|
|
188
|
+
annotations_1.type([ReflectionType], reflectionContext),
|
|
189
|
+
__metadata("design:type", ArraySchema_1.ArraySchema)
|
|
181
190
|
], Reflection.prototype, "types", void 0);
|
|
182
191
|
__decorate([
|
|
183
|
-
annotations_1.type("number", reflectionContext)
|
|
192
|
+
annotations_1.type("number", reflectionContext),
|
|
193
|
+
__metadata("design:type", Number)
|
|
184
194
|
], Reflection.prototype, "rootType", void 0);
|
|
185
195
|
return Reflection;
|
|
186
196
|
}(Schema_1.Schema));
|
package/lib/Reflection.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Reflection.js","sourceRoot":"","sources":["../src/Reflection.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAAA,6CAA6E;AAC7E,mCAAkC;AAClC,mDAAkD;AAClD,iCAAkC;AAGlC,IAAM,iBAAiB,GAAG,IAAI,qBAAO,EAAE,CAAC;AAExC;;GAEG;AACH;IAAqC,mCAAM;IAA3C;;IASA,CAAC;IAPG;QADC,kBAAI,CAAC,QAAQ,EAAE,iBAAiB,CAAC;iDACrB;IAGb;QADC,kBAAI,CAAC,QAAQ,EAAE,iBAAiB,CAAC;iDACrB;IAGb;QADC,kBAAI,CAAC,QAAQ,EA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nimport { ArraySchema } from \"./types/ArraySchema\";\nimport { getType } from \"./types\";\nimport { Iterator } from \"./encoding/decode\";\n\nconst reflectionContext = new Context();\n\n/**\n * Reflection\n */\nexport class ReflectionField extends Schema {\n @type(\"string\", reflectionContext)\n name: string;\n\n @type(\"string\", reflectionContext)\n type: string;\n\n @type(\"number\", reflectionContext)\n referencedType: number;\n}\n\nexport class ReflectionType extends Schema {\n @type(\"number\", reflectionContext)\n id: number;\n\n @type([ ReflectionField ], reflectionContext)\n fields: ArraySchema<ReflectionField> = new ArraySchema<ReflectionField>();\n}\n\nexport class Reflection extends Schema {\n @type([ ReflectionType ], reflectionContext)\n types: ArraySchema<ReflectionType> = new ArraySchema<ReflectionType>();\n\n @type(\"number\", reflectionContext)\n rootType: number;\n\n static encode (instance: Schema) {\n const rootSchemaType = instance.constructor as typeof Schema;\n\n const reflection = new Reflection();\n reflection.rootType = rootSchemaType._typeid;\n\n const buildType = (currentType: ReflectionType, schema: any) => {\n for (let fieldName in schema) {\n const field = new ReflectionField();\n field.name = fieldName;\n\n let fieldType: string;\n\n if (typeof (schema[fieldName]) === \"string\") {\n fieldType = schema[fieldName];\n\n } else {\n const type = schema[fieldName];\n let childTypeSchema: typeof Schema;\n\n //\n // TODO: refactor below.\n //\n if (Schema.is(type)) {\n fieldType = \"ref\";\n childTypeSchema = schema[fieldName];\n\n } else {\n fieldType = Object.keys(type)[0];\n\n if (typeof(type[fieldType]) === \"string\") {\n fieldType += \":\" + type[fieldType]; // array:string\n\n } else {\n childTypeSchema = type[fieldType];\n }\n }\n\n field.referencedType = (childTypeSchema)\n ? childTypeSchema._typeid\n : -1;\n }\n\n field.type = fieldType;\n currentType.fields.push(field);\n }\n\n reflection.types.push(currentType);\n }\n\n const types = rootSchemaType._context.types;\n for (let typeid in types) {\n const type = new ReflectionType();\n type.id = Number(typeid);\n buildType(type, types[typeid]._definition.schema);\n }\n\n return reflection.encodeAll();\n }\n\n static decode<T extends Schema = Schema>(bytes: number[], it?: Iterator): T {\n const context = new Context();\n\n const reflection = new Reflection();\n reflection.decode(bytes, it);\n\n const schemaTypes = reflection.types.reduce((types, reflectionType) => {\n const schema: typeof Schema = class _ extends Schema {};\n const typeid = reflectionType.id;\n types[typeid] = schema\n context.add(schema, typeid);\n return types;\n }, {});\n\n reflection.types.forEach((reflectionType) => {\n const schemaType = schemaTypes[reflectionType.id];\n\n reflectionType.fields.forEach(field => {\n if (field.referencedType !== undefined) {\n let fieldType = field.type;\n let refType = schemaTypes[field.referencedType];\n\n // map or array of primitive type (-1)\n if (!refType) {\n const typeInfo = field.type.split(\":\");\n fieldType = typeInfo[0];\n refType = typeInfo[1];\n }\n\n if (fieldType === \"ref\") {\n type(refType, context)(schemaType.prototype, field.name);\n\n } else {\n type({ [fieldType]: refType } as DefinitionType, context)(schemaType.prototype, field.name);\n }\n\n } else {\n type(field.type as PrimitiveType, context)(schemaType.prototype, field.name);\n }\n });\n })\n\n const rootType: any = schemaTypes[reflection.rootType];\n const rootInstance = new rootType();\n\n /**\n * auto-initialize referenced types on root type\n * to allow registering listeners immediatelly on client-side\n */\n for (let fieldName in rootType._definition.schema) {\n const fieldType = rootType._definition.schema[fieldName];\n\n if (typeof(fieldType) !== \"string\") {\n rootInstance[fieldName] = (typeof (fieldType) === \"function\")\n ? new (fieldType as any)() // is a schema reference\n : new (getType(Object.keys(fieldType)[0])).constructor(); // is a \"collection\"\n }\n }\n\n return rootInstance;\n }\n}"]}
|
|
1
|
+
{"version":3,"file":"Reflection.js","sourceRoot":"","sources":["../src/Reflection.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,6CAA6E;AAC7E,mCAAkC;AAClC,mDAAkD;AAClD,qDAA+C;AAG/C,IAAM,iBAAiB,GAAG,EAAE,OAAO,EAAE,IAAI,qBAAO,EAAE,EAAE,CAAC;AAErD;;GAEG;AACH;IAAqC,mCAAM;IAA3C;;IASA,CAAC;IAPG;QADC,kBAAI,CAAC,QAAQ,EAAE,iBAAiB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;IAGvE;QADC,kBAAI,CAAC,QAAQ,EAAE,iBAAiB,CAAC;;gDACjB;IA2HrB,iBAAC;CAAA,AAhID,CAAgC,eAAM,GAgIrC;AAhIY,gCAAU","sourcesContent":["import { type, PrimitiveType, Context, DefinitionType } from \"./annotations\";\nimport { Schema } from \"./Schema\";\nimport { ArraySchema } from \"./types/ArraySchema\";\nimport { getType } from \"./types/typeRegistry\";\nimport { Iterator } from \"./encoding/decode\";\n\nconst reflectionContext = { context: new Context() };\n\n/**\n * Reflection\n */\nexport class ReflectionField extends Schema {\n @type(\"string\", reflectionContext)\n name: string;\n\n @type(\"string\", reflectionContext)\n type: string;\n\n @type(\"number\", reflectionContext)\n referencedType: number;\n}\n\nexport class ReflectionType extends Schema {\n @type(\"number\", reflectionContext)\n id: number;\n\n @type([ ReflectionField ], reflectionContext)\n fields: ArraySchema<ReflectionField> = new ArraySchema<ReflectionField>();\n}\n\nexport class Reflection extends Schema {\n @type([ ReflectionType ], reflectionContext)\n types: ArraySchema<ReflectionType> = new ArraySchema<ReflectionType>();\n\n @type(\"number\", reflectionContext)\n rootType: number;\n\n static encode (instance: Schema) {\n const rootSchemaType = instance.constructor as typeof Schema;\n\n const reflection = new Reflection();\n reflection.rootType = rootSchemaType._typeid;\n\n const buildType = (currentType: ReflectionType, schema: any) => {\n for (let fieldName in schema) {\n const field = new ReflectionField();\n field.name = fieldName;\n\n let fieldType: string;\n\n if (typeof (schema[fieldName]) === \"string\") {\n fieldType = schema[fieldName];\n\n } else {\n const type = schema[fieldName];\n let childTypeSchema: typeof Schema;\n\n //\n // TODO: refactor below.\n //\n if (Schema.is(type)) {\n fieldType = \"ref\";\n childTypeSchema = schema[fieldName];\n\n } else {\n fieldType = Object.keys(type)[0];\n\n if (typeof(type[fieldType]) === \"string\") {\n fieldType += \":\" + type[fieldType]; // array:string\n\n } else {\n childTypeSchema = type[fieldType];\n }\n }\n\n field.referencedType = (childTypeSchema)\n ? childTypeSchema._typeid\n : -1;\n }\n\n field.type = fieldType;\n currentType.fields.push(field);\n }\n\n reflection.types.push(currentType);\n }\n\n const types = rootSchemaType._context.types;\n for (let typeid in types) {\n const type = new ReflectionType();\n type.id = Number(typeid);\n buildType(type, types[typeid]._definition.schema);\n }\n\n return reflection.encodeAll();\n }\n\n static decode<T extends Schema = Schema>(bytes: number[], it?: Iterator): T {\n const context = new Context();\n\n const reflection = new Reflection();\n reflection.decode(bytes, it);\n\n const schemaTypes = reflection.types.reduce((types, reflectionType) => {\n const schema: typeof Schema = class _ extends Schema {};\n const typeid = reflectionType.id;\n types[typeid] = schema\n context.add(schema, typeid);\n return types;\n }, {});\n\n reflection.types.forEach((reflectionType) => {\n const schemaType = schemaTypes[reflectionType.id];\n\n reflectionType.fields.forEach(field => {\n if (field.referencedType !== undefined) {\n let fieldType = field.type;\n let refType = schemaTypes[field.referencedType];\n\n // map or array of primitive type (-1)\n if (!refType) {\n const typeInfo = field.type.split(\":\");\n fieldType = typeInfo[0];\n refType = typeInfo[1];\n }\n\n if (fieldType === \"ref\") {\n type(refType, { context })(schemaType.prototype, field.name);\n\n } else {\n type({ [fieldType]: refType } as DefinitionType, { context })(schemaType.prototype, field.name);\n }\n\n } else {\n type(field.type as PrimitiveType, { context })(schemaType.prototype, field.name);\n }\n });\n })\n\n const rootType: any = schemaTypes[reflection.rootType];\n const rootInstance = new rootType();\n\n /**\n * auto-initialize referenced types on root type\n * to allow registering listeners immediatelly on client-side\n */\n for (let fieldName in rootType._definition.schema) {\n const fieldType = rootType._definition.schema[fieldName];\n\n if (typeof(fieldType) !== \"string\") {\n rootInstance[fieldName] = (typeof (fieldType) === \"function\")\n ? new (fieldType as any)() // is a schema reference\n : new (getType(Object.keys(fieldType)[0])).constructor(); // is a \"collection\"\n }\n }\n\n return rootInstance;\n }\n}"]}
|
package/lib/Schema.d.ts
CHANGED
|
@@ -3,20 +3,23 @@ import { ClientWithSessionId, Context, SchemaDefinition, DefinitionType } from "
|
|
|
3
3
|
import type { Iterator } from "./encoding/decode";
|
|
4
4
|
import { ChangeTree, Ref } from "./changes/ChangeTree";
|
|
5
5
|
import { NonFunctionPropNames } from './types/HelperTypes';
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
export interface DataChange<T = any, F = string> {
|
|
7
|
+
refId: number;
|
|
8
8
|
op: OPERATION;
|
|
9
|
-
field:
|
|
9
|
+
field: F;
|
|
10
10
|
dynamicIndex?: number | string;
|
|
11
11
|
value: T;
|
|
12
12
|
previousValue: T;
|
|
13
13
|
}
|
|
14
|
-
export interface SchemaDecoderCallbacks {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
14
|
+
export interface SchemaDecoderCallbacks<TValue = any, TKey = any> {
|
|
15
|
+
$callbacks: {
|
|
16
|
+
[operation: number]: Array<(item: TValue, key: TKey) => void>;
|
|
17
|
+
};
|
|
18
|
+
onAdd(callback: (item: any, key: any) => void, ignoreExisting?: boolean): () => void;
|
|
19
|
+
onRemove(callback: (item: any, key: any) => void): () => void;
|
|
20
|
+
onChange(callback: (item: any, key: any) => void): () => void;
|
|
18
21
|
clone(decoding?: boolean): SchemaDecoderCallbacks;
|
|
19
|
-
clear(
|
|
22
|
+
clear(changes?: DataChange[]): any;
|
|
20
23
|
decode?(byte: any, it: Iterator): any;
|
|
21
24
|
}
|
|
22
25
|
/**
|
|
@@ -29,23 +32,29 @@ export declare abstract class Schema {
|
|
|
29
32
|
static onError(e: any): void;
|
|
30
33
|
static is(type: DefinitionType): boolean;
|
|
31
34
|
protected $changes: ChangeTree;
|
|
32
|
-
protected $
|
|
33
|
-
[
|
|
35
|
+
protected $callbacks: {
|
|
36
|
+
[op: number]: Array<Function>;
|
|
34
37
|
};
|
|
35
|
-
onChange
|
|
36
|
-
onRemove
|
|
38
|
+
onChange(callback: (changes: DataChange[]) => void): () => void;
|
|
39
|
+
onRemove(callback: () => void): () => void;
|
|
37
40
|
constructor(...args: any[]);
|
|
38
41
|
assign(props: {
|
|
39
42
|
[prop in NonFunctionPropNames<this>]?: this[prop];
|
|
40
43
|
}): this;
|
|
41
44
|
protected get _definition(): SchemaDefinition;
|
|
42
|
-
|
|
43
|
-
|
|
45
|
+
/**
|
|
46
|
+
* (Server-side): Flag a property to be encoded for the next patch.
|
|
47
|
+
* @param instance Schema instance
|
|
48
|
+
* @param property string representing the property name, or number representing the index of the property.
|
|
49
|
+
* @param operation OPERATION to perform (detected automatically)
|
|
50
|
+
*/
|
|
51
|
+
setDirty<K extends NonFunctionPropNames<this>>(property: K | number, operation?: OPERATION): void;
|
|
52
|
+
listen<K extends NonFunctionPropNames<this>>(attr: K, callback: (value: this[K], previousValue: this[K]) => void): () => boolean;
|
|
53
|
+
decode(bytes: number[], it?: Iterator, ref?: Ref): DataChange<any, string>[];
|
|
44
54
|
encode(encodeAll?: boolean, bytes?: number[], useFilters?: boolean): number[];
|
|
45
55
|
encodeAll(useFilters?: boolean): number[];
|
|
46
56
|
applyFilters(client: ClientWithSessionId, encodeAll?: boolean): number[];
|
|
47
57
|
clone(): this;
|
|
48
|
-
triggerAll(): void;
|
|
49
58
|
toJSON(): {};
|
|
50
59
|
discardAllChanges(): void;
|
|
51
60
|
protected getByIndex(index: number): any;
|
|
@@ -53,6 +62,5 @@ export declare abstract class Schema {
|
|
|
53
62
|
private tryEncodeTypeId;
|
|
54
63
|
private getSchemaType;
|
|
55
64
|
private createTypeInstance;
|
|
56
|
-
private _triggerAllFillChanges;
|
|
57
65
|
private _triggerChanges;
|
|
58
66
|
}
|