@colyseus/schema 1.0.45 → 1.0.46
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/build/cjs/index.js +0 -3
- package/build/cjs/index.js.map +1 -1
- package/build/esm/index.mjs +4 -3
- package/build/esm/index.mjs.map +1 -1
- package/build/umd/index.js +0 -3
- package/lib/Reflection.js +77 -109
- package/lib/Reflection.js.map +1 -1
- package/lib/Schema.js +194 -255
- package/lib/Schema.js.map +1 -1
- package/lib/annotations.d.ts +6 -6
- package/lib/annotations.js +56 -89
- package/lib/annotations.js.map +1 -1
- package/lib/changes/ChangeTree.d.ts +1 -1
- package/lib/changes/ChangeTree.js +86 -96
- package/lib/changes/ChangeTree.js.map +1 -1
- package/lib/codegen/api.js +9 -9
- package/lib/codegen/api.js.map +1 -1
- package/lib/codegen/argv.js +11 -11
- package/lib/codegen/argv.js.map +1 -1
- package/lib/codegen/cli.js +20 -9
- 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 +68 -68
- 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 -72
- package/lib/codegen/languages/ts.js.map +1 -1
- package/lib/codegen/parser.js +47 -51
- package/lib/codegen/parser.js.map +1 -1
- package/lib/codegen/types.js +55 -60
- 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 -51
- 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 +7 -7
- package/lib/index.js.map +1 -1
- package/lib/types/ArraySchema.d.ts +1 -1
- package/lib/types/ArraySchema.js +157 -219
- package/lib/types/ArraySchema.js.map +1 -1
- package/lib/types/CollectionSchema.d.ts +1 -1
- package/lib/types/CollectionSchema.js +60 -68
- package/lib/types/CollectionSchema.js.map +1 -1
- package/lib/types/HelperTypes.d.ts +9 -9
- package/lib/types/MapSchema.js +63 -74
- package/lib/types/MapSchema.js.map +1 -1
- package/lib/types/SetSchema.js +59 -68
- 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/utils.js +10 -13
- package/lib/utils.js.map +1 -1
- package/package.json +1 -1
- package/src/types/ArraySchema.ts +4 -3
package/lib/Schema.js
CHANGED
|
@@ -1,59 +1,23 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __extends = (this && this.__extends) || (function () {
|
|
3
|
-
var extendStatics = function (d, b) {
|
|
4
|
-
extendStatics = Object.setPrototypeOf ||
|
|
5
|
-
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
-
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
-
return extendStatics(d, b);
|
|
8
|
-
};
|
|
9
|
-
return function (d, b) {
|
|
10
|
-
if (typeof b !== "function" && b !== null)
|
|
11
|
-
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
-
extendStatics(d, b);
|
|
13
|
-
function __() { this.constructor = d; }
|
|
14
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
-
};
|
|
16
|
-
})();
|
|
17
|
-
var __read = (this && this.__read) || function (o, n) {
|
|
18
|
-
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
19
|
-
if (!m) return o;
|
|
20
|
-
var i = m.call(o), r, ar = [], e;
|
|
21
|
-
try {
|
|
22
|
-
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
23
|
-
}
|
|
24
|
-
catch (error) { e = { error: error }; }
|
|
25
|
-
finally {
|
|
26
|
-
try {
|
|
27
|
-
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
28
|
-
}
|
|
29
|
-
finally { if (e) throw e.error; }
|
|
30
|
-
}
|
|
31
|
-
return ar;
|
|
32
|
-
};
|
|
33
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
34
3
|
exports.Schema = void 0;
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
function EncodeSchemaError() {
|
|
50
|
-
return _super !== null && _super.apply(this, arguments) || this;
|
|
51
|
-
}
|
|
52
|
-
return EncodeSchemaError;
|
|
53
|
-
}(Error));
|
|
4
|
+
const spec_1 = require("./spec");
|
|
5
|
+
const annotations_1 = require("./annotations");
|
|
6
|
+
const encode = require("./encoding/encode");
|
|
7
|
+
const decode = require("./encoding/decode");
|
|
8
|
+
const ArraySchema_1 = require("./types/ArraySchema");
|
|
9
|
+
const MapSchema_1 = require("./types/MapSchema");
|
|
10
|
+
const CollectionSchema_1 = require("./types/CollectionSchema");
|
|
11
|
+
const SetSchema_1 = require("./types/SetSchema");
|
|
12
|
+
const ChangeTree_1 = require("./changes/ChangeTree");
|
|
13
|
+
const EventEmitter_1 = require("./events/EventEmitter");
|
|
14
|
+
const filters_1 = require("./filters");
|
|
15
|
+
const types_1 = require("./types");
|
|
16
|
+
class EncodeSchemaError extends Error {
|
|
17
|
+
}
|
|
54
18
|
function assertType(value, type, klass, field) {
|
|
55
|
-
|
|
56
|
-
|
|
19
|
+
let typeofTarget;
|
|
20
|
+
let allowNull = false;
|
|
57
21
|
switch (type) {
|
|
58
22
|
case "number":
|
|
59
23
|
case "int8":
|
|
@@ -68,7 +32,7 @@ function assertType(value, type, klass, field) {
|
|
|
68
32
|
case "float64":
|
|
69
33
|
typeofTarget = "number";
|
|
70
34
|
if (isNaN(value)) {
|
|
71
|
-
console.log(
|
|
35
|
+
console.log(`trying to encode "NaN" in ${klass.constructor.name}#${field}`);
|
|
72
36
|
}
|
|
73
37
|
break;
|
|
74
38
|
case "string":
|
|
@@ -80,23 +44,23 @@ function assertType(value, type, klass, field) {
|
|
|
80
44
|
return;
|
|
81
45
|
}
|
|
82
46
|
if (typeof (value) !== typeofTarget && (!allowNull || (allowNull && value !== null))) {
|
|
83
|
-
|
|
84
|
-
throw new EncodeSchemaError(
|
|
47
|
+
let foundValue = `'${JSON.stringify(value)}'${(value && value.constructor && ` (${value.constructor.name})`) || ''}`;
|
|
48
|
+
throw new EncodeSchemaError(`a '${typeofTarget}' was expected, but ${foundValue} was provided in ${klass.constructor.name}#${field}`);
|
|
85
49
|
}
|
|
86
50
|
}
|
|
87
51
|
function assertInstanceType(value, type, klass, field) {
|
|
88
52
|
if (!(value instanceof type)) {
|
|
89
|
-
throw new EncodeSchemaError(
|
|
53
|
+
throw new EncodeSchemaError(`a '${type.name}' was expected, but '${value.constructor.name}' was provided in ${klass.constructor.name}#${field}`);
|
|
90
54
|
}
|
|
91
55
|
}
|
|
92
56
|
function encodePrimitiveType(type, bytes, value, klass, field) {
|
|
93
57
|
assertType(value, type, klass, field);
|
|
94
|
-
|
|
58
|
+
const encodeFunc = encode[type];
|
|
95
59
|
if (encodeFunc) {
|
|
96
60
|
encodeFunc(bytes, value);
|
|
97
61
|
}
|
|
98
62
|
else {
|
|
99
|
-
throw new EncodeSchemaError(
|
|
63
|
+
throw new EncodeSchemaError(`a '${type}' was expected, but ${value} was provided in ${klass.constructor.name}#${field}`);
|
|
100
64
|
}
|
|
101
65
|
}
|
|
102
66
|
function decodePrimitiveType(type, bytes, it) {
|
|
@@ -105,13 +69,17 @@ function decodePrimitiveType(type, bytes, it) {
|
|
|
105
69
|
/**
|
|
106
70
|
* Schema encoder / decoder
|
|
107
71
|
*/
|
|
108
|
-
|
|
72
|
+
class Schema {
|
|
73
|
+
static { this._definition = annotations_1.SchemaDefinition.create(); }
|
|
74
|
+
static onError(e) {
|
|
75
|
+
console.error(e);
|
|
76
|
+
}
|
|
77
|
+
static is(type) {
|
|
78
|
+
return (type['_definition'] &&
|
|
79
|
+
type['_definition'].schema !== undefined);
|
|
80
|
+
}
|
|
109
81
|
// allow inherited classes to have a constructor
|
|
110
|
-
|
|
111
|
-
var args = [];
|
|
112
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
113
|
-
args[_i] = arguments[_i];
|
|
114
|
-
}
|
|
82
|
+
constructor(...args) {
|
|
115
83
|
// fix enumerability of fields for end-user
|
|
116
84
|
Object.defineProperties(this, {
|
|
117
85
|
$changes: {
|
|
@@ -125,7 +93,7 @@ var Schema = /** @class */ (function () {
|
|
|
125
93
|
writable: true
|
|
126
94
|
},
|
|
127
95
|
});
|
|
128
|
-
|
|
96
|
+
const descriptors = this._definition.descriptors;
|
|
129
97
|
if (descriptors) {
|
|
130
98
|
Object.defineProperties(this, descriptors);
|
|
131
99
|
}
|
|
@@ -136,53 +104,36 @@ var Schema = /** @class */ (function () {
|
|
|
136
104
|
this.assign(args[0]);
|
|
137
105
|
}
|
|
138
106
|
}
|
|
139
|
-
|
|
140
|
-
console.error(e);
|
|
141
|
-
};
|
|
142
|
-
Schema.is = function (type) {
|
|
143
|
-
return (type['_definition'] &&
|
|
144
|
-
type['_definition'].schema !== undefined);
|
|
145
|
-
};
|
|
146
|
-
Schema.prototype.assign = function (props) {
|
|
107
|
+
assign(props) {
|
|
147
108
|
Object.assign(this, props);
|
|
148
109
|
return this;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
enumerable: false,
|
|
153
|
-
configurable: true
|
|
154
|
-
});
|
|
155
|
-
Schema.prototype.listen = function (attr, callback) {
|
|
156
|
-
var _this = this;
|
|
110
|
+
}
|
|
111
|
+
get _definition() { return this.constructor._definition; }
|
|
112
|
+
listen(attr, callback) {
|
|
157
113
|
if (!this.$listeners[attr]) {
|
|
158
114
|
this.$listeners[attr] = new EventEmitter_1.EventEmitter_();
|
|
159
115
|
}
|
|
160
116
|
this.$listeners[attr].register(callback);
|
|
161
117
|
// return un-register callback.
|
|
162
|
-
return
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
if (allChanges === void 0) { allChanges = new Map(); }
|
|
170
|
-
var $root = this.$changes.root;
|
|
171
|
-
var totalBytes = bytes.length;
|
|
172
|
-
var refId = 0;
|
|
173
|
-
var changes = [];
|
|
118
|
+
return () => this.$listeners[attr].remove(callback);
|
|
119
|
+
}
|
|
120
|
+
decode(bytes, it = { offset: 0 }, ref = this, allChanges = new Map()) {
|
|
121
|
+
const $root = this.$changes.root;
|
|
122
|
+
const totalBytes = bytes.length;
|
|
123
|
+
let refId = 0;
|
|
124
|
+
let changes = [];
|
|
174
125
|
$root.refs.set(refId, this);
|
|
175
126
|
allChanges.set(refId, changes);
|
|
176
127
|
while (it.offset < totalBytes) {
|
|
177
|
-
|
|
128
|
+
let byte = bytes[it.offset++];
|
|
178
129
|
if (byte == spec_1.SWITCH_TO_STRUCTURE) {
|
|
179
130
|
refId = decode.number(bytes, it);
|
|
180
|
-
|
|
131
|
+
const nextRef = $root.refs.get(refId);
|
|
181
132
|
//
|
|
182
133
|
// Trying to access a reference that haven't been decoded yet.
|
|
183
134
|
//
|
|
184
135
|
if (!nextRef) {
|
|
185
|
-
throw new Error("
|
|
136
|
+
throw new Error(`"refId" not found: ${refId}`);
|
|
186
137
|
}
|
|
187
138
|
ref = nextRef;
|
|
188
139
|
// create empty list of changes for this refId.
|
|
@@ -190,9 +141,9 @@ var Schema = /** @class */ (function () {
|
|
|
190
141
|
allChanges.set(refId, changes);
|
|
191
142
|
continue;
|
|
192
143
|
}
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
144
|
+
const changeTree = ref['$changes'];
|
|
145
|
+
const isSchema = (ref['_definition'] !== undefined);
|
|
146
|
+
const operation = (isSchema)
|
|
196
147
|
? (byte >> 6) << 6 // "compressed" index + operation
|
|
197
148
|
: byte; // "uncompressed" index + operation (array/map items)
|
|
198
149
|
if (operation === spec_1.OPERATION.CLEAR) {
|
|
@@ -204,16 +155,16 @@ var Schema = /** @class */ (function () {
|
|
|
204
155
|
ref.clear(true);
|
|
205
156
|
continue;
|
|
206
157
|
}
|
|
207
|
-
|
|
158
|
+
const fieldIndex = (isSchema)
|
|
208
159
|
? byte % (operation || 255) // if "REPLACE" operation (0), use 255
|
|
209
160
|
: decode.number(bytes, it);
|
|
210
|
-
|
|
161
|
+
const fieldName = (isSchema)
|
|
211
162
|
? (ref['_definition'].fieldsByIndex[fieldIndex])
|
|
212
163
|
: "";
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
164
|
+
let type = changeTree.getType(fieldIndex);
|
|
165
|
+
let value;
|
|
166
|
+
let previousValue;
|
|
167
|
+
let dynamicIndex;
|
|
217
168
|
if (!isSchema) {
|
|
218
169
|
previousValue = ref['getByIndex'](fieldIndex);
|
|
219
170
|
if ((operation & spec_1.OPERATION.ADD) === spec_1.OPERATION.ADD) { // ADD or DELETE_AND_ADD
|
|
@@ -228,7 +179,7 @@ var Schema = /** @class */ (function () {
|
|
|
228
179
|
}
|
|
229
180
|
}
|
|
230
181
|
else {
|
|
231
|
-
previousValue = ref[
|
|
182
|
+
previousValue = ref[`_${fieldName}`];
|
|
232
183
|
}
|
|
233
184
|
//
|
|
234
185
|
// Delete operations
|
|
@@ -249,7 +200,7 @@ var Schema = /** @class */ (function () {
|
|
|
249
200
|
// keep skipping next bytes until reaches a known structure
|
|
250
201
|
// by local decoder.
|
|
251
202
|
//
|
|
252
|
-
|
|
203
|
+
const nextIterator = { offset: it.offset };
|
|
253
204
|
while (it.offset < totalBytes) {
|
|
254
205
|
if (decode.switchStructureCheck(bytes, it)) {
|
|
255
206
|
nextIterator.offset = it.offset + 1;
|
|
@@ -268,24 +219,24 @@ var Schema = /** @class */ (function () {
|
|
|
268
219
|
//
|
|
269
220
|
}
|
|
270
221
|
else if (Schema.is(type)) {
|
|
271
|
-
|
|
272
|
-
value = $root.refs.get(
|
|
222
|
+
const refId = decode.number(bytes, it);
|
|
223
|
+
value = $root.refs.get(refId);
|
|
273
224
|
if (operation !== spec_1.OPERATION.REPLACE) {
|
|
274
|
-
|
|
225
|
+
const childType = this.getSchemaType(bytes, it, type);
|
|
275
226
|
if (!value) {
|
|
276
227
|
value = this.createTypeInstance(childType);
|
|
277
|
-
value.$changes.refId =
|
|
228
|
+
value.$changes.refId = refId;
|
|
278
229
|
if (previousValue) {
|
|
279
230
|
value.onChange = previousValue.onChange;
|
|
280
231
|
value.onRemove = previousValue.onRemove;
|
|
281
232
|
value.$listeners = previousValue.$listeners;
|
|
282
233
|
if (previousValue['$changes'].refId &&
|
|
283
|
-
|
|
234
|
+
refId !== previousValue['$changes'].refId) {
|
|
284
235
|
$root.removeRef(previousValue['$changes'].refId);
|
|
285
236
|
}
|
|
286
237
|
}
|
|
287
238
|
}
|
|
288
|
-
$root.addRef(
|
|
239
|
+
$root.addRef(refId, value, (value !== previousValue));
|
|
289
240
|
}
|
|
290
241
|
}
|
|
291
242
|
else if (typeof (type) === "string") {
|
|
@@ -295,40 +246,40 @@ var Schema = /** @class */ (function () {
|
|
|
295
246
|
value = decodePrimitiveType(type, bytes, it);
|
|
296
247
|
}
|
|
297
248
|
else {
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
? previousValue || $root.refs.get(
|
|
249
|
+
const typeDef = (0, types_1.getType)(Object.keys(type)[0]);
|
|
250
|
+
const refId = decode.number(bytes, it);
|
|
251
|
+
const valueRef = ($root.refs.has(refId))
|
|
252
|
+
? previousValue || $root.refs.get(refId)
|
|
302
253
|
: new typeDef.constructor();
|
|
303
254
|
value = valueRef.clone(true);
|
|
304
|
-
value.$changes.refId =
|
|
255
|
+
value.$changes.refId = refId;
|
|
305
256
|
// preserve schema callbacks
|
|
306
257
|
if (previousValue) {
|
|
307
258
|
value.onAdd = previousValue.onAdd;
|
|
308
259
|
value.onRemove = previousValue.onRemove;
|
|
309
260
|
value.onChange = previousValue.onChange;
|
|
310
261
|
if (previousValue['$changes'].refId &&
|
|
311
|
-
|
|
262
|
+
refId !== previousValue['$changes'].refId) {
|
|
312
263
|
$root.removeRef(previousValue['$changes'].refId);
|
|
313
264
|
//
|
|
314
265
|
// Trigger onRemove if structure has been replaced.
|
|
315
266
|
//
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
267
|
+
const deletes = [];
|
|
268
|
+
const entries = previousValue.entries();
|
|
269
|
+
let iter;
|
|
319
270
|
while ((iter = entries.next()) && !iter.done) {
|
|
320
|
-
|
|
271
|
+
const [key, value] = iter.value;
|
|
321
272
|
deletes.push({
|
|
322
273
|
op: spec_1.OPERATION.DELETE,
|
|
323
274
|
field: key,
|
|
324
275
|
value: undefined,
|
|
325
|
-
previousValue:
|
|
276
|
+
previousValue: value,
|
|
326
277
|
});
|
|
327
278
|
}
|
|
328
279
|
allChanges.set(previousValue['$changes'].refId, deletes);
|
|
329
280
|
}
|
|
330
281
|
}
|
|
331
|
-
$root.addRef(
|
|
282
|
+
$root.addRef(refId, value, (valueRef !== previousValue));
|
|
332
283
|
//
|
|
333
284
|
// TODO: deprecate proxies on next version.
|
|
334
285
|
// get proxy to target value.
|
|
@@ -337,7 +288,7 @@ var Schema = /** @class */ (function () {
|
|
|
337
288
|
value = typeDef.getProxy(value);
|
|
338
289
|
}
|
|
339
290
|
}
|
|
340
|
-
|
|
291
|
+
let hasChange = (previousValue !== value);
|
|
341
292
|
if (value !== null &&
|
|
342
293
|
value !== undefined) {
|
|
343
294
|
if (value['$changes']) {
|
|
@@ -356,7 +307,7 @@ var Schema = /** @class */ (function () {
|
|
|
356
307
|
}
|
|
357
308
|
else if (ref instanceof MapSchema_1.MapSchema) {
|
|
358
309
|
// const key = ref['$indexes'].get(field);
|
|
359
|
-
|
|
310
|
+
const key = dynamicIndex;
|
|
360
311
|
// ref.set(key, value);
|
|
361
312
|
ref['$items'].set(key, value);
|
|
362
313
|
ref['$changes'].allChanges.add(fieldIndex);
|
|
@@ -368,11 +319,11 @@ var Schema = /** @class */ (function () {
|
|
|
368
319
|
ref.setAt(fieldIndex, value);
|
|
369
320
|
}
|
|
370
321
|
else if (ref instanceof CollectionSchema_1.CollectionSchema) {
|
|
371
|
-
|
|
322
|
+
const index = ref.add(value);
|
|
372
323
|
ref['setIndex'](fieldIndex, index);
|
|
373
324
|
}
|
|
374
325
|
else if (ref instanceof SetSchema_1.SetSchema) {
|
|
375
|
-
|
|
326
|
+
const index = ref.add(value);
|
|
376
327
|
if (index !== false) {
|
|
377
328
|
ref['setIndex'](fieldIndex, index);
|
|
378
329
|
}
|
|
@@ -387,9 +338,9 @@ var Schema = /** @class */ (function () {
|
|
|
387
338
|
changes.push({
|
|
388
339
|
op: operation,
|
|
389
340
|
field: fieldName,
|
|
390
|
-
dynamicIndex
|
|
391
|
-
value
|
|
392
|
-
previousValue
|
|
341
|
+
dynamicIndex,
|
|
342
|
+
value,
|
|
343
|
+
previousValue,
|
|
393
344
|
});
|
|
394
345
|
}
|
|
395
346
|
}
|
|
@@ -397,19 +348,16 @@ var Schema = /** @class */ (function () {
|
|
|
397
348
|
// drop references of unused schemas
|
|
398
349
|
$root.garbageCollectDeletedRefs();
|
|
399
350
|
return allChanges;
|
|
400
|
-
}
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
var changeTree = changeTrees[i];
|
|
411
|
-
var ref = changeTree.ref;
|
|
412
|
-
var isSchema = (ref instanceof Schema);
|
|
351
|
+
}
|
|
352
|
+
encode(encodeAll = false, bytes = [], useFilters = false) {
|
|
353
|
+
const rootChangeTree = this.$changes;
|
|
354
|
+
const refIdsVisited = new WeakSet();
|
|
355
|
+
const changeTrees = [rootChangeTree];
|
|
356
|
+
let numChangeTrees = 1;
|
|
357
|
+
for (let i = 0; i < numChangeTrees; i++) {
|
|
358
|
+
const changeTree = changeTrees[i];
|
|
359
|
+
const ref = changeTree.ref;
|
|
360
|
+
const isSchema = (ref instanceof Schema);
|
|
413
361
|
// Generate unique refId for the ChangeTree.
|
|
414
362
|
changeTree.ensureRefId();
|
|
415
363
|
// mark this ChangeTree as visited.
|
|
@@ -420,19 +368,19 @@ var Schema = /** @class */ (function () {
|
|
|
420
368
|
encode.uint8(bytes, spec_1.SWITCH_TO_STRUCTURE);
|
|
421
369
|
encode.number(bytes, changeTree.refId);
|
|
422
370
|
}
|
|
423
|
-
|
|
371
|
+
const changes = (encodeAll)
|
|
424
372
|
? Array.from(changeTree.allChanges)
|
|
425
373
|
: Array.from(changeTree.changes.values());
|
|
426
|
-
for (
|
|
427
|
-
|
|
374
|
+
for (let j = 0, cl = changes.length; j < cl; j++) {
|
|
375
|
+
const operation = (encodeAll)
|
|
428
376
|
? { op: spec_1.OPERATION.ADD, index: changes[j] }
|
|
429
377
|
: changes[j];
|
|
430
|
-
|
|
431
|
-
|
|
378
|
+
const fieldIndex = operation.index;
|
|
379
|
+
const field = (isSchema)
|
|
432
380
|
? ref['_definition'].fieldsByIndex && ref['_definition'].fieldsByIndex[fieldIndex]
|
|
433
381
|
: fieldIndex;
|
|
434
382
|
// cache begin index if `useFilters`
|
|
435
|
-
|
|
383
|
+
const beginIndex = bytes.length;
|
|
436
384
|
// encode field index + operation
|
|
437
385
|
if (operation.op !== spec_1.OPERATION.TOUCH) {
|
|
438
386
|
if (isSchema) {
|
|
@@ -462,7 +410,7 @@ var Schema = /** @class */ (function () {
|
|
|
462
410
|
//
|
|
463
411
|
// MapSchema dynamic key
|
|
464
412
|
//
|
|
465
|
-
|
|
413
|
+
const dynamicIndex = changeTree.ref['$indexes'].get(fieldIndex);
|
|
466
414
|
encode.string(bytes, dynamicIndex);
|
|
467
415
|
}
|
|
468
416
|
}
|
|
@@ -476,9 +424,9 @@ var Schema = /** @class */ (function () {
|
|
|
476
424
|
continue;
|
|
477
425
|
}
|
|
478
426
|
// const type = changeTree.childType || ref._schema[field];
|
|
479
|
-
|
|
427
|
+
const type = changeTree.getType(fieldIndex);
|
|
480
428
|
// const type = changeTree.getType(fieldIndex);
|
|
481
|
-
|
|
429
|
+
const value = changeTree.getValue(fieldIndex);
|
|
482
430
|
// Enqueue ChangeTree to be visited
|
|
483
431
|
if (value &&
|
|
484
432
|
value['$changes'] &&
|
|
@@ -512,11 +460,11 @@ var Schema = /** @class */ (function () {
|
|
|
512
460
|
//
|
|
513
461
|
// Custom type (MapSchema, ArraySchema, etc)
|
|
514
462
|
//
|
|
515
|
-
|
|
463
|
+
const definition = (0, types_1.getType)(Object.keys(type)[0]);
|
|
516
464
|
//
|
|
517
465
|
// ensure a ArraySchema has been provided
|
|
518
466
|
//
|
|
519
|
-
assertInstanceType(ref[
|
|
467
|
+
assertInstanceType(ref[`_${field}`], definition.constructor, ref, field);
|
|
520
468
|
//
|
|
521
469
|
// Encode refId for this instance.
|
|
522
470
|
// The actual instance is going to be encoded on next `changeTree` iteration.
|
|
@@ -533,38 +481,37 @@ var Schema = /** @class */ (function () {
|
|
|
533
481
|
}
|
|
534
482
|
}
|
|
535
483
|
return bytes;
|
|
536
|
-
}
|
|
537
|
-
|
|
484
|
+
}
|
|
485
|
+
encodeAll(useFilters) {
|
|
538
486
|
return this.encode(true, [], useFilters);
|
|
539
|
-
}
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
var _loop_1 = function (i) {
|
|
550
|
-
var changeTree = changeTrees[i];
|
|
487
|
+
}
|
|
488
|
+
applyFilters(client, encodeAll = false) {
|
|
489
|
+
const root = this;
|
|
490
|
+
const refIdsDissallowed = new Set();
|
|
491
|
+
const $filterState = filters_1.ClientState.get(client);
|
|
492
|
+
const changeTrees = [this.$changes];
|
|
493
|
+
let numChangeTrees = 1;
|
|
494
|
+
let filteredBytes = [];
|
|
495
|
+
for (let i = 0; i < numChangeTrees; i++) {
|
|
496
|
+
const changeTree = changeTrees[i];
|
|
551
497
|
if (refIdsDissallowed.has(changeTree.refId)) {
|
|
552
|
-
|
|
498
|
+
// console.log("REFID IS NOT ALLOWED. SKIP.", { refId: changeTree.refId })
|
|
499
|
+
continue;
|
|
553
500
|
}
|
|
554
|
-
|
|
555
|
-
|
|
501
|
+
const ref = changeTree.ref;
|
|
502
|
+
const isSchema = ref instanceof Schema;
|
|
556
503
|
encode.uint8(filteredBytes, spec_1.SWITCH_TO_STRUCTURE);
|
|
557
504
|
encode.number(filteredBytes, changeTree.refId);
|
|
558
|
-
|
|
559
|
-
|
|
505
|
+
const clientHasRefId = $filterState.refIds.has(changeTree);
|
|
506
|
+
const isEncodeAll = (encodeAll || !clientHasRefId);
|
|
560
507
|
// console.log("REF:", ref.constructor.name);
|
|
561
508
|
// console.log("Encode all?", isEncodeAll);
|
|
562
509
|
//
|
|
563
510
|
// include `changeTree` on list of known refIds by this client.
|
|
564
511
|
//
|
|
565
512
|
$filterState.addRefId(changeTree);
|
|
566
|
-
|
|
567
|
-
|
|
513
|
+
const containerIndexes = $filterState.containerIndexes.get(changeTree);
|
|
514
|
+
const changes = (isEncodeAll)
|
|
568
515
|
? Array.from(changeTree.allChanges)
|
|
569
516
|
: Array.from(changeTree.changes.values());
|
|
570
517
|
//
|
|
@@ -574,8 +521,8 @@ var Schema = /** @class */ (function () {
|
|
|
574
521
|
if (!encodeAll &&
|
|
575
522
|
isSchema &&
|
|
576
523
|
ref._definition.indexesWithFilters) {
|
|
577
|
-
|
|
578
|
-
indexesWithFilters.forEach(
|
|
524
|
+
const indexesWithFilters = ref._definition.indexesWithFilters;
|
|
525
|
+
indexesWithFilters.forEach(indexWithFilter => {
|
|
579
526
|
if (!containerIndexes.has(indexWithFilter) &&
|
|
580
527
|
changeTree.allChanges.has(indexWithFilter)) {
|
|
581
528
|
if (isEncodeAll) {
|
|
@@ -587,8 +534,8 @@ var Schema = /** @class */ (function () {
|
|
|
587
534
|
}
|
|
588
535
|
});
|
|
589
536
|
}
|
|
590
|
-
for (
|
|
591
|
-
|
|
537
|
+
for (let j = 0, cl = changes.length; j < cl; j++) {
|
|
538
|
+
const change = (isEncodeAll)
|
|
592
539
|
? { op: spec_1.OPERATION.ADD, index: changes[j] }
|
|
593
540
|
: changes[j];
|
|
594
541
|
// custom operations
|
|
@@ -596,7 +543,7 @@ var Schema = /** @class */ (function () {
|
|
|
596
543
|
encode.uint8(filteredBytes, change.op);
|
|
597
544
|
continue;
|
|
598
545
|
}
|
|
599
|
-
|
|
546
|
+
const fieldIndex = change.index;
|
|
600
547
|
//
|
|
601
548
|
// Deleting fields: encode the operation + field index
|
|
602
549
|
//
|
|
@@ -617,11 +564,11 @@ var Schema = /** @class */ (function () {
|
|
|
617
564
|
continue;
|
|
618
565
|
}
|
|
619
566
|
// indexed operation
|
|
620
|
-
|
|
621
|
-
|
|
567
|
+
const value = changeTree.getValue(fieldIndex);
|
|
568
|
+
const type = changeTree.getType(fieldIndex);
|
|
622
569
|
if (isSchema) {
|
|
623
570
|
// Is a Schema!
|
|
624
|
-
|
|
571
|
+
const filter = (ref._definition.filters &&
|
|
625
572
|
ref._definition.filters[fieldIndex]);
|
|
626
573
|
if (filter && !filter.call(ref, client, value, root)) {
|
|
627
574
|
if (value && value['$changes']) {
|
|
@@ -633,8 +580,8 @@ var Schema = /** @class */ (function () {
|
|
|
633
580
|
}
|
|
634
581
|
else {
|
|
635
582
|
// Is a collection! (map, array, etc.)
|
|
636
|
-
|
|
637
|
-
|
|
583
|
+
const parent = changeTree.parent;
|
|
584
|
+
const filter = changeTree.getChildrenFilter();
|
|
638
585
|
if (filter && !filter.call(parent, client, ref['$indexes'].get(fieldIndex), value, root)) {
|
|
639
586
|
if (value && value['$changes']) {
|
|
640
587
|
refIdsDissallowed.add(value['$changes'].refId);
|
|
@@ -658,7 +605,7 @@ var Schema = /** @class */ (function () {
|
|
|
658
605
|
//
|
|
659
606
|
// use cached bytes directly if is from Schema type.
|
|
660
607
|
//
|
|
661
|
-
filteredBytes.push.apply(filteredBytes,
|
|
608
|
+
filteredBytes.push.apply(filteredBytes, changeTree.caches[fieldIndex] ?? []);
|
|
662
609
|
containerIndexes.add(fieldIndex);
|
|
663
610
|
}
|
|
664
611
|
else {
|
|
@@ -666,7 +613,7 @@ var Schema = /** @class */ (function () {
|
|
|
666
613
|
//
|
|
667
614
|
// use cached bytes if already has the field
|
|
668
615
|
//
|
|
669
|
-
filteredBytes.push.apply(filteredBytes,
|
|
616
|
+
filteredBytes.push.apply(filteredBytes, changeTree.caches[fieldIndex] ?? []);
|
|
670
617
|
}
|
|
671
618
|
else {
|
|
672
619
|
//
|
|
@@ -679,7 +626,7 @@ var Schema = /** @class */ (function () {
|
|
|
679
626
|
//
|
|
680
627
|
// MapSchema dynamic key
|
|
681
628
|
//
|
|
682
|
-
|
|
629
|
+
const dynamicIndex = changeTree.ref['$indexes'].get(fieldIndex);
|
|
683
630
|
encode.string(filteredBytes, dynamicIndex);
|
|
684
631
|
}
|
|
685
632
|
if (value['$changes']) {
|
|
@@ -706,26 +653,22 @@ var Schema = /** @class */ (function () {
|
|
|
706
653
|
//
|
|
707
654
|
// MapSchema dynamic key
|
|
708
655
|
//
|
|
709
|
-
|
|
656
|
+
const dynamicIndex = changeTree.ref['$indexes'].get(fieldIndex);
|
|
710
657
|
encode.string(filteredBytes, dynamicIndex);
|
|
711
658
|
}
|
|
712
659
|
encode.number(filteredBytes, value['$changes'].refId);
|
|
713
660
|
}
|
|
714
661
|
}
|
|
715
662
|
;
|
|
716
|
-
};
|
|
717
|
-
for (var i = 0; i < numChangeTrees; i++) {
|
|
718
|
-
_loop_1(i);
|
|
719
663
|
}
|
|
720
664
|
return filteredBytes;
|
|
721
|
-
}
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
for (var field in schema) {
|
|
665
|
+
}
|
|
666
|
+
clone() {
|
|
667
|
+
const cloned = new (this.constructor);
|
|
668
|
+
const schema = this._definition.schema;
|
|
669
|
+
for (let field in schema) {
|
|
727
670
|
if (typeof (this[field]) === "object" &&
|
|
728
|
-
typeof (
|
|
671
|
+
typeof (this[field]?.clone) === "function") {
|
|
729
672
|
// deep clone
|
|
730
673
|
cloned[field] = this[field].clone();
|
|
731
674
|
}
|
|
@@ -735,13 +678,13 @@ var Schema = /** @class */ (function () {
|
|
|
735
678
|
}
|
|
736
679
|
}
|
|
737
680
|
return cloned;
|
|
738
|
-
}
|
|
739
|
-
|
|
681
|
+
}
|
|
682
|
+
triggerAll() {
|
|
740
683
|
// skip if haven't received any remote refs yet.
|
|
741
684
|
if (this.$changes.root.refs.size === 0) {
|
|
742
685
|
return;
|
|
743
686
|
}
|
|
744
|
-
|
|
687
|
+
const allChanges = new Map();
|
|
745
688
|
Schema.prototype._triggerAllFillChanges.call(this, this, allChanges);
|
|
746
689
|
try {
|
|
747
690
|
Schema.prototype._triggerChanges.call(this, allChanges);
|
|
@@ -749,65 +692,65 @@ var Schema = /** @class */ (function () {
|
|
|
749
692
|
catch (e) {
|
|
750
693
|
Schema.onError(e);
|
|
751
694
|
}
|
|
752
|
-
}
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
for (
|
|
695
|
+
}
|
|
696
|
+
toJSON() {
|
|
697
|
+
const schema = this._definition.schema;
|
|
698
|
+
const deprecated = this._definition.deprecated;
|
|
699
|
+
const obj = {};
|
|
700
|
+
for (let field in schema) {
|
|
758
701
|
if (!deprecated[field] && this[field] !== null && typeof (this[field]) !== "undefined") {
|
|
759
702
|
obj[field] = (typeof (this[field]['toJSON']) === "function")
|
|
760
703
|
? this[field]['toJSON']()
|
|
761
|
-
: this[
|
|
704
|
+
: this[`_${field}`];
|
|
762
705
|
}
|
|
763
706
|
}
|
|
764
707
|
return obj;
|
|
765
|
-
}
|
|
766
|
-
|
|
708
|
+
}
|
|
709
|
+
discardAllChanges() {
|
|
767
710
|
this.$changes.discardAll();
|
|
768
|
-
}
|
|
769
|
-
|
|
711
|
+
}
|
|
712
|
+
getByIndex(index) {
|
|
770
713
|
return this[this._definition.fieldsByIndex[index]];
|
|
771
|
-
}
|
|
772
|
-
|
|
714
|
+
}
|
|
715
|
+
deleteByIndex(index) {
|
|
773
716
|
this[this._definition.fieldsByIndex[index]] = undefined;
|
|
774
|
-
}
|
|
775
|
-
|
|
717
|
+
}
|
|
718
|
+
tryEncodeTypeId(bytes, type, targetType) {
|
|
776
719
|
if (type._typeid !== targetType._typeid) {
|
|
777
720
|
encode.uint8(bytes, spec_1.TYPE_ID);
|
|
778
721
|
encode.number(bytes, targetType._typeid);
|
|
779
722
|
}
|
|
780
|
-
}
|
|
781
|
-
|
|
782
|
-
|
|
723
|
+
}
|
|
724
|
+
getSchemaType(bytes, it, defaultType) {
|
|
725
|
+
let type;
|
|
783
726
|
if (bytes[it.offset] === spec_1.TYPE_ID) {
|
|
784
727
|
it.offset++;
|
|
785
728
|
type = this.constructor._context.get(decode.number(bytes, it));
|
|
786
729
|
}
|
|
787
730
|
return type || defaultType;
|
|
788
|
-
}
|
|
789
|
-
|
|
790
|
-
|
|
731
|
+
}
|
|
732
|
+
createTypeInstance(type) {
|
|
733
|
+
let instance = new type();
|
|
791
734
|
// assign root on $changes
|
|
792
735
|
instance.$changes.root = this.$changes.root;
|
|
793
736
|
return instance;
|
|
794
|
-
}
|
|
795
|
-
|
|
737
|
+
}
|
|
738
|
+
_triggerAllFillChanges(ref, allChanges) {
|
|
796
739
|
if (allChanges.has(ref['$changes'].refId)) {
|
|
797
740
|
return;
|
|
798
741
|
}
|
|
799
|
-
|
|
742
|
+
const changes = [];
|
|
800
743
|
allChanges.set(ref['$changes'].refId || 0, changes);
|
|
801
744
|
if (ref instanceof Schema) {
|
|
802
|
-
|
|
803
|
-
for (
|
|
804
|
-
|
|
805
|
-
|
|
745
|
+
const schema = ref._definition.schema;
|
|
746
|
+
for (let fieldName in schema) {
|
|
747
|
+
const _field = `_${fieldName}`;
|
|
748
|
+
const value = ref[_field];
|
|
806
749
|
if (value !== undefined) {
|
|
807
750
|
changes.push({
|
|
808
751
|
op: spec_1.OPERATION.ADD,
|
|
809
752
|
field: fieldName,
|
|
810
|
-
value
|
|
753
|
+
value,
|
|
811
754
|
previousValue: undefined
|
|
812
755
|
});
|
|
813
756
|
if (value['$changes'] !== undefined) {
|
|
@@ -817,10 +760,10 @@ var Schema = /** @class */ (function () {
|
|
|
817
760
|
}
|
|
818
761
|
}
|
|
819
762
|
else {
|
|
820
|
-
|
|
821
|
-
|
|
763
|
+
const entries = ref.entries();
|
|
764
|
+
let iter;
|
|
822
765
|
while ((iter = entries.next()) && !iter.done) {
|
|
823
|
-
|
|
766
|
+
const [key, value] = iter.value;
|
|
824
767
|
changes.push({
|
|
825
768
|
op: spec_1.OPERATION.ADD,
|
|
826
769
|
field: key,
|
|
@@ -833,20 +776,18 @@ var Schema = /** @class */ (function () {
|
|
|
833
776
|
}
|
|
834
777
|
}
|
|
835
778
|
}
|
|
836
|
-
}
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
allChanges.forEach(function (changes, refId) {
|
|
840
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
|
|
779
|
+
}
|
|
780
|
+
_triggerChanges(allChanges) {
|
|
781
|
+
allChanges.forEach((changes, refId) => {
|
|
841
782
|
if (changes.length > 0) {
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
for (
|
|
845
|
-
|
|
846
|
-
|
|
783
|
+
const ref = this.$changes.root.refs.get(refId);
|
|
784
|
+
const isSchema = ref instanceof Schema;
|
|
785
|
+
for (let i = 0; i < changes.length; i++) {
|
|
786
|
+
const change = changes[i];
|
|
787
|
+
const listener = ref['$listeners'] && ref['$listeners'][change.field];
|
|
847
788
|
if (!isSchema) {
|
|
848
789
|
if (change.op === spec_1.OPERATION.ADD && change.previousValue === undefined) {
|
|
849
|
-
|
|
790
|
+
ref.onAdd?.(change.value, change.dynamicIndex ?? change.field);
|
|
850
791
|
}
|
|
851
792
|
else if (change.op === spec_1.OPERATION.DELETE) {
|
|
852
793
|
//
|
|
@@ -854,18 +795,18 @@ var Schema = /** @class */ (function () {
|
|
|
854
795
|
// ADD + DELETE operations are still encoding DELETE operation.
|
|
855
796
|
//
|
|
856
797
|
if (change.previousValue !== undefined) {
|
|
857
|
-
|
|
798
|
+
ref.onRemove?.(change.previousValue, change.dynamicIndex ?? change.field);
|
|
858
799
|
}
|
|
859
800
|
}
|
|
860
801
|
else if (change.op === spec_1.OPERATION.DELETE_AND_ADD) {
|
|
861
802
|
if (change.previousValue !== undefined) {
|
|
862
|
-
|
|
803
|
+
ref.onRemove?.(change.previousValue, change.dynamicIndex);
|
|
863
804
|
}
|
|
864
|
-
|
|
805
|
+
ref.onAdd?.(change.value, change.dynamicIndex);
|
|
865
806
|
}
|
|
866
807
|
else if (change.op === spec_1.OPERATION.REPLACE ||
|
|
867
808
|
change.value !== change.previousValue) {
|
|
868
|
-
|
|
809
|
+
ref.onChange?.(change.value, change.dynamicIndex);
|
|
869
810
|
}
|
|
870
811
|
}
|
|
871
812
|
//
|
|
@@ -897,9 +838,7 @@ var Schema = /** @class */ (function () {
|
|
|
897
838
|
}
|
|
898
839
|
}
|
|
899
840
|
});
|
|
900
|
-
}
|
|
901
|
-
|
|
902
|
-
return Schema;
|
|
903
|
-
}());
|
|
841
|
+
}
|
|
842
|
+
}
|
|
904
843
|
exports.Schema = Schema;
|
|
905
844
|
//# sourceMappingURL=Schema.js.map
|