@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
package/lib/annotations.js
CHANGED
|
@@ -1,33 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __read = (this && this.__read) || function (o, n) {
|
|
3
|
-
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
4
|
-
if (!m) return o;
|
|
5
|
-
var i = m.call(o), r, ar = [], e;
|
|
6
|
-
try {
|
|
7
|
-
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
8
|
-
}
|
|
9
|
-
catch (error) { e = { error: error }; }
|
|
10
|
-
finally {
|
|
11
|
-
try {
|
|
12
|
-
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
13
|
-
}
|
|
14
|
-
finally { if (e) throw e.error; }
|
|
15
|
-
}
|
|
16
|
-
return ar;
|
|
17
|
-
};
|
|
18
|
-
var __spreadArray = (this && this.__spreadArray) || function (to, from) {
|
|
19
|
-
for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
|
|
20
|
-
to[j] = from[i];
|
|
21
|
-
return to;
|
|
22
|
-
};
|
|
23
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
24
3
|
exports.defineTypes = exports.deprecated = exports.filterChildren = exports.filter = exports.type = exports.globalContext = exports.Context = exports.hasFilter = exports.SchemaDefinition = void 0;
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
4
|
+
const Schema_1 = require("./Schema");
|
|
5
|
+
const ArraySchema_1 = require("./types/ArraySchema");
|
|
6
|
+
const MapSchema_1 = require("./types/MapSchema");
|
|
7
|
+
const typeRegistry_1 = require("./types/typeRegistry");
|
|
8
|
+
class SchemaDefinition {
|
|
9
|
+
constructor() {
|
|
31
10
|
//
|
|
32
11
|
// TODO: use a "field" structure combining all these properties per-field.
|
|
33
12
|
//
|
|
@@ -36,8 +15,8 @@ var SchemaDefinition = /** @class */ (function () {
|
|
|
36
15
|
this.deprecated = {};
|
|
37
16
|
this.descriptors = {};
|
|
38
17
|
}
|
|
39
|
-
|
|
40
|
-
|
|
18
|
+
static create(parent) {
|
|
19
|
+
const definition = new SchemaDefinition();
|
|
41
20
|
// support inheritance
|
|
42
21
|
definition.schema = Object.assign({}, parent && parent.schema || {});
|
|
43
22
|
definition.indexes = Object.assign({}, parent && parent.indexes || {});
|
|
@@ -45,19 +24,19 @@ var SchemaDefinition = /** @class */ (function () {
|
|
|
45
24
|
definition.descriptors = Object.assign({}, parent && parent.descriptors || {});
|
|
46
25
|
definition.deprecated = Object.assign({}, parent && parent.deprecated || {});
|
|
47
26
|
return definition;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
|
|
27
|
+
}
|
|
28
|
+
addField(field, type) {
|
|
29
|
+
const index = this.getNextFieldIndex();
|
|
51
30
|
this.fieldsByIndex[index] = field;
|
|
52
31
|
this.indexes[field] = index;
|
|
53
32
|
this.schema[field] = (Array.isArray(type))
|
|
54
33
|
? { array: type[0] }
|
|
55
34
|
: type;
|
|
56
|
-
}
|
|
57
|
-
|
|
35
|
+
}
|
|
36
|
+
hasField(field) {
|
|
58
37
|
return this.indexes[field] !== undefined;
|
|
59
|
-
}
|
|
60
|
-
|
|
38
|
+
}
|
|
39
|
+
addFilter(field, cb) {
|
|
61
40
|
if (!this.filters) {
|
|
62
41
|
this.filters = {};
|
|
63
42
|
this.indexesWithFilters = [];
|
|
@@ -65,11 +44,11 @@ var SchemaDefinition = /** @class */ (function () {
|
|
|
65
44
|
this.filters[this.indexes[field]] = cb;
|
|
66
45
|
this.indexesWithFilters.push(this.indexes[field]);
|
|
67
46
|
return true;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
if (typeRegistry_1.getType(Object.keys(type)[0])) {
|
|
47
|
+
}
|
|
48
|
+
addChildrenFilter(field, cb) {
|
|
49
|
+
const index = this.indexes[field];
|
|
50
|
+
const type = this.schema[field];
|
|
51
|
+
if ((0, typeRegistry_1.getType)(Object.keys(type)[0])) {
|
|
73
52
|
if (!this.childFilters) {
|
|
74
53
|
this.childFilters = {};
|
|
75
54
|
}
|
|
@@ -77,54 +56,50 @@ var SchemaDefinition = /** @class */ (function () {
|
|
|
77
56
|
return true;
|
|
78
57
|
}
|
|
79
58
|
else {
|
|
80
|
-
console.warn(
|
|
59
|
+
console.warn(`@filterChildren: field '${field}' can't have children. Ignoring filter.`);
|
|
81
60
|
}
|
|
82
|
-
}
|
|
83
|
-
|
|
61
|
+
}
|
|
62
|
+
getChildrenFilter(field) {
|
|
84
63
|
return this.childFilters && this.childFilters[this.indexes[field]];
|
|
85
|
-
}
|
|
86
|
-
|
|
64
|
+
}
|
|
65
|
+
getNextFieldIndex() {
|
|
87
66
|
return Object.keys(this.schema || {}).length;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
}());
|
|
67
|
+
}
|
|
68
|
+
}
|
|
91
69
|
exports.SchemaDefinition = SchemaDefinition;
|
|
92
70
|
function hasFilter(klass) {
|
|
93
71
|
return klass._context && klass._context.useFilters;
|
|
94
72
|
}
|
|
95
73
|
exports.hasFilter = hasFilter;
|
|
96
|
-
|
|
97
|
-
|
|
74
|
+
class Context {
|
|
75
|
+
constructor() {
|
|
98
76
|
this.types = {};
|
|
99
77
|
this.schemas = new Map();
|
|
100
78
|
this.useFilters = false;
|
|
101
79
|
}
|
|
102
|
-
|
|
80
|
+
has(schema) {
|
|
103
81
|
return this.schemas.has(schema);
|
|
104
|
-
}
|
|
105
|
-
|
|
82
|
+
}
|
|
83
|
+
get(typeid) {
|
|
106
84
|
return this.types[typeid];
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
if (typeid === void 0) { typeid = this.schemas.size; }
|
|
85
|
+
}
|
|
86
|
+
add(schema, typeid = this.schemas.size) {
|
|
110
87
|
// FIXME: move this to somewhere else?
|
|
111
88
|
// support inheritance
|
|
112
89
|
schema._definition = SchemaDefinition.create(schema._definition);
|
|
113
90
|
schema._typeid = typeid;
|
|
114
91
|
this.types[typeid] = schema;
|
|
115
92
|
this.schemas.set(schema, typeid);
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
if (options === void 0) { options = {}; }
|
|
93
|
+
}
|
|
94
|
+
static create(options = {}) {
|
|
119
95
|
return function (definition) {
|
|
120
96
|
if (!options.context) {
|
|
121
97
|
options.context = new Context();
|
|
122
98
|
}
|
|
123
99
|
return type(definition, options);
|
|
124
100
|
};
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
}());
|
|
101
|
+
}
|
|
102
|
+
}
|
|
128
103
|
exports.Context = Context;
|
|
129
104
|
exports.globalContext = new Context();
|
|
130
105
|
/**
|
|
@@ -143,14 +118,13 @@ exports.globalContext = new Context();
|
|
|
143
118
|
* \@type("string", { manual: true })
|
|
144
119
|
* ```
|
|
145
120
|
*/
|
|
146
|
-
function type(type, options) {
|
|
147
|
-
if (options === void 0) { options = {}; }
|
|
121
|
+
function type(type, options = {}) {
|
|
148
122
|
return function (target, field) {
|
|
149
|
-
|
|
150
|
-
|
|
123
|
+
const context = options.context || exports.globalContext;
|
|
124
|
+
const constructor = target.constructor;
|
|
151
125
|
constructor._context = context;
|
|
152
126
|
if (!type) {
|
|
153
|
-
throw new Error(constructor.name
|
|
127
|
+
throw new Error(`${constructor.name}: @type() reference provided for "${field}" is undefined. Make sure you don't have any circular dependencies.`);
|
|
154
128
|
}
|
|
155
129
|
/*
|
|
156
130
|
* static schema
|
|
@@ -158,7 +132,7 @@ function type(type, options) {
|
|
|
158
132
|
if (!context.has(constructor)) {
|
|
159
133
|
context.add(constructor);
|
|
160
134
|
}
|
|
161
|
-
|
|
135
|
+
const definition = constructor._definition;
|
|
162
136
|
definition.addField(field, type);
|
|
163
137
|
/**
|
|
164
138
|
* skip if descriptor already exists for this field (`@deprecated()`)
|
|
@@ -172,21 +146,21 @@ function type(type, options) {
|
|
|
172
146
|
// trying to define same property multiple times across inheritance.
|
|
173
147
|
// https://github.com/colyseus/colyseus-unity3d/issues/131#issuecomment-814308572
|
|
174
148
|
try {
|
|
175
|
-
throw new Error(
|
|
149
|
+
throw new Error(`@colyseus/schema: Duplicate '${field}' definition on '${constructor.name}'.\nCheck @type() annotation`);
|
|
176
150
|
}
|
|
177
151
|
catch (e) {
|
|
178
|
-
|
|
179
|
-
throw new Error(e.message
|
|
152
|
+
const definitionAtLine = e.stack.split("\n")[4].trim();
|
|
153
|
+
throw new Error(`${e.message} ${definitionAtLine}`);
|
|
180
154
|
}
|
|
181
155
|
}
|
|
182
156
|
}
|
|
183
|
-
|
|
184
|
-
|
|
157
|
+
const isArray = ArraySchema_1.ArraySchema.is(type);
|
|
158
|
+
const isMap = !isArray && MapSchema_1.MapSchema.is(type);
|
|
185
159
|
// TODO: refactor me.
|
|
186
160
|
// Allow abstract intermediary classes with no fields to be serialized
|
|
187
161
|
// (See "should support an inheritance with a Schema type without fields" test)
|
|
188
162
|
if (typeof (type) !== "string" && !Schema_1.Schema.is(type)) {
|
|
189
|
-
|
|
163
|
+
const childType = Object.values(type)[0];
|
|
190
164
|
if (typeof (childType) !== "string" && !context.has(childType)) {
|
|
191
165
|
context.add(childType);
|
|
192
166
|
}
|
|
@@ -200,7 +174,7 @@ function type(type, options) {
|
|
|
200
174
|
};
|
|
201
175
|
return;
|
|
202
176
|
}
|
|
203
|
-
|
|
177
|
+
const fieldCached = `_${field}`;
|
|
204
178
|
definition.descriptors[fieldCached] = {
|
|
205
179
|
enumerable: false,
|
|
206
180
|
configurable: false,
|
|
@@ -222,7 +196,7 @@ function type(type, options) {
|
|
|
222
196
|
value !== null) {
|
|
223
197
|
// automaticallty transform Array into ArraySchema
|
|
224
198
|
if (isArray && !(value instanceof ArraySchema_1.ArraySchema)) {
|
|
225
|
-
value = new
|
|
199
|
+
value = new ArraySchema_1.ArraySchema(...value);
|
|
226
200
|
}
|
|
227
201
|
// automaticallty transform Map into MapSchema
|
|
228
202
|
if (isMap && !(value instanceof MapSchema_1.MapSchema)) {
|
|
@@ -231,10 +205,10 @@ function type(type, options) {
|
|
|
231
205
|
// try to turn provided structure into a Proxy
|
|
232
206
|
if (value['$proxy'] === undefined) {
|
|
233
207
|
if (isMap) {
|
|
234
|
-
value = MapSchema_1.getMapProxy(value);
|
|
208
|
+
value = (0, MapSchema_1.getMapProxy)(value);
|
|
235
209
|
}
|
|
236
210
|
else if (isArray) {
|
|
237
|
-
value = ArraySchema_1.getArrayProxy(value);
|
|
211
|
+
value = (0, ArraySchema_1.getArrayProxy)(value);
|
|
238
212
|
}
|
|
239
213
|
}
|
|
240
214
|
// flag the change for encoding.
|
|
@@ -247,7 +221,7 @@ function type(type, options) {
|
|
|
247
221
|
value['$changes'].setParent(this, this.$changes.root, this._definition.indexes[field]);
|
|
248
222
|
}
|
|
249
223
|
}
|
|
250
|
-
else {
|
|
224
|
+
else if (this[fieldCached]) {
|
|
251
225
|
//
|
|
252
226
|
// Setting a field to `null` or `undefined` will delete it.
|
|
253
227
|
//
|
|
@@ -266,8 +240,8 @@ exports.type = type;
|
|
|
266
240
|
*/
|
|
267
241
|
function filter(cb) {
|
|
268
242
|
return function (target, field) {
|
|
269
|
-
|
|
270
|
-
|
|
243
|
+
const constructor = target.constructor;
|
|
244
|
+
const definition = constructor._definition;
|
|
271
245
|
if (definition.addFilter(field, cb)) {
|
|
272
246
|
constructor._context.useFilters = true;
|
|
273
247
|
}
|
|
@@ -276,8 +250,8 @@ function filter(cb) {
|
|
|
276
250
|
exports.filter = filter;
|
|
277
251
|
function filterChildren(cb) {
|
|
278
252
|
return function (target, field) {
|
|
279
|
-
|
|
280
|
-
|
|
253
|
+
const constructor = target.constructor;
|
|
254
|
+
const definition = constructor._definition;
|
|
281
255
|
if (definition.addChildrenFilter(field, cb)) {
|
|
282
256
|
constructor._context.useFilters = true;
|
|
283
257
|
}
|
|
@@ -288,15 +262,14 @@ exports.filterChildren = filterChildren;
|
|
|
288
262
|
* `@deprecated()` flag a field as deprecated.
|
|
289
263
|
* The previous `@type()` annotation should remain along with this one.
|
|
290
264
|
*/
|
|
291
|
-
function deprecated(throws) {
|
|
292
|
-
if (throws === void 0) { throws = true; }
|
|
265
|
+
function deprecated(throws = true) {
|
|
293
266
|
return function (target, field) {
|
|
294
|
-
|
|
295
|
-
|
|
267
|
+
const constructor = target.constructor;
|
|
268
|
+
const definition = constructor._definition;
|
|
296
269
|
definition.deprecated[field] = true;
|
|
297
270
|
if (throws) {
|
|
298
271
|
definition.descriptors[field] = {
|
|
299
|
-
get: function () { throw new Error(field
|
|
272
|
+
get: function () { throw new Error(`${field} is deprecated.`); },
|
|
300
273
|
set: function (value) { },
|
|
301
274
|
enumerable: false,
|
|
302
275
|
configurable: true
|
|
@@ -305,12 +278,11 @@ function deprecated(throws) {
|
|
|
305
278
|
};
|
|
306
279
|
}
|
|
307
280
|
exports.deprecated = deprecated;
|
|
308
|
-
function defineTypes(target, fields, options) {
|
|
309
|
-
if (options === void 0) { options = {}; }
|
|
281
|
+
function defineTypes(target, fields, options = {}) {
|
|
310
282
|
if (!options.context) {
|
|
311
283
|
options.context = target._context || options.context || exports.globalContext;
|
|
312
284
|
}
|
|
313
|
-
for (
|
|
285
|
+
for (let field in fields) {
|
|
314
286
|
type(fields[field], options)(target.prototype, field);
|
|
315
287
|
}
|
|
316
288
|
return target;
|
package/lib/annotations.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"annotations.js","sourceRoot":"","sources":["../src/annotations.ts"],"names":[],"mappings":";;;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AAC,UAAU,CAAC,KAAK,CAAC,GAAG,IAAI,CAAC;QAEpC,IAAI,MAAM,EAAE;YACR,UAAU,CAAC,WAAW,CAAC,KAAK,CAAC,GAAG;gBAC5B,GAAG,EAAE,cAAc,MAAM,IAAI,KAAK,CAAI,KAAK,oBAAiB,CAAC,CAAC,CAAC,CAAC;gBAChE,GAAG,EAAE,UAAwB,KAAU,IAAsD,CAAC;gBAC9F,UAAU,EAAE,KAAK;gBACjB,YAAY,EAAE,IAAI;aACrB,CAAC;SACL;IACL,CAAC,CAAA;AACL,CAAC;AAhBD,gCAgBC;AAED,SAAgB,WAAW,CACvB,MAAqB,EACrB,MAA8C,EAC9C,OAAyB;IAAzB,wBAAA,EAAA,YAAyB;IAEzB,IAAI,CAAC,OAAO,CAAC,OAAO,EAAE;QAClB,OAAO,CAAC,OAAO,GAAG,MAAM,CAAC,QAAQ,IAAI,OAAO,CAAC,OAAO,IAAI,qBAAa,CAAC;KACzE;IAED,KAAK,IAAI,KAAK,IAAI,MAAM,EAAE;QACtB,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,OAAO,CAAC,CAAC,MAAM,CAAC,SAAS,EAAE,KAAK,CAAC,CAAC;KACzD;IACD,OAAO,MAAM,CAAC;AAClB,CAAC;AAbD,kCAaC","sourcesContent":["import { ChangeTree } from './changes/ChangeTree';\nimport { Schema } from './Schema';\nimport { ArraySchema, getArrayProxy } from './types/ArraySchema';\nimport { MapSchema, getMapProxy } from './types/MapSchema';\nimport { getType } from './types/typeRegistry';\n\n/**\n * Data types\n */\nexport type PrimitiveType =\n \"string\" |\n \"number\" |\n \"boolean\" |\n \"int8\" |\n \"uint8\" |\n \"int16\" |\n \"uint16\" |\n \"int32\" |\n \"uint32\" |\n \"int64\" |\n \"uint64\" |\n \"float32\" |\n \"float64\" |\n typeof Schema;\n\nexport type DefinitionType = PrimitiveType\n | PrimitiveType[]\n | { array: PrimitiveType }\n | { map: PrimitiveType }\n | { collection: PrimitiveType }\n | { set: PrimitiveType };\n\nexport type Definition = { [field: string]: DefinitionType };\nexport type FilterCallback<\n T extends Schema = any,\n V = any,\n R extends Schema = any\n> = (\n ((this: T, client: ClientWithSessionId, value: V) => boolean) |\n ((this: T, client: ClientWithSessionId, value: V, root: R) => boolean)\n);\n\nexport type FilterChildrenCallback<\n T extends Schema = any,\n K = any,\n V = any,\n R extends Schema = any\n> = (\n ((this: T, client: ClientWithSessionId, key: K, value: V) => boolean) |\n ((this: T, client: ClientWithSessionId, key: K, value: V, root: R) => boolean)\n)\n\nexport class SchemaDefinition {\n schema: Definition;\n\n //\n // TODO: use a \"field\" structure combining all these properties per-field.\n //\n\n indexes: { [field: string]: number } = {};\n fieldsByIndex: { [index: number]: string } = {};\n\n filters: { [field: string]: FilterCallback };\n indexesWithFilters: number[];\n childFilters: { [field: string]: FilterChildrenCallback }; // childFilters are used on Map, Array, Set items.\n\n deprecated: { [field: string]: boolean } = {};\n descriptors: PropertyDescriptorMap & ThisType<any> = {};\n\n static create(parent?: SchemaDefinition) {\n const definition = new SchemaDefinition();\n\n // support inheritance\n definition.schema = Object.assign({}, parent && parent.schema || {});\n definition.indexes = Object.assign({}, parent && parent.indexes || {});\n definition.fieldsByIndex = Object.assign({}, parent && parent.fieldsByIndex || {});\n definition.descriptors = Object.assign({}, parent && parent.descriptors || {});\n definition.deprecated = Object.assign({}, parent && parent.deprecated || {});\n\n return definition;\n }\n\n addField(field: string, type: DefinitionType) {\n const index = this.getNextFieldIndex();\n this.fieldsByIndex[index] = field;\n this.indexes[field] = index;\n this.schema[field] = (Array.isArray(type))\n ? { array: type[0] }\n : type;\n }\n\n hasField(field: string) {\n return this.indexes[field] !== undefined;\n }\n\n addFilter(field: string, cb: FilterCallback) {\n if (!this.filters) {\n this.filters = {};\n this.indexesWithFilters = [];\n }\n this.filters[this.indexes[field]] = cb;\n this.indexesWithFilters.push(this.indexes[field]);\n return true;\n }\n\n addChildrenFilter(field: string, cb: FilterChildrenCallback) {\n const index = this.indexes[field];\n const type = this.schema[field];\n\n if (getType(Object.keys(type)[0])) {\n if (!this.childFilters) { this.childFilters = {}; }\n\n this.childFilters[index] = cb;\n return true;\n\n } else {\n console.warn(`@filterChildren: field '${field}' can't have children. Ignoring filter.`);\n }\n }\n\n getChildrenFilter(field: string) {\n return this.childFilters && this.childFilters[this.indexes[field]];\n }\n\n getNextFieldIndex() {\n return Object.keys(this.schema || {}).length;\n }\n}\n\nexport function hasFilter(klass: typeof Schema) {\n return klass._context && klass._context.useFilters;\n}\n\n// Colyseus integration\nexport type ClientWithSessionId = { sessionId: string } & any;\n\nexport interface TypeOptions {\n manual?: boolean,\n context?: Context,\n}\n\nexport class Context {\n types: {[id: number]: typeof Schema} = {};\n schemas = new Map<typeof Schema, number>();\n useFilters = false;\n\n has(schema: typeof Schema) {\n return this.schemas.has(schema);\n }\n\n get(typeid: number) {\n return this.types[typeid];\n }\n\n add(schema: typeof Schema, typeid: number = this.schemas.size) {\n // FIXME: move this to somewhere else?\n // support inheritance\n schema._definition = SchemaDefinition.create(schema._definition);\n\n schema._typeid = typeid;\n this.types[typeid] = schema;\n this.schemas.set(schema, typeid);\n }\n\n\n static create(options: TypeOptions = {}) {\n return function (definition: DefinitionType) {\n if (!options.context) {\n options.context = new Context();\n }\n return type(definition, options);\n }\n }\n}\n\nexport const globalContext = new Context();\n\n/**\n * [See documentation](https://docs.colyseus.io/state/schema/)\n *\n * Annotate a Schema property to be serializeable.\n * \\@type()'d fields are automatically flagged as \"dirty\" for the next patch.\n *\n * @example Standard usage, with automatic change tracking.\n * ```\n * \\@type(\"string\") propertyName: string;\n * ```\n *\n * @example You can provide the \"manual\" option if you'd like to manually control your patches via .setDirty().\n * ```\n * \\@type(\"string\", { manual: true })\n * ```\n */\nexport function type (\n type: DefinitionType,\n options: TypeOptions = {}\n): PropertyDecorator {\n return function (target: typeof Schema, field: string) {\n const context = options.context || globalContext;\n const constructor = target.constructor as typeof Schema;\n constructor._context = context;\n\n if (!type) {\n throw new Error(`${constructor.name}: @type() reference provided for \"${field}\" is undefined. Make sure you don't have any circular dependencies.`);\n }\n\n /*\n * static schema\n */\n if (!context.has(constructor)) {\n context.add(constructor);\n }\n\n const definition = constructor._definition;\n definition.addField(field, type);\n\n /**\n * skip if descriptor already exists for this field (`@deprecated()`)\n */\n if (definition.descriptors[field]) {\n if (definition.deprecated[field]) {\n // do not create accessors for deprecated properties.\n return;\n\n } else {\n // trying to define same property multiple times across inheritance.\n // https://github.com/colyseus/colyseus-unity3d/issues/131#issuecomment-814308572\n try {\n throw new Error(`@colyseus/schema: Duplicate '${field}' definition on '${constructor.name}'.\\nCheck @type() annotation`);\n\n } catch (e) {\n const definitionAtLine = e.stack.split(\"\\n\")[4].trim();\n throw new Error(`${e.message} ${definitionAtLine}`);\n }\n }\n }\n\n const isArray = ArraySchema.is(type);\n const isMap = !isArray && MapSchema.is(type);\n\n // TODO: refactor me.\n // Allow abstract intermediary classes with no fields to be serialized\n // (See \"should support an inheritance with a Schema type without fields\" test)\n if (typeof (type) !== \"string\" && !Schema.is(type)) {\n const childType = Object.values(type)[0];\n if (typeof (childType) !== \"string\" && !context.has(childType)) {\n context.add(childType);\n }\n }\n\n if (options.manual) {\n // do not declare getter/setter descriptor\n definition.descriptors[field] = {\n enumerable: true,\n configurable: true,\n writable: true,\n };\n return;\n }\n\n const fieldCached = `_${field}`;\n definition.descriptors[fieldCached] = {\n enumerable: false,\n configurable: false,\n writable: true,\n };\n\n definition.descriptors[field] = {\n get: function () {\n return this[fieldCached];\n },\n\n set: function (this: Schema, value: any) {\n /**\n * Create Proxy for array or map items\n */\n\n // skip if value is the same as cached.\n if (value === this[fieldCached]) {\n return;\n }\n\n if (\n value !== undefined &&\n value !== null\n ) {\n // automaticallty transform Array into ArraySchema\n if (isArray && !(value instanceof ArraySchema)) {\n value = new ArraySchema(...value);\n }\n\n // automaticallty transform Map into MapSchema\n if (isMap && !(value instanceof MapSchema)) {\n value = new MapSchema(value);\n }\n\n // try to turn provided structure into a Proxy\n if (value['$proxy'] === undefined) {\n if (isMap) {\n value = getMapProxy(value);\n\n } else if (isArray) {\n value = getArrayProxy(value);\n }\n }\n\n // flag the change for encoding.\n this.$changes.change(field);\n\n //\n // call setParent() recursively for this and its child\n // structures.\n //\n if (value['$changes']) {\n (value['$changes'] as ChangeTree).setParent(\n this,\n this.$changes.root,\n this._definition.indexes[field],\n );\n }\n\n } else {\n //\n // Setting a field to `null` or `undefined` will delete it.\n //\n this.$changes.delete(field);\n }\n\n this[fieldCached] = value;\n },\n\n enumerable: true,\n configurable: true\n };\n }\n}\n\n/**\n * `@filter()` decorator for defining data filters per client\n */\n\nexport function filter<T extends Schema, V, R extends Schema>(cb: FilterCallback<T, V, R>): PropertyDecorator {\n return function (target: any, field: string) {\n const constructor = target.constructor as typeof Schema;\n const definition = constructor._definition;\n\n if (definition.addFilter(field, cb)) {\n constructor._context.useFilters = true;\n }\n }\n}\n\nexport function filterChildren<T extends Schema, K, V, R extends Schema>(cb: FilterChildrenCallback<T, K, V, R>): PropertyDecorator {\n return function (target: any, field: string) {\n const constructor = target.constructor as typeof Schema;\n const definition = constructor._definition;\n if (definition.addChildrenFilter(field, cb)) {\n constructor._context.useFilters = true;\n }\n }\n}\n\n\n/**\n * `@deprecated()` flag a field as deprecated.\n * The previous `@type()` annotation should remain along with this one.\n */\n\nexport function deprecated(throws: boolean = true): PropertyDecorator {\n return function (target: typeof Schema, field: string) {\n const constructor = target.constructor as typeof Schema;\n const definition = constructor._definition;\n\n definition.deprecated[field] = true;\n\n if (throws) {\n definition.descriptors[field] = {\n get: function () { throw new Error(`${field} is deprecated.`); },\n set: function (this: Schema, value: any) { /* throw new Error(`${field} is deprecated.`); */ },\n enumerable: false,\n configurable: true\n };\n }\n }\n}\n\nexport function defineTypes(\n target: typeof Schema,\n fields: { [property: string]: DefinitionType },\n options: TypeOptions = {}\n) {\n if (!options.context) {\n options.context = target._context || options.context || globalContext;\n }\n\n for (let field in fields) {\n type(fields[field], options)(target.prototype, field);\n }\n return target;\n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C,IAAI,MAAM,EAAE;YACR,UAAU,CAAC,WAAW,CAAC,KAAK,CAAC,GAAG;gBAC5B,GAAG,EAAE,cAAc,MAAM,IAAI,KAAK,CAAC,GAAG,KAAK,iBAAiB,CAAC,CAAC,CAAC,CAAC;gBAChE,GAAG,EAAE,UAAwB,KAAU,IAAsD,CAAC;gBAC9F,UAAU,EAAE,KAAK;gBACjB,YAAY,EAAE,IAAI;aACrB,CAAC;SACL;IACL,CAAC,CAAA;AACL,CAAC;AAhBD,gCAgBC;AAED,SAAgB,WAAW,CACvB,MAAqB,EACrB,MAA8C,EAC9C,UAAuB,EAAE;IAEzB,IAAI,CAAC,OAAO,CAAC,OAAO,EAAE;QAClB,OAAO,CAAC,OAAO,GAAG,MAAM,CAAC,QAAQ,IAAI,OAAO,CAAC,OAAO,IAAI,qBAAa,CAAC;KACzE;IAED,KAAK,IAAI,KAAK,IAAI,MAAM,EAAE;QACtB,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,OAAO,CAAC,CAAC,MAAM,CAAC,SAAS,EAAE,KAAK,CAAC,CAAC;KACzD;IACD,OAAO,MAAM,CAAC;AAClB,CAAC;AAbD,kCAaC","sourcesContent":["import { ChangeTree } from './changes/ChangeTree';\nimport { Schema } from './Schema';\nimport { ArraySchema, getArrayProxy } from './types/ArraySchema';\nimport { MapSchema, getMapProxy } from './types/MapSchema';\nimport { getType } from './types/typeRegistry';\n\n/**\n * Data types\n */\nexport type PrimitiveType =\n \"string\" |\n \"number\" |\n \"boolean\" |\n \"int8\" |\n \"uint8\" |\n \"int16\" |\n \"uint16\" |\n \"int32\" |\n \"uint32\" |\n \"int64\" |\n \"uint64\" |\n \"float32\" |\n \"float64\" |\n typeof Schema;\n\nexport type DefinitionType = PrimitiveType\n | PrimitiveType[]\n | { array: PrimitiveType }\n | { map: PrimitiveType }\n | { collection: PrimitiveType }\n | { set: PrimitiveType };\n\nexport type Definition = { [field: string]: DefinitionType };\nexport type FilterCallback<\n T extends Schema = any,\n V = any,\n R extends Schema = any\n> = (\n ((this: T, client: ClientWithSessionId, value: V) => boolean) |\n ((this: T, client: ClientWithSessionId, value: V, root: R) => boolean)\n);\n\nexport type FilterChildrenCallback<\n T extends Schema = any,\n K = any,\n V = any,\n R extends Schema = any\n> = (\n ((this: T, client: ClientWithSessionId, key: K, value: V) => boolean) |\n ((this: T, client: ClientWithSessionId, key: K, value: V, root: R) => boolean)\n)\n\nexport class SchemaDefinition {\n schema: Definition;\n\n //\n // TODO: use a \"field\" structure combining all these properties per-field.\n //\n\n indexes: { [field: string]: number } = {};\n fieldsByIndex: { [index: number]: string } = {};\n\n filters: { [field: string]: FilterCallback };\n indexesWithFilters: number[];\n childFilters: { [field: string]: FilterChildrenCallback }; // childFilters are used on Map, Array, Set items.\n\n deprecated: { [field: string]: boolean } = {};\n descriptors: PropertyDescriptorMap & ThisType<any> = {};\n\n static create(parent?: SchemaDefinition) {\n const definition = new SchemaDefinition();\n\n // support inheritance\n definition.schema = Object.assign({}, parent && parent.schema || {});\n definition.indexes = Object.assign({}, parent && parent.indexes || {});\n definition.fieldsByIndex = Object.assign({}, parent && parent.fieldsByIndex || {});\n definition.descriptors = Object.assign({}, parent && parent.descriptors || {});\n definition.deprecated = Object.assign({}, parent && parent.deprecated || {});\n\n return definition;\n }\n\n addField(field: string, type: DefinitionType) {\n const index = this.getNextFieldIndex();\n this.fieldsByIndex[index] = field;\n this.indexes[field] = index;\n this.schema[field] = (Array.isArray(type))\n ? { array: type[0] }\n : type;\n }\n\n hasField(field: string) {\n return this.indexes[field] !== undefined;\n }\n\n addFilter(field: string, cb: FilterCallback) {\n if (!this.filters) {\n this.filters = {};\n this.indexesWithFilters = [];\n }\n this.filters[this.indexes[field]] = cb;\n this.indexesWithFilters.push(this.indexes[field]);\n return true;\n }\n\n addChildrenFilter(field: string, cb: FilterChildrenCallback) {\n const index = this.indexes[field];\n const type = this.schema[field];\n\n if (getType(Object.keys(type)[0])) {\n if (!this.childFilters) { this.childFilters = {}; }\n\n this.childFilters[index] = cb;\n return true;\n\n } else {\n console.warn(`@filterChildren: field '${field}' can't have children. Ignoring filter.`);\n }\n }\n\n getChildrenFilter(field: string) {\n return this.childFilters && this.childFilters[this.indexes[field]];\n }\n\n getNextFieldIndex() {\n return Object.keys(this.schema || {}).length;\n }\n}\n\nexport function hasFilter(klass: typeof Schema) {\n return klass._context && klass._context.useFilters;\n}\n\n// Colyseus integration\nexport type ClientWithSessionId = { sessionId: string } & any;\n\nexport interface TypeOptions {\n manual?: boolean,\n context?: Context,\n}\n\nexport class Context {\n types: {[id: number]: typeof Schema} = {};\n schemas = new Map<typeof Schema, number>();\n useFilters = false;\n\n has(schema: typeof Schema) {\n return this.schemas.has(schema);\n }\n\n get(typeid: number) {\n return this.types[typeid];\n }\n\n add(schema: typeof Schema, typeid: number = this.schemas.size) {\n // FIXME: move this to somewhere else?\n // support inheritance\n schema._definition = SchemaDefinition.create(schema._definition);\n\n schema._typeid = typeid;\n this.types[typeid] = schema;\n this.schemas.set(schema, typeid);\n }\n\n\n static create(options: TypeOptions = {}) {\n return function (definition: DefinitionType) {\n if (!options.context) {\n options.context = new Context();\n }\n return type(definition, options);\n }\n }\n}\n\nexport const globalContext = new Context();\n\n/**\n * [See documentation](https://docs.colyseus.io/state/schema/)\n *\n * Annotate a Schema property to be serializeable.\n * \\@type()'d fields are automatically flagged as \"dirty\" for the next patch.\n *\n * @example Standard usage, with automatic change tracking.\n * ```\n * \\@type(\"string\") propertyName: string;\n * ```\n *\n * @example You can provide the \"manual\" option if you'd like to manually control your patches via .setDirty().\n * ```\n * \\@type(\"string\", { manual: true })\n * ```\n */\nexport function type (\n type: DefinitionType,\n options: TypeOptions = {}\n): PropertyDecorator {\n return function (target: typeof Schema, field: string) {\n const context = options.context || globalContext;\n const constructor = target.constructor as typeof Schema;\n constructor._context = context;\n\n if (!type) {\n throw new Error(`${constructor.name}: @type() reference provided for \"${field}\" is undefined. Make sure you don't have any circular dependencies.`);\n }\n\n /*\n * static schema\n */\n if (!context.has(constructor)) {\n context.add(constructor);\n }\n\n const definition = constructor._definition;\n definition.addField(field, type);\n\n /**\n * skip if descriptor already exists for this field (`@deprecated()`)\n */\n if (definition.descriptors[field]) {\n if (definition.deprecated[field]) {\n // do not create accessors for deprecated properties.\n return;\n\n } else {\n // trying to define same property multiple times across inheritance.\n // https://github.com/colyseus/colyseus-unity3d/issues/131#issuecomment-814308572\n try {\n throw new Error(`@colyseus/schema: Duplicate '${field}' definition on '${constructor.name}'.\\nCheck @type() annotation`);\n\n } catch (e) {\n const definitionAtLine = e.stack.split(\"\\n\")[4].trim();\n throw new Error(`${e.message} ${definitionAtLine}`);\n }\n }\n }\n\n const isArray = ArraySchema.is(type);\n const isMap = !isArray && MapSchema.is(type);\n\n // TODO: refactor me.\n // Allow abstract intermediary classes with no fields to be serialized\n // (See \"should support an inheritance with a Schema type without fields\" test)\n if (typeof (type) !== \"string\" && !Schema.is(type)) {\n const childType = Object.values(type)[0];\n if (typeof (childType) !== \"string\" && !context.has(childType)) {\n context.add(childType);\n }\n }\n\n if (options.manual) {\n // do not declare getter/setter descriptor\n definition.descriptors[field] = {\n enumerable: true,\n configurable: true,\n writable: true,\n };\n return;\n }\n\n const fieldCached = `_${field}`;\n definition.descriptors[fieldCached] = {\n enumerable: false,\n configurable: false,\n writable: true,\n };\n\n definition.descriptors[field] = {\n get: function () {\n return this[fieldCached];\n },\n\n set: function (this: Schema, value: any) {\n /**\n * Create Proxy for array or map items\n */\n\n // skip if value is the same as cached.\n if (value === this[fieldCached]) {\n return;\n }\n\n if (\n value !== undefined &&\n value !== null\n ) {\n // automaticallty transform Array into ArraySchema\n if (isArray && !(value instanceof ArraySchema)) {\n value = new ArraySchema(...value);\n }\n\n // automaticallty transform Map into MapSchema\n if (isMap && !(value instanceof MapSchema)) {\n value = new MapSchema(value);\n }\n\n // try to turn provided structure into a Proxy\n if (value['$proxy'] === undefined) {\n if (isMap) {\n value = getMapProxy(value);\n\n } else if (isArray) {\n value = getArrayProxy(value);\n }\n }\n\n // flag the change for encoding.\n this.$changes.change(field);\n\n //\n // call setParent() recursively for this and its child\n // structures.\n //\n if (value['$changes']) {\n (value['$changes'] as ChangeTree).setParent(\n this,\n this.$changes.root,\n this._definition.indexes[field],\n );\n }\n\n } else if (this[fieldCached]) {\n //\n // Setting a field to `null` or `undefined` will delete it.\n //\n this.$changes.delete(field);\n }\n\n this[fieldCached] = value;\n },\n\n enumerable: true,\n configurable: true\n };\n }\n}\n\n/**\n * `@filter()` decorator for defining data filters per client\n */\n\nexport function filter<T extends Schema, V, R extends Schema>(cb: FilterCallback<T, V, R>): PropertyDecorator {\n return function (target: any, field: string) {\n const constructor = target.constructor as typeof Schema;\n const definition = constructor._definition;\n\n if (definition.addFilter(field, cb)) {\n constructor._context.useFilters = true;\n }\n }\n}\n\nexport function filterChildren<T extends Schema, K, V, R extends Schema>(cb: FilterChildrenCallback<T, K, V, R>): PropertyDecorator {\n return function (target: any, field: string) {\n const constructor = target.constructor as typeof Schema;\n const definition = constructor._definition;\n if (definition.addChildrenFilter(field, cb)) {\n constructor._context.useFilters = true;\n }\n }\n}\n\n\n/**\n * `@deprecated()` flag a field as deprecated.\n * The previous `@type()` annotation should remain along with this one.\n */\n\nexport function deprecated(throws: boolean = true): PropertyDecorator {\n return function (target: typeof Schema, field: string) {\n const constructor = target.constructor as typeof Schema;\n const definition = constructor._definition;\n\n definition.deprecated[field] = true;\n\n if (throws) {\n definition.descriptors[field] = {\n get: function () { throw new Error(`${field} is deprecated.`); },\n set: function (this: Schema, value: any) { /* throw new Error(`${field} is deprecated.`); */ },\n enumerable: false,\n configurable: true\n };\n }\n }\n}\n\nexport function defineTypes(\n target: typeof Schema,\n fields: { [property: string]: DefinitionType },\n options: TypeOptions = {}\n) {\n if (!options.context) {\n options.context = target._context || options.context || globalContext;\n }\n\n for (let field in fields) {\n type(fields[field], options)(target.prototype, field);\n }\n return target;\n}\n"]}
|
|
@@ -6,7 +6,7 @@ import { ArraySchema } from "../types/ArraySchema";
|
|
|
6
6
|
import { CollectionSchema } from "../types/CollectionSchema";
|
|
7
7
|
import { SetSchema } from "../types/SetSchema";
|
|
8
8
|
import { ReferenceTracker } from "./ReferenceTracker";
|
|
9
|
-
export
|
|
9
|
+
export type Ref = Schema | ArraySchema | MapSchema | CollectionSchema | SetSchema;
|
|
10
10
|
export interface ChangeOperation {
|
|
11
11
|
op: OPERATION;
|
|
12
12
|
index: number;
|