@colyseus/schema 3.0.0-alpha.0 → 3.0.0-alpha.2
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 +1181 -1390
- package/build/cjs/index.js.map +1 -1
- package/build/esm/index.mjs +35 -15
- package/build/esm/index.mjs.map +1 -1
- package/build/umd/index.js +1181 -1390
- package/lib/encoder/ChangeTree.d.ts +0 -2
- package/lib/encoder/ChangeTree.js +0 -1
- package/lib/encoder/ChangeTree.js.map +1 -1
- package/lib/encoder/Encoder.d.ts +4 -5
- package/lib/encoder/Encoder.js +14 -14
- package/lib/encoder/Encoder.js.map +1 -1
- package/lib/encoder/StateView.d.ts +2 -0
- package/lib/encoder/StateView.js +4 -1
- package/lib/encoder/StateView.js.map +1 -1
- package/lib/encoding/encode.d.ts +18 -16
- package/lib/encoding/encode.js +2 -1
- package/lib/encoding/encode.js.map +1 -1
- package/package.json +1 -1
- package/src/encoder/ChangeTree.ts +0 -2
- package/src/encoder/Encoder.ts +17 -18
- package/src/encoder/StateView.ts +4 -0
- package/src/encoding/encode.ts +19 -16
package/build/cjs/index.js
CHANGED
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
const SWITCH_TO_STRUCTURE = 255; // (decoding collides with DELETE_AND_ADD + fieldIndex = 63)
|
|
6
|
+
const TYPE_ID = 213;
|
|
7
7
|
/**
|
|
8
8
|
* Encoding Schema field operations.
|
|
9
9
|
*/
|
|
@@ -29,95 +29,40 @@ exports.OPERATION = void 0;
|
|
|
29
29
|
OPERATION[OPERATION["DELETE_BY_REFID"] = 33] = "DELETE_BY_REFID";
|
|
30
30
|
})(exports.OPERATION || (exports.OPERATION = {}));
|
|
31
31
|
|
|
32
|
-
|
|
33
|
-
Copyright (c) Microsoft Corporation.
|
|
34
|
-
|
|
35
|
-
Permission to use, copy, modify, and/or distribute this software for any
|
|
36
|
-
purpose with or without fee is hereby granted.
|
|
37
|
-
|
|
38
|
-
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
39
|
-
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
40
|
-
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
41
|
-
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
42
|
-
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
43
|
-
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
44
|
-
PERFORMANCE OF THIS SOFTWARE.
|
|
45
|
-
***************************************************************************** */
|
|
46
|
-
/* global Reflect, Promise, SuppressedError, Symbol */
|
|
47
|
-
|
|
48
|
-
var extendStatics = function(d, b) {
|
|
49
|
-
extendStatics = Object.setPrototypeOf ||
|
|
50
|
-
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
51
|
-
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
52
|
-
return extendStatics(d, b);
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
function __extends(d, b) {
|
|
56
|
-
if (typeof b !== "function" && b !== null)
|
|
57
|
-
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
58
|
-
extendStatics(d, b);
|
|
59
|
-
function __() { this.constructor = d; }
|
|
60
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
function __decorate(decorators, target, key, desc) {
|
|
64
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
65
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
66
|
-
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;
|
|
67
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
function __spreadArray(to, from, pack) {
|
|
71
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
72
|
-
if (ar || !(i in from)) {
|
|
73
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
74
|
-
ar[i] = from[i];
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
81
|
-
var e = new Error(message);
|
|
82
|
-
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
83
|
-
};
|
|
84
|
-
|
|
85
|
-
var _a$1;
|
|
86
|
-
var _b;
|
|
87
|
-
(_a$1 = (_b = Symbol).metadata) !== null && _a$1 !== void 0 ? _a$1 : (_b.metadata = Symbol.for("Symbol.metadata"));
|
|
32
|
+
Symbol.metadata ??= Symbol.for("Symbol.metadata");
|
|
88
33
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
34
|
+
const $track = Symbol("$track");
|
|
35
|
+
const $encoder = Symbol("$encoder");
|
|
36
|
+
const $decoder = Symbol("$decoder");
|
|
37
|
+
const $filter = Symbol("$filter");
|
|
38
|
+
const $getByIndex = Symbol("$getByIndex");
|
|
39
|
+
const $deleteByIndex = Symbol("$deleteByIndex");
|
|
95
40
|
/**
|
|
96
41
|
* Used to hold ChangeTree instances whitin the structures
|
|
97
42
|
*/
|
|
98
|
-
|
|
43
|
+
const $changes = Symbol('$changes');
|
|
99
44
|
/**
|
|
100
45
|
* Used to keep track of the type of the child elements of a collection
|
|
101
46
|
* (MapSchema, ArraySchema, etc.)
|
|
102
47
|
*/
|
|
103
|
-
|
|
48
|
+
const $childType = Symbol('$childType');
|
|
104
49
|
/**
|
|
105
50
|
* Special ChangeTree property to identify new instances
|
|
106
51
|
* (Once they're encoded, they're not new anymore)
|
|
107
52
|
*/
|
|
108
|
-
|
|
53
|
+
const $isNew = Symbol("$isNew");
|
|
109
54
|
/**
|
|
110
55
|
* Optional "discard" method for custom types (ArraySchema)
|
|
111
56
|
* (Discards changes for next serialization)
|
|
112
57
|
*/
|
|
113
|
-
|
|
58
|
+
const $onEncodeEnd = Symbol('$onEncodeEnd');
|
|
114
59
|
/**
|
|
115
60
|
* When decoding, this method is called after the instance is fully decoded
|
|
116
61
|
*/
|
|
117
|
-
|
|
62
|
+
const $onDecodeEnd = Symbol("$onDecodeEnd");
|
|
118
63
|
|
|
119
|
-
|
|
120
|
-
|
|
64
|
+
const registeredTypes = {};
|
|
65
|
+
const identifiers = new Map();
|
|
121
66
|
function registerType(identifier, definition) {
|
|
122
67
|
identifiers.set(definition.constructor, identifier);
|
|
123
68
|
registeredTypes[identifier] = definition;
|
|
@@ -126,18 +71,18 @@ function getType(identifier) {
|
|
|
126
71
|
return registeredTypes[identifier];
|
|
127
72
|
}
|
|
128
73
|
|
|
129
|
-
|
|
130
|
-
addField
|
|
74
|
+
const Metadata = {
|
|
75
|
+
addField(metadata, index, field, type, descriptor) {
|
|
131
76
|
if (index > 64) {
|
|
132
|
-
throw new Error(
|
|
77
|
+
throw new Error(`Can't define field '${field}'.\nSchema instances may only have up to 64 fields.`);
|
|
133
78
|
}
|
|
134
79
|
metadata[field] = Object.assign(metadata[field] || {}, // avoid overwriting previous field metadata (@owned / @deprecated)
|
|
135
80
|
{
|
|
136
81
|
type: (Array.isArray(type))
|
|
137
82
|
? { array: type[0] }
|
|
138
83
|
: type,
|
|
139
|
-
index
|
|
140
|
-
descriptor
|
|
84
|
+
index,
|
|
85
|
+
descriptor,
|
|
141
86
|
});
|
|
142
87
|
// map -1 as last field index
|
|
143
88
|
Object.defineProperty(metadata, -1, {
|
|
@@ -152,9 +97,9 @@ var Metadata = {
|
|
|
152
97
|
configurable: true,
|
|
153
98
|
});
|
|
154
99
|
},
|
|
155
|
-
setTag
|
|
100
|
+
setTag(metadata, fieldName, tag) {
|
|
156
101
|
// add 'tag' to the field
|
|
157
|
-
|
|
102
|
+
const field = metadata[fieldName];
|
|
158
103
|
field.tag = tag;
|
|
159
104
|
if (!metadata[-2]) {
|
|
160
105
|
// -2: all field indexes with "view" tag
|
|
@@ -176,47 +121,45 @@ var Metadata = {
|
|
|
176
121
|
}
|
|
177
122
|
metadata[-3][tag].push(field.index);
|
|
178
123
|
},
|
|
179
|
-
setFields
|
|
180
|
-
|
|
181
|
-
var _b, _c;
|
|
182
|
-
var metadata = ((_a = (_b = target.prototype.constructor)[_c = Symbol.metadata]) !== null && _a !== void 0 ? _a : (_b[_c] = {}));
|
|
124
|
+
setFields(target, fields) {
|
|
125
|
+
const metadata = (target.prototype.constructor[Symbol.metadata] ??= {});
|
|
183
126
|
// target[$track] = function (changeTree, index: number, operation: OPERATION = OPERATION.ADD) {
|
|
184
127
|
// changeTree.change(index, operation, encodeSchemaOperation);
|
|
185
128
|
// };
|
|
186
129
|
// target[$encoder] = encodeSchemaOperation;
|
|
187
130
|
// target[$decoder] = decodeSchemaOperation;
|
|
188
131
|
// if (!target.prototype.toJSON) { target.prototype.toJSON = Schema.prototype.toJSON; }
|
|
189
|
-
|
|
190
|
-
for (
|
|
191
|
-
|
|
132
|
+
let index = 0;
|
|
133
|
+
for (const field in fields) {
|
|
134
|
+
const type = fields[field];
|
|
192
135
|
// FIXME: this code is duplicated from @type() annotation
|
|
193
|
-
|
|
136
|
+
const complexTypeKlass = (Array.isArray(type))
|
|
194
137
|
? getType("array")
|
|
195
138
|
: (typeof (Object.keys(type)[0]) === "string") && getType(Object.keys(type)[0]);
|
|
196
|
-
Metadata.addField(metadata, index, field, type, getPropertyDescriptor(
|
|
139
|
+
Metadata.addField(metadata, index, field, type, getPropertyDescriptor(`_${field}`, index, type, complexTypeKlass, metadata, field));
|
|
197
140
|
index++;
|
|
198
141
|
}
|
|
199
142
|
},
|
|
200
|
-
isDeprecated
|
|
143
|
+
isDeprecated(metadata, field) {
|
|
201
144
|
return metadata[field].deprecated === true;
|
|
202
145
|
},
|
|
203
|
-
isValidInstance
|
|
146
|
+
isValidInstance(klass) {
|
|
204
147
|
return (klass.constructor[Symbol.metadata] &&
|
|
205
148
|
Object.prototype.hasOwnProperty.call(klass.constructor[Symbol.metadata], -1));
|
|
206
149
|
},
|
|
207
|
-
getFields
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
for (
|
|
150
|
+
getFields(klass) {
|
|
151
|
+
const metadata = klass[Symbol.metadata];
|
|
152
|
+
const fields = {};
|
|
153
|
+
for (let i = 0; i <= metadata[-1]; i++) {
|
|
211
154
|
fields[metadata[i]] = metadata[metadata[i]].type;
|
|
212
155
|
}
|
|
213
156
|
return fields;
|
|
214
157
|
}
|
|
215
158
|
};
|
|
216
159
|
|
|
217
|
-
var _a;
|
|
218
|
-
|
|
219
|
-
|
|
160
|
+
var _a$5;
|
|
161
|
+
class Root {
|
|
162
|
+
constructor() {
|
|
220
163
|
this.nextUniqueId = 0;
|
|
221
164
|
this.refCount = new WeakMap();
|
|
222
165
|
// all changes
|
|
@@ -225,18 +168,16 @@ var Root = /** @class */ (function () {
|
|
|
225
168
|
// pending changes to be encoded
|
|
226
169
|
this.changes = new Map();
|
|
227
170
|
this.filteredChanges = new Map();
|
|
228
|
-
this.views = [];
|
|
229
171
|
}
|
|
230
|
-
|
|
172
|
+
getNextUniqueId() {
|
|
231
173
|
return this.nextUniqueId++;
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
|
|
174
|
+
}
|
|
175
|
+
add(changeTree) {
|
|
176
|
+
const refCount = this.refCount.get(changeTree) || 0;
|
|
235
177
|
this.refCount.set(changeTree, refCount + 1);
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
var refCount = this.refCount.get(changeTree);
|
|
178
|
+
}
|
|
179
|
+
remove(changeTree) {
|
|
180
|
+
const refCount = this.refCount.get(changeTree);
|
|
240
181
|
if (refCount <= 1) {
|
|
241
182
|
this.allChanges.delete(changeTree);
|
|
242
183
|
this.changes.delete(changeTree);
|
|
@@ -249,27 +190,26 @@ var Root = /** @class */ (function () {
|
|
|
249
190
|
else {
|
|
250
191
|
this.refCount.set(changeTree, refCount - 1);
|
|
251
192
|
}
|
|
252
|
-
changeTree.forEachChild(
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
};
|
|
256
|
-
Root.prototype.clear = function () {
|
|
193
|
+
changeTree.forEachChild((child, _) => this.remove(child));
|
|
194
|
+
}
|
|
195
|
+
clear() {
|
|
257
196
|
this.changes.clear();
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
class ChangeTree {
|
|
200
|
+
static { _a$5 = $isNew; }
|
|
201
|
+
;
|
|
202
|
+
constructor(ref) {
|
|
263
203
|
this.indexes = {}; // TODO: remove this, only used by MapSchema/SetSchema/CollectionSchema (`encodeKeyValueOperation`)
|
|
264
204
|
this.currentOperationIndex = 0;
|
|
265
205
|
this.allChanges = new Map();
|
|
266
206
|
this.allFilteredChanges = new Map();
|
|
267
207
|
this.changes = new Map();
|
|
268
208
|
this.filteredChanges = new Map();
|
|
269
|
-
this[_a] = true;
|
|
209
|
+
this[_a$5] = true;
|
|
270
210
|
this.ref = ref;
|
|
271
211
|
}
|
|
272
|
-
|
|
212
|
+
setRoot(root) {
|
|
273
213
|
this.root = root;
|
|
274
214
|
this.root.add(this);
|
|
275
215
|
//
|
|
@@ -293,7 +233,7 @@ var ChangeTree = /** @class */ (function () {
|
|
|
293
233
|
if (!this.isFiltered) {
|
|
294
234
|
this.root.allChanges.set(this, this.allChanges);
|
|
295
235
|
}
|
|
296
|
-
this.forEachChild(
|
|
236
|
+
this.forEachChild((changeTree, _) => {
|
|
297
237
|
changeTree.setRoot(root);
|
|
298
238
|
});
|
|
299
239
|
// this.allChanges.forEach((_, index) => {
|
|
@@ -302,9 +242,8 @@ var ChangeTree = /** @class */ (function () {
|
|
|
302
242
|
// childRef[$changes].setRoot(root);
|
|
303
243
|
// }
|
|
304
244
|
// });
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
var _this = this;
|
|
245
|
+
}
|
|
246
|
+
setParent(parent, root, parentIndex) {
|
|
308
247
|
this.parent = parent;
|
|
309
248
|
this.parentIndex = parentIndex;
|
|
310
249
|
// avoid setting parents with empty `root`
|
|
@@ -314,8 +253,8 @@ var ChangeTree = /** @class */ (function () {
|
|
|
314
253
|
root.add(this);
|
|
315
254
|
// skip if parent is already set
|
|
316
255
|
if (root === this.root) {
|
|
317
|
-
this.forEachChild(
|
|
318
|
-
changeTree.setParent(
|
|
256
|
+
this.forEachChild((changeTree, atIndex) => {
|
|
257
|
+
changeTree.setParent(this.ref, root, atIndex);
|
|
319
258
|
});
|
|
320
259
|
return;
|
|
321
260
|
}
|
|
@@ -332,20 +271,19 @@ var ChangeTree = /** @class */ (function () {
|
|
|
332
271
|
this.root.allChanges.set(this, this.allChanges);
|
|
333
272
|
}
|
|
334
273
|
this.ensureRefId();
|
|
335
|
-
this.forEachChild(
|
|
336
|
-
changeTree.setParent(
|
|
274
|
+
this.forEachChild((changeTree, atIndex) => {
|
|
275
|
+
changeTree.setParent(this.ref, root, atIndex);
|
|
337
276
|
});
|
|
338
|
-
}
|
|
339
|
-
|
|
340
|
-
var _this = this;
|
|
277
|
+
}
|
|
278
|
+
forEachChild(callback) {
|
|
341
279
|
//
|
|
342
280
|
// assign same parent on child structures
|
|
343
281
|
//
|
|
344
282
|
if (Metadata.isValidInstance(this.ref)) {
|
|
345
|
-
|
|
283
|
+
const metadata = this.ref['constructor'][Symbol.metadata];
|
|
346
284
|
// FIXME: need to iterate over parent metadata instead.
|
|
347
|
-
for (
|
|
348
|
-
|
|
285
|
+
for (const field in metadata) {
|
|
286
|
+
const value = this.ref[field];
|
|
349
287
|
if (value && value[$changes]) {
|
|
350
288
|
callback(value[$changes], metadata[field].index);
|
|
351
289
|
}
|
|
@@ -353,29 +291,26 @@ var ChangeTree = /** @class */ (function () {
|
|
|
353
291
|
}
|
|
354
292
|
else if (typeof (this.ref) === "object") {
|
|
355
293
|
// MapSchema / ArraySchema, etc.
|
|
356
|
-
this.ref.forEach(
|
|
294
|
+
this.ref.forEach((value, key) => {
|
|
357
295
|
if (Metadata.isValidInstance(value)) {
|
|
358
|
-
callback(value[$changes],
|
|
296
|
+
callback(value[$changes], this.ref[$changes].indexes[key]);
|
|
359
297
|
}
|
|
360
298
|
});
|
|
361
299
|
}
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
var _b;
|
|
300
|
+
}
|
|
301
|
+
operation(op) {
|
|
365
302
|
this.changes.set(--this.currentOperationIndex, op);
|
|
366
|
-
|
|
367
|
-
}
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
var isFiltered = this.isFiltered || (metadata && metadata[metadata[index]].tag !== undefined);
|
|
373
|
-
var changeSet = (isFiltered)
|
|
303
|
+
this.root?.changes.set(this, this.changes);
|
|
304
|
+
}
|
|
305
|
+
change(index, operation = exports.OPERATION.ADD) {
|
|
306
|
+
const metadata = this.ref['constructor'][Symbol.metadata];
|
|
307
|
+
const isFiltered = this.isFiltered || (metadata && metadata[metadata[index]].tag !== undefined);
|
|
308
|
+
const changeSet = (isFiltered)
|
|
374
309
|
? this.filteredChanges
|
|
375
310
|
: this.changes;
|
|
376
|
-
|
|
311
|
+
const previousOperation = changeSet.get(index);
|
|
377
312
|
if (!previousOperation || previousOperation === exports.OPERATION.DELETE) {
|
|
378
|
-
|
|
313
|
+
const op = (!previousOperation)
|
|
379
314
|
? operation
|
|
380
315
|
: (previousOperation === exports.OPERATION.DELETE)
|
|
381
316
|
? exports.OPERATION.DELETE_AND_ADD
|
|
@@ -387,32 +322,30 @@ var ChangeTree = /** @class */ (function () {
|
|
|
387
322
|
//
|
|
388
323
|
if (isFiltered) {
|
|
389
324
|
this.allFilteredChanges.set(index, exports.OPERATION.ADD);
|
|
390
|
-
|
|
325
|
+
this.root?.filteredChanges.set(this, this.filteredChanges);
|
|
391
326
|
}
|
|
392
327
|
else {
|
|
393
328
|
this.allChanges.set(index, exports.OPERATION.ADD);
|
|
394
|
-
|
|
329
|
+
this.root?.changes.set(this, this.changes);
|
|
395
330
|
}
|
|
396
|
-
}
|
|
397
|
-
|
|
331
|
+
}
|
|
332
|
+
shiftChangeIndexes(shiftIndex) {
|
|
398
333
|
//
|
|
399
334
|
// Used only during:
|
|
400
335
|
//
|
|
401
336
|
// - ArraySchema#unshift()
|
|
402
337
|
//
|
|
403
|
-
|
|
338
|
+
const changeSet = (this.isFiltered)
|
|
404
339
|
? this.filteredChanges
|
|
405
340
|
: this.changes;
|
|
406
|
-
|
|
341
|
+
const changeSetEntries = Array.from(changeSet.entries());
|
|
407
342
|
changeSet.clear();
|
|
408
343
|
// Re-insert each entry with the shifted index
|
|
409
|
-
for (
|
|
410
|
-
var _b = changeSetEntries_1[_i], index = _b[0], op = _b[1];
|
|
344
|
+
for (const [index, op] of changeSetEntries) {
|
|
411
345
|
changeSet.set(index + shiftIndex, op);
|
|
412
346
|
}
|
|
413
|
-
}
|
|
414
|
-
|
|
415
|
-
if (startIndex === void 0) { startIndex = 0; }
|
|
347
|
+
}
|
|
348
|
+
shiftAllChangeIndexes(shiftIndex, startIndex = 0) {
|
|
416
349
|
//
|
|
417
350
|
// Used only during:
|
|
418
351
|
//
|
|
@@ -425,37 +358,33 @@ var ChangeTree = /** @class */ (function () {
|
|
|
425
358
|
else {
|
|
426
359
|
this._shiftAllChangeIndexes(shiftIndex, startIndex, this.allChanges);
|
|
427
360
|
}
|
|
428
|
-
}
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
Array.from(allChangeSet.entries()).forEach(function (_b) {
|
|
432
|
-
var index = _b[0], op = _b[1];
|
|
361
|
+
}
|
|
362
|
+
_shiftAllChangeIndexes(shiftIndex, startIndex = 0, allChangeSet) {
|
|
363
|
+
Array.from(allChangeSet.entries()).forEach(([index, op]) => {
|
|
433
364
|
// console.log('shiftAllChangeIndexes', index >= startIndex, { index, op, shiftIndex, startIndex })
|
|
434
365
|
if (index >= startIndex) {
|
|
435
366
|
allChangeSet.delete(index);
|
|
436
367
|
allChangeSet.set(index + shiftIndex, op);
|
|
437
368
|
}
|
|
438
369
|
});
|
|
439
|
-
}
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
var metadata = this.ref['constructor'][Symbol.metadata];
|
|
444
|
-
var isFiltered = this.isFiltered || (metadata && metadata[metadata[index]].tag !== undefined);
|
|
370
|
+
}
|
|
371
|
+
indexedOperation(index, operation, allChangesIndex = index) {
|
|
372
|
+
const metadata = this.ref['constructor'][Symbol.metadata];
|
|
373
|
+
const isFiltered = this.isFiltered || (metadata && metadata[metadata[index]].tag !== undefined);
|
|
445
374
|
if (isFiltered) {
|
|
446
375
|
this.allFilteredChanges.set(allChangesIndex, exports.OPERATION.ADD);
|
|
447
376
|
this.filteredChanges.set(index, operation);
|
|
448
|
-
|
|
377
|
+
this.root?.filteredChanges.set(this, this.filteredChanges);
|
|
449
378
|
}
|
|
450
379
|
else {
|
|
451
380
|
this.allChanges.set(allChangesIndex, exports.OPERATION.ADD);
|
|
452
381
|
this.changes.set(index, operation);
|
|
453
|
-
|
|
382
|
+
this.root?.changes.set(this, this.changes);
|
|
454
383
|
}
|
|
455
|
-
}
|
|
456
|
-
|
|
384
|
+
}
|
|
385
|
+
getType(index) {
|
|
457
386
|
if (Metadata.isValidInstance(this.ref)) {
|
|
458
|
-
|
|
387
|
+
const metadata = this.ref['constructor'][Symbol.metadata];
|
|
459
388
|
return metadata[metadata[index]].type;
|
|
460
389
|
}
|
|
461
390
|
else {
|
|
@@ -467,41 +396,37 @@ var ChangeTree = /** @class */ (function () {
|
|
|
467
396
|
//
|
|
468
397
|
return this.ref[$childType];
|
|
469
398
|
}
|
|
470
|
-
}
|
|
471
|
-
|
|
472
|
-
var _b;
|
|
399
|
+
}
|
|
400
|
+
getChange(index) {
|
|
473
401
|
// TODO: optimize this. avoid checking against multiple instances
|
|
474
|
-
return
|
|
475
|
-
}
|
|
402
|
+
return this.changes.get(index) ?? this.filteredChanges.get(index);
|
|
403
|
+
}
|
|
476
404
|
//
|
|
477
405
|
// used during `.encode()`
|
|
478
406
|
//
|
|
479
|
-
|
|
480
|
-
if (isEncodeAll === void 0) { isEncodeAll = false; }
|
|
407
|
+
getValue(index, isEncodeAll = false) {
|
|
481
408
|
//
|
|
482
409
|
// `isEncodeAll` param is only used by ArraySchema
|
|
483
410
|
//
|
|
484
411
|
return this.ref[$getByIndex](index, isEncodeAll);
|
|
485
|
-
}
|
|
486
|
-
|
|
487
|
-
var _b, _c, _d;
|
|
488
|
-
if (allChangesIndex === void 0) { allChangesIndex = index; }
|
|
412
|
+
}
|
|
413
|
+
delete(index, operation, allChangesIndex = index) {
|
|
489
414
|
if (index === undefined) {
|
|
490
415
|
try {
|
|
491
|
-
throw new Error(
|
|
416
|
+
throw new Error(`@colyseus/schema ${this.ref.constructor.name}: trying to delete non-existing index '${index}'`);
|
|
492
417
|
}
|
|
493
418
|
catch (e) {
|
|
494
419
|
console.warn(e);
|
|
495
420
|
}
|
|
496
421
|
return;
|
|
497
422
|
}
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
423
|
+
const metadata = this.ref['constructor'][Symbol.metadata];
|
|
424
|
+
const isFiltered = this.isFiltered || (metadata && metadata[metadata[index]].tag !== undefined);
|
|
425
|
+
const changeSet = (isFiltered)
|
|
501
426
|
? this.filteredChanges
|
|
502
427
|
: this.changes;
|
|
503
|
-
|
|
504
|
-
changeSet.set(index, operation
|
|
428
|
+
const previousValue = this.getValue(index);
|
|
429
|
+
changeSet.set(index, operation ?? exports.OPERATION.DELETE);
|
|
505
430
|
// remove `root` reference
|
|
506
431
|
if (previousValue && previousValue[$changes]) {
|
|
507
432
|
previousValue[$changes].root = undefined;
|
|
@@ -515,37 +440,33 @@ var ChangeTree = /** @class */ (function () {
|
|
|
515
440
|
//
|
|
516
441
|
// (the property descriptors should NOT be used at decoding time. only at encoding time.)
|
|
517
442
|
//
|
|
518
|
-
|
|
443
|
+
this.root?.remove(previousValue[$changes]);
|
|
519
444
|
}
|
|
520
445
|
//
|
|
521
446
|
// FIXME: this is looking a bit ugly (and repeated from `.change()`)
|
|
522
447
|
//
|
|
523
448
|
if (isFiltered) {
|
|
524
|
-
|
|
449
|
+
this.root?.filteredChanges.set(this, this.filteredChanges);
|
|
525
450
|
this.allFilteredChanges.delete(allChangesIndex);
|
|
526
451
|
}
|
|
527
452
|
else {
|
|
528
|
-
|
|
453
|
+
this.root?.changes.set(this, this.changes);
|
|
529
454
|
this.allChanges.delete(allChangesIndex);
|
|
530
455
|
}
|
|
531
|
-
}
|
|
532
|
-
|
|
533
|
-
var _b, _c;
|
|
456
|
+
}
|
|
457
|
+
endEncode() {
|
|
534
458
|
this.changes.clear();
|
|
535
|
-
|
|
459
|
+
this.ref[$onEncodeEnd]?.();
|
|
536
460
|
// Not a new instance anymore
|
|
537
461
|
delete this[$isNew];
|
|
538
|
-
}
|
|
539
|
-
|
|
540
|
-
var _this = this;
|
|
541
|
-
var _b, _c;
|
|
542
|
-
if (discardAll === void 0) { discardAll = false; }
|
|
462
|
+
}
|
|
463
|
+
discard(discardAll = false) {
|
|
543
464
|
//
|
|
544
465
|
// > MapSchema:
|
|
545
466
|
// Remove cached key to ensure ADD operations is unsed instead of
|
|
546
467
|
// REPLACE in case same key is used on next patches.
|
|
547
468
|
//
|
|
548
|
-
|
|
469
|
+
this.ref[$onEncodeEnd]?.();
|
|
549
470
|
this.changes.clear();
|
|
550
471
|
this.filteredChanges.clear();
|
|
551
472
|
// reset operation index
|
|
@@ -554,46 +475,40 @@ var ChangeTree = /** @class */ (function () {
|
|
|
554
475
|
this.allChanges.clear();
|
|
555
476
|
this.allFilteredChanges.clear();
|
|
556
477
|
// remove children references
|
|
557
|
-
this.forEachChild(
|
|
478
|
+
this.forEachChild((changeTree, _) => this.root?.remove(changeTree));
|
|
558
479
|
}
|
|
559
|
-
}
|
|
480
|
+
}
|
|
560
481
|
/**
|
|
561
482
|
* Recursively discard all changes from this, and child structures.
|
|
562
483
|
*/
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
var value = _this.getValue(fieldIndex);
|
|
484
|
+
discardAll() {
|
|
485
|
+
this.changes.forEach((_, fieldIndex) => {
|
|
486
|
+
const value = this.getValue(fieldIndex);
|
|
567
487
|
if (value && value[$changes]) {
|
|
568
488
|
value[$changes].discardAll();
|
|
569
489
|
}
|
|
570
490
|
});
|
|
571
491
|
this.discard();
|
|
572
|
-
}
|
|
573
|
-
|
|
492
|
+
}
|
|
493
|
+
ensureRefId() {
|
|
574
494
|
// skip if refId is already set.
|
|
575
495
|
if (this.refId !== undefined) {
|
|
576
496
|
return;
|
|
577
497
|
}
|
|
578
498
|
this.refId = this.root.getNextUniqueId();
|
|
579
|
-
}
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
enumerable: false,
|
|
585
|
-
configurable: true
|
|
586
|
-
});
|
|
587
|
-
ChangeTree.prototype.checkIsFiltered = function (parent, parentIndex) {
|
|
588
|
-
var _b, _c, _d;
|
|
499
|
+
}
|
|
500
|
+
get changed() {
|
|
501
|
+
return this.changes.size > 0;
|
|
502
|
+
}
|
|
503
|
+
checkIsFiltered(parent, parentIndex) {
|
|
589
504
|
// Detect if current structure has "filters" declared
|
|
590
|
-
this.isPartiallyFiltered =
|
|
505
|
+
this.isPartiallyFiltered = this.ref['constructor']?.[Symbol.metadata]?.[-2];
|
|
591
506
|
// TODO: support "partially filtered", where the instance is visible, but only a field is not.
|
|
592
507
|
// Detect if parent has "filters" declared
|
|
593
508
|
while (parent && !this.isFiltered) {
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
509
|
+
const metadata = parent['constructor'][Symbol.metadata];
|
|
510
|
+
const fieldName = metadata?.[parentIndex];
|
|
511
|
+
const isParentOwned = metadata?.[fieldName]?.tag !== undefined;
|
|
597
512
|
this.isFiltered = isParentOwned || parent[$changes].isFiltered; // metadata?.[-2]
|
|
598
513
|
parent = parent[$changes].parent;
|
|
599
514
|
}
|
|
@@ -605,18 +520,16 @@ var ChangeTree = /** @class */ (function () {
|
|
|
605
520
|
//
|
|
606
521
|
if (this.isFiltered && this.changes.size > 0) {
|
|
607
522
|
// swap changes reference
|
|
608
|
-
|
|
523
|
+
const changes = this.changes;
|
|
609
524
|
this.changes = this.filteredChanges;
|
|
610
525
|
this.filteredChanges = changes;
|
|
611
526
|
// swap "all changes" reference
|
|
612
|
-
|
|
527
|
+
const allFilteredChanges = this.allFilteredChanges;
|
|
613
528
|
this.allFilteredChanges = this.allChanges;
|
|
614
529
|
this.allChanges = allFilteredChanges;
|
|
615
530
|
}
|
|
616
|
-
}
|
|
617
|
-
|
|
618
|
-
}());
|
|
619
|
-
_a = $isNew;
|
|
531
|
+
}
|
|
532
|
+
}
|
|
620
533
|
|
|
621
534
|
/**
|
|
622
535
|
* Copyright (c) 2018 Endel Dreyer
|
|
@@ -644,12 +557,32 @@ _a = $isNew;
|
|
|
644
557
|
* msgpack implementation highly based on notepack.io
|
|
645
558
|
* https://github.com/darrachequesne/notepack
|
|
646
559
|
*/
|
|
647
|
-
|
|
560
|
+
let textEncoder;
|
|
648
561
|
// @ts-ignore
|
|
649
562
|
try {
|
|
650
563
|
textEncoder = new TextEncoder();
|
|
651
564
|
}
|
|
652
565
|
catch (e) { }
|
|
566
|
+
function utf8Length(str) {
|
|
567
|
+
var c = 0, length = 0;
|
|
568
|
+
for (var i = 0, l = str.length; i < l; i++) {
|
|
569
|
+
c = str.charCodeAt(i);
|
|
570
|
+
if (c < 0x80) {
|
|
571
|
+
length += 1;
|
|
572
|
+
}
|
|
573
|
+
else if (c < 0x800) {
|
|
574
|
+
length += 2;
|
|
575
|
+
}
|
|
576
|
+
else if (c < 0xd800 || c >= 0xe000) {
|
|
577
|
+
length += 3;
|
|
578
|
+
}
|
|
579
|
+
else {
|
|
580
|
+
i++;
|
|
581
|
+
length += 4;
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
return length;
|
|
585
|
+
}
|
|
653
586
|
function utf8Write(view, str, it) {
|
|
654
587
|
var c = 0;
|
|
655
588
|
for (var i = 0, l = str.length; i < l; i++) {
|
|
@@ -697,24 +630,24 @@ function int32$1(bytes, value, it) {
|
|
|
697
630
|
bytes[it.offset++] = (value >> 24) & 255;
|
|
698
631
|
}
|
|
699
632
|
function uint32$1(bytes, value, it) {
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
633
|
+
const b4 = value >> 24;
|
|
634
|
+
const b3 = value >> 16;
|
|
635
|
+
const b2 = value >> 8;
|
|
636
|
+
const b1 = value;
|
|
704
637
|
bytes[it.offset++] = b1 & 255;
|
|
705
638
|
bytes[it.offset++] = b2 & 255;
|
|
706
639
|
bytes[it.offset++] = b3 & 255;
|
|
707
640
|
bytes[it.offset++] = b4 & 255;
|
|
708
641
|
}
|
|
709
642
|
function int64$1(bytes, value, it) {
|
|
710
|
-
|
|
711
|
-
|
|
643
|
+
const high = Math.floor(value / Math.pow(2, 32));
|
|
644
|
+
const low = value >>> 0;
|
|
712
645
|
uint32$1(bytes, low, it);
|
|
713
646
|
uint32$1(bytes, high, it);
|
|
714
647
|
}
|
|
715
648
|
function uint64$1(bytes, value, it) {
|
|
716
|
-
|
|
717
|
-
|
|
649
|
+
const high = (value / Math.pow(2, 32)) >> 0;
|
|
650
|
+
const low = value >>> 0;
|
|
718
651
|
uint32$1(bytes, low, it);
|
|
719
652
|
uint32$1(bytes, high, it);
|
|
720
653
|
}
|
|
@@ -724,9 +657,9 @@ function float32$1(bytes, value, it) {
|
|
|
724
657
|
function float64$1(bytes, value, it) {
|
|
725
658
|
writeFloat64(bytes, value, it);
|
|
726
659
|
}
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
660
|
+
const _int32$1 = new Int32Array(2);
|
|
661
|
+
const _float32$1 = new Float32Array(_int32$1.buffer);
|
|
662
|
+
const _float64$1 = new Float64Array(_int32$1.buffer);
|
|
730
663
|
function writeFloat32(bytes, value, it) {
|
|
731
664
|
_float32$1[0] = value;
|
|
732
665
|
int32$1(bytes, _int32$1[0], it);
|
|
@@ -745,8 +678,8 @@ function string$1(bytes, value, it) {
|
|
|
745
678
|
value = "";
|
|
746
679
|
}
|
|
747
680
|
// let length = utf8Length(value);
|
|
748
|
-
|
|
749
|
-
|
|
681
|
+
let length = Buffer.byteLength(value, "utf8");
|
|
682
|
+
let size = 0;
|
|
750
683
|
// fixstr
|
|
751
684
|
if (length < 0x20) {
|
|
752
685
|
bytes[it.offset++] = length | 0xa0;
|
|
@@ -856,6 +789,7 @@ function number$1(bytes, value, it) {
|
|
|
856
789
|
|
|
857
790
|
var encode = /*#__PURE__*/Object.freeze({
|
|
858
791
|
__proto__: null,
|
|
792
|
+
utf8Length: utf8Length,
|
|
859
793
|
utf8Write: utf8Write,
|
|
860
794
|
int8: int8$1,
|
|
861
795
|
uint8: uint8$1,
|
|
@@ -874,16 +808,11 @@ var encode = /*#__PURE__*/Object.freeze({
|
|
|
874
808
|
number: number$1
|
|
875
809
|
});
|
|
876
810
|
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
function EncodeSchemaError() {
|
|
880
|
-
return _super !== null && _super.apply(this, arguments) || this;
|
|
881
|
-
}
|
|
882
|
-
return EncodeSchemaError;
|
|
883
|
-
}(Error));
|
|
811
|
+
class EncodeSchemaError extends Error {
|
|
812
|
+
}
|
|
884
813
|
function assertType(value, type, klass, field) {
|
|
885
|
-
|
|
886
|
-
|
|
814
|
+
let typeofTarget;
|
|
815
|
+
let allowNull = false;
|
|
887
816
|
switch (type) {
|
|
888
817
|
case "number":
|
|
889
818
|
case "int8":
|
|
@@ -898,7 +827,7 @@ function assertType(value, type, klass, field) {
|
|
|
898
827
|
case "float64":
|
|
899
828
|
typeofTarget = "number";
|
|
900
829
|
if (isNaN(value)) {
|
|
901
|
-
console.log(
|
|
830
|
+
console.log(`trying to encode "NaN" in ${klass.constructor.name}#${field}`);
|
|
902
831
|
}
|
|
903
832
|
break;
|
|
904
833
|
case "string":
|
|
@@ -910,25 +839,25 @@ function assertType(value, type, klass, field) {
|
|
|
910
839
|
return;
|
|
911
840
|
}
|
|
912
841
|
if (typeof (value) !== typeofTarget && (!allowNull || (allowNull && value !== null))) {
|
|
913
|
-
|
|
914
|
-
throw new EncodeSchemaError(
|
|
842
|
+
let foundValue = `'${JSON.stringify(value)}'${(value && value.constructor && ` (${value.constructor.name})`) || ''}`;
|
|
843
|
+
throw new EncodeSchemaError(`a '${typeofTarget}' was expected, but ${foundValue} was provided in ${klass.constructor.name}#${field}`);
|
|
915
844
|
}
|
|
916
845
|
}
|
|
917
846
|
function assertInstanceType(value, type, klass, field) {
|
|
918
847
|
if (!(value instanceof type)) {
|
|
919
|
-
throw new EncodeSchemaError(
|
|
848
|
+
throw new EncodeSchemaError(`a '${type.name}' was expected, but '${value && value.constructor.name}' was provided in ${klass.constructor.name}#${field}`);
|
|
920
849
|
}
|
|
921
850
|
}
|
|
922
851
|
|
|
923
852
|
function encodePrimitiveType(type, bytes, value, klass, field, it) {
|
|
924
853
|
assertType(value, type, klass, field);
|
|
925
|
-
|
|
854
|
+
const encodeFunc = encode[type];
|
|
926
855
|
if (encodeFunc) {
|
|
927
856
|
encodeFunc(bytes, value, it);
|
|
928
857
|
// encodeFunc(bytes, value);
|
|
929
858
|
}
|
|
930
859
|
else {
|
|
931
|
-
throw new EncodeSchemaError(
|
|
860
|
+
throw new EncodeSchemaError(`a '${type}' was expected, but ${value} was provided in ${klass.constructor.name}#${field}`);
|
|
932
861
|
}
|
|
933
862
|
}
|
|
934
863
|
function encodeValue(encoder, bytes, ref, type, value, field, operation, it) {
|
|
@@ -955,7 +884,7 @@ function encodeValue(encoder, bytes, ref, type, value, field, operation, it) {
|
|
|
955
884
|
//
|
|
956
885
|
// Custom type (MapSchema, ArraySchema, etc)
|
|
957
886
|
//
|
|
958
|
-
|
|
887
|
+
const definition = getType(Object.keys(type)[0]);
|
|
959
888
|
//
|
|
960
889
|
// ensure a ArraySchema has been provided
|
|
961
890
|
//
|
|
@@ -971,12 +900,12 @@ function encodeValue(encoder, bytes, ref, type, value, field, operation, it) {
|
|
|
971
900
|
* Used for Schema instances.
|
|
972
901
|
* @private
|
|
973
902
|
*/
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
903
|
+
const encodeSchemaOperation = function (encoder, bytes, changeTree, index, operation, it) {
|
|
904
|
+
const ref = changeTree.ref;
|
|
905
|
+
const metadata = ref['constructor'][Symbol.metadata];
|
|
906
|
+
const field = metadata[index];
|
|
907
|
+
const type = metadata[field].type;
|
|
908
|
+
const value = ref[field];
|
|
980
909
|
// "compress" field index + operation
|
|
981
910
|
bytes[it.offset++] = (index | operation) & 255;
|
|
982
911
|
// Do not encode value for DELETE operations
|
|
@@ -990,8 +919,8 @@ var encodeSchemaOperation = function (encoder, bytes, changeTree, index, operati
|
|
|
990
919
|
* Used for collections (MapSchema, CollectionSchema, SetSchema)
|
|
991
920
|
* @private
|
|
992
921
|
*/
|
|
993
|
-
|
|
994
|
-
|
|
922
|
+
const encodeKeyValueOperation = function (encoder, bytes, changeTree, field, operation, it) {
|
|
923
|
+
const ref = changeTree.ref;
|
|
995
924
|
// encode operation
|
|
996
925
|
bytes[it.offset++] = operation & 255;
|
|
997
926
|
// custom operations
|
|
@@ -1012,12 +941,12 @@ var encodeKeyValueOperation = function (encoder, bytes, changeTree, field, opera
|
|
|
1012
941
|
//
|
|
1013
942
|
// MapSchema dynamic key
|
|
1014
943
|
//
|
|
1015
|
-
|
|
944
|
+
const dynamicIndex = changeTree.ref['$indexes'].get(field);
|
|
1016
945
|
string$1(bytes, dynamicIndex, it);
|
|
1017
946
|
}
|
|
1018
947
|
}
|
|
1019
|
-
|
|
1020
|
-
|
|
948
|
+
const type = changeTree.getType(field);
|
|
949
|
+
const value = changeTree.getValue(field);
|
|
1021
950
|
// TODO: inline this function call small performance gain
|
|
1022
951
|
encodeValue(encoder, bytes, ref, type, value, field, operation, it);
|
|
1023
952
|
};
|
|
@@ -1025,15 +954,15 @@ var encodeKeyValueOperation = function (encoder, bytes, changeTree, field, opera
|
|
|
1025
954
|
* Used for collections (MapSchema, ArraySchema, etc.)
|
|
1026
955
|
* @private
|
|
1027
956
|
*/
|
|
1028
|
-
|
|
1029
|
-
|
|
957
|
+
const encodeArray = function (encoder, bytes, changeTree, field, operation, it, isEncodeAll, hasView) {
|
|
958
|
+
const ref = changeTree.ref;
|
|
1030
959
|
if (hasView &&
|
|
1031
960
|
operation === exports.OPERATION.DELETE &&
|
|
1032
961
|
typeof (changeTree.getType(field)) !== "string") {
|
|
1033
962
|
// encode delete by refId (array of schemas)
|
|
1034
963
|
bytes[it.offset++] = exports.OPERATION.DELETE_BY_REFID;
|
|
1035
|
-
|
|
1036
|
-
|
|
964
|
+
const value = ref['tmpItems'][field];
|
|
965
|
+
const refId = value[$changes].refId;
|
|
1037
966
|
number$1(bytes, refId, it);
|
|
1038
967
|
return;
|
|
1039
968
|
}
|
|
@@ -1049,8 +978,8 @@ var encodeArray = function (encoder, bytes, changeTree, field, operation, it, is
|
|
|
1049
978
|
if (operation === exports.OPERATION.DELETE) {
|
|
1050
979
|
return;
|
|
1051
980
|
}
|
|
1052
|
-
|
|
1053
|
-
|
|
981
|
+
const type = changeTree.getType(field);
|
|
982
|
+
const value = changeTree.getValue(field, isEncodeAll);
|
|
1054
983
|
// console.log("encodeArray -> ", {
|
|
1055
984
|
// ref: changeTree.ref.constructor.name,
|
|
1056
985
|
// field,
|
|
@@ -1148,18 +1077,18 @@ function float64(bytes, it) {
|
|
|
1148
1077
|
return readFloat64(bytes, it);
|
|
1149
1078
|
}
|
|
1150
1079
|
function int64(bytes, it) {
|
|
1151
|
-
|
|
1152
|
-
|
|
1080
|
+
const low = uint32(bytes, it);
|
|
1081
|
+
const high = int32(bytes, it) * Math.pow(2, 32);
|
|
1153
1082
|
return high + low;
|
|
1154
1083
|
}
|
|
1155
1084
|
function uint64(bytes, it) {
|
|
1156
|
-
|
|
1157
|
-
|
|
1085
|
+
const low = uint32(bytes, it);
|
|
1086
|
+
const high = uint32(bytes, it) * Math.pow(2, 32);
|
|
1158
1087
|
return high + low;
|
|
1159
1088
|
}
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1089
|
+
const _int32 = new Int32Array(2);
|
|
1090
|
+
const _float32 = new Float32Array(_int32.buffer);
|
|
1091
|
+
const _float64 = new Float64Array(_int32.buffer);
|
|
1163
1092
|
function readFloat32(bytes, it) {
|
|
1164
1093
|
_int32[0] = int32(bytes, it);
|
|
1165
1094
|
return _float32[0];
|
|
@@ -1173,8 +1102,8 @@ function boolean(bytes, it) {
|
|
|
1173
1102
|
return uint8(bytes, it) > 0;
|
|
1174
1103
|
}
|
|
1175
1104
|
function string(bytes, it) {
|
|
1176
|
-
|
|
1177
|
-
|
|
1105
|
+
const prefix = bytes[it.offset++];
|
|
1106
|
+
let length;
|
|
1178
1107
|
if (prefix < 0xc0) {
|
|
1179
1108
|
// fixstr
|
|
1180
1109
|
length = prefix & 0x1f;
|
|
@@ -1188,12 +1117,12 @@ function string(bytes, it) {
|
|
|
1188
1117
|
else if (prefix === 0xdb) {
|
|
1189
1118
|
length = uint32(bytes, it);
|
|
1190
1119
|
}
|
|
1191
|
-
|
|
1120
|
+
const value = utf8Read(bytes, it.offset, length);
|
|
1192
1121
|
it.offset += length;
|
|
1193
1122
|
return value;
|
|
1194
1123
|
}
|
|
1195
1124
|
function stringCheck(bytes, it) {
|
|
1196
|
-
|
|
1125
|
+
const prefix = bytes[it.offset];
|
|
1197
1126
|
return (
|
|
1198
1127
|
// fixstr
|
|
1199
1128
|
(prefix < 0xc0 && prefix > 0xa0) ||
|
|
@@ -1205,7 +1134,7 @@ function stringCheck(bytes, it) {
|
|
|
1205
1134
|
prefix === 0xdb);
|
|
1206
1135
|
}
|
|
1207
1136
|
function number(bytes, it) {
|
|
1208
|
-
|
|
1137
|
+
const prefix = bytes[it.offset++];
|
|
1209
1138
|
if (prefix < 0x80) {
|
|
1210
1139
|
// positive fixint
|
|
1211
1140
|
return prefix;
|
|
@@ -1256,7 +1185,7 @@ function number(bytes, it) {
|
|
|
1256
1185
|
}
|
|
1257
1186
|
}
|
|
1258
1187
|
function numberCheck(bytes, it) {
|
|
1259
|
-
|
|
1188
|
+
const prefix = bytes[it.offset];
|
|
1260
1189
|
// positive fixint - 0x00 - 0x7f
|
|
1261
1190
|
// float 32 - 0xca
|
|
1262
1191
|
// float 64 - 0xcb
|
|
@@ -1315,14 +1244,14 @@ var decode = /*#__PURE__*/Object.freeze({
|
|
|
1315
1244
|
switchStructureCheck: switchStructureCheck
|
|
1316
1245
|
});
|
|
1317
1246
|
|
|
1318
|
-
|
|
1247
|
+
const DEFINITION_MISMATCH = -1;
|
|
1319
1248
|
function decodeValue(decoder, operation, ref, index, type, bytes, it, allChanges) {
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1249
|
+
const $root = decoder.$root;
|
|
1250
|
+
const previousValue = ref[$getByIndex](index);
|
|
1251
|
+
let value;
|
|
1323
1252
|
if ((operation & exports.OPERATION.DELETE) === exports.OPERATION.DELETE) {
|
|
1324
1253
|
// Flag `refId` for garbage collection.
|
|
1325
|
-
|
|
1254
|
+
const previousRefId = $root.refIds.get(previousValue);
|
|
1326
1255
|
if (previousRefId !== undefined) {
|
|
1327
1256
|
$root.removeRef(previousRefId);
|
|
1328
1257
|
}
|
|
@@ -1349,10 +1278,10 @@ function decodeValue(decoder, operation, ref, index, type, bytes, it, allChanges
|
|
|
1349
1278
|
}
|
|
1350
1279
|
if (operation === exports.OPERATION.DELETE) ;
|
|
1351
1280
|
else if (Schema.is(type)) {
|
|
1352
|
-
|
|
1281
|
+
const refId = number(bytes, it);
|
|
1353
1282
|
value = $root.refs.get(refId);
|
|
1354
1283
|
if (previousValue) {
|
|
1355
|
-
|
|
1284
|
+
const previousRefId = $root.refIds.get(previousValue);
|
|
1356
1285
|
if (previousRefId &&
|
|
1357
1286
|
refId !== previousRefId &&
|
|
1358
1287
|
// FIXME: we may need to check for REPLACE operation as well
|
|
@@ -1361,7 +1290,7 @@ function decodeValue(decoder, operation, ref, index, type, bytes, it, allChanges
|
|
|
1361
1290
|
}
|
|
1362
1291
|
}
|
|
1363
1292
|
if ((operation & exports.OPERATION.ADD) === exports.OPERATION.ADD) {
|
|
1364
|
-
|
|
1293
|
+
const childType = decoder.getInstanceType(bytes, it, type);
|
|
1365
1294
|
if (!value) {
|
|
1366
1295
|
value = decoder.createInstanceOfType(childType);
|
|
1367
1296
|
}
|
|
@@ -1375,28 +1304,28 @@ function decodeValue(decoder, operation, ref, index, type, bytes, it, allChanges
|
|
|
1375
1304
|
value = decode[type](bytes, it);
|
|
1376
1305
|
}
|
|
1377
1306
|
else {
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1307
|
+
const typeDef = getType(Object.keys(type)[0]);
|
|
1308
|
+
const refId = number(bytes, it);
|
|
1309
|
+
const valueRef = ($root.refs.has(refId))
|
|
1381
1310
|
? previousValue || $root.refs.get(refId)
|
|
1382
1311
|
: new typeDef.constructor();
|
|
1383
1312
|
value = valueRef.clone(true);
|
|
1384
1313
|
value[$childType] = Object.values(type)[0]; // cache childType for ArraySchema and MapSchema
|
|
1385
1314
|
if (previousValue) {
|
|
1386
|
-
|
|
1315
|
+
let previousRefId = $root.refIds.get(previousValue);
|
|
1387
1316
|
if (previousRefId !== undefined && refId !== previousRefId) {
|
|
1388
1317
|
$root.removeRef(previousRefId);
|
|
1389
1318
|
//
|
|
1390
1319
|
// enqueue onRemove if structure has been replaced.
|
|
1391
1320
|
//
|
|
1392
|
-
|
|
1393
|
-
|
|
1321
|
+
const entries = previousValue.entries();
|
|
1322
|
+
let iter;
|
|
1394
1323
|
while ((iter = entries.next()) && !iter.done) {
|
|
1395
|
-
|
|
1324
|
+
const [key, value] = iter.value;
|
|
1396
1325
|
// if value is a schema, remove its reference
|
|
1397
1326
|
// FIXME: not sure if this is necessary, add more tests to confirm
|
|
1398
|
-
if (typeof (
|
|
1399
|
-
previousRefId = $root.refIds.get(
|
|
1327
|
+
if (typeof (value) === "object") {
|
|
1328
|
+
previousRefId = $root.refIds.get(value);
|
|
1400
1329
|
$root.removeRef(previousRefId);
|
|
1401
1330
|
}
|
|
1402
1331
|
allChanges.push({
|
|
@@ -1405,45 +1334,45 @@ function decodeValue(decoder, operation, ref, index, type, bytes, it, allChanges
|
|
|
1405
1334
|
op: exports.OPERATION.DELETE,
|
|
1406
1335
|
field: key,
|
|
1407
1336
|
value: undefined,
|
|
1408
|
-
previousValue:
|
|
1337
|
+
previousValue: value,
|
|
1409
1338
|
});
|
|
1410
1339
|
}
|
|
1411
1340
|
}
|
|
1412
1341
|
}
|
|
1413
1342
|
$root.addRef(refId, value, (valueRef !== previousValue));
|
|
1414
1343
|
}
|
|
1415
|
-
return { value
|
|
1344
|
+
return { value, previousValue };
|
|
1416
1345
|
}
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1346
|
+
const decodeSchemaOperation = function (decoder, bytes, it, ref, allChanges) {
|
|
1347
|
+
const first_byte = bytes[it.offset++];
|
|
1348
|
+
const metadata = ref['constructor'][Symbol.metadata];
|
|
1420
1349
|
// "compressed" index + operation
|
|
1421
|
-
|
|
1422
|
-
|
|
1350
|
+
const operation = (first_byte >> 6) << 6;
|
|
1351
|
+
const index = first_byte % (operation || 255);
|
|
1423
1352
|
// skip early if field is not defined
|
|
1424
|
-
|
|
1353
|
+
const field = metadata[index];
|
|
1425
1354
|
if (field === undefined) {
|
|
1426
1355
|
return DEFINITION_MISMATCH;
|
|
1427
1356
|
}
|
|
1428
|
-
|
|
1357
|
+
const { value, previousValue } = decodeValue(decoder, operation, ref, index, metadata[field].type, bytes, it, allChanges);
|
|
1429
1358
|
if (value !== null && value !== undefined) {
|
|
1430
1359
|
ref[field] = value;
|
|
1431
1360
|
}
|
|
1432
1361
|
// add change
|
|
1433
1362
|
if (previousValue !== value) {
|
|
1434
1363
|
allChanges.push({
|
|
1435
|
-
ref
|
|
1364
|
+
ref,
|
|
1436
1365
|
refId: decoder.currentRefId,
|
|
1437
1366
|
op: operation,
|
|
1438
1367
|
field: field,
|
|
1439
|
-
value
|
|
1440
|
-
previousValue
|
|
1368
|
+
value,
|
|
1369
|
+
previousValue,
|
|
1441
1370
|
});
|
|
1442
1371
|
}
|
|
1443
1372
|
};
|
|
1444
|
-
|
|
1373
|
+
const decodeKeyValueOperation = function (decoder, bytes, it, ref, allChanges) {
|
|
1445
1374
|
// "uncompressed" index + operation (array/map items)
|
|
1446
|
-
|
|
1375
|
+
const operation = bytes[it.offset++];
|
|
1447
1376
|
if (operation === exports.OPERATION.CLEAR) {
|
|
1448
1377
|
//
|
|
1449
1378
|
// When decoding:
|
|
@@ -1454,9 +1383,9 @@ var decodeKeyValueOperation = function (decoder, bytes, it, ref, allChanges) {
|
|
|
1454
1383
|
ref.clear();
|
|
1455
1384
|
return;
|
|
1456
1385
|
}
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1386
|
+
const index = number(bytes, it);
|
|
1387
|
+
const type = ref[$childType];
|
|
1388
|
+
let dynamicIndex;
|
|
1460
1389
|
if ((operation & exports.OPERATION.ADD) === exports.OPERATION.ADD) { // ADD or DELETE_AND_ADD
|
|
1461
1390
|
if (typeof (ref['set']) === "function") {
|
|
1462
1391
|
dynamicIndex = string(bytes, it); // MapSchema
|
|
@@ -1470,7 +1399,7 @@ var decodeKeyValueOperation = function (decoder, bytes, it, ref, allChanges) {
|
|
|
1470
1399
|
// get dynamic index from "ref"
|
|
1471
1400
|
dynamicIndex = ref['getIndex'](index);
|
|
1472
1401
|
}
|
|
1473
|
-
|
|
1402
|
+
const { value, previousValue } = decodeValue(decoder, operation, ref, index, type, bytes, it, allChanges);
|
|
1474
1403
|
if (value !== null && value !== undefined) {
|
|
1475
1404
|
if (typeof (ref['set']) === "function") {
|
|
1476
1405
|
// MapSchema
|
|
@@ -1482,28 +1411,28 @@ var decodeKeyValueOperation = function (decoder, bytes, it, ref, allChanges) {
|
|
|
1482
1411
|
}
|
|
1483
1412
|
else if (typeof (ref['add']) === "function") {
|
|
1484
1413
|
// CollectionSchema && SetSchema
|
|
1485
|
-
|
|
1486
|
-
if (typeof (
|
|
1487
|
-
ref['setIndex'](
|
|
1414
|
+
const index = ref.add(value);
|
|
1415
|
+
if (typeof (index) === "number") {
|
|
1416
|
+
ref['setIndex'](index, index);
|
|
1488
1417
|
}
|
|
1489
1418
|
}
|
|
1490
1419
|
}
|
|
1491
1420
|
// add change
|
|
1492
1421
|
if (previousValue !== value) {
|
|
1493
1422
|
allChanges.push({
|
|
1494
|
-
ref
|
|
1423
|
+
ref,
|
|
1495
1424
|
refId: decoder.currentRefId,
|
|
1496
1425
|
op: operation,
|
|
1497
1426
|
field: "", // FIXME: remove this
|
|
1498
|
-
dynamicIndex
|
|
1499
|
-
value
|
|
1500
|
-
previousValue
|
|
1427
|
+
dynamicIndex,
|
|
1428
|
+
value,
|
|
1429
|
+
previousValue,
|
|
1501
1430
|
});
|
|
1502
1431
|
}
|
|
1503
1432
|
};
|
|
1504
|
-
|
|
1433
|
+
const decodeArray = function (decoder, bytes, it, ref, allChanges) {
|
|
1505
1434
|
// "uncompressed" index + operation (array/map items)
|
|
1506
|
-
|
|
1435
|
+
const operation = bytes[it.offset++];
|
|
1507
1436
|
if (operation === exports.OPERATION.CLEAR) {
|
|
1508
1437
|
//
|
|
1509
1438
|
// When decoding:
|
|
@@ -1516,25 +1445,25 @@ var decodeArray = function (decoder, bytes, it, ref, allChanges) {
|
|
|
1516
1445
|
}
|
|
1517
1446
|
else if (operation === exports.OPERATION.DELETE_BY_REFID) {
|
|
1518
1447
|
// TODO: refactor here, try to follow same flow as below
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
ref[$deleteByIndex](
|
|
1448
|
+
const refId = number(bytes, it);
|
|
1449
|
+
const previousValue = decoder.$root.refs.get(refId);
|
|
1450
|
+
const index = ref.findIndex((value) => value === previousValue);
|
|
1451
|
+
ref[$deleteByIndex](index);
|
|
1523
1452
|
allChanges.push({
|
|
1524
|
-
ref
|
|
1453
|
+
ref,
|
|
1525
1454
|
refId: decoder.currentRefId,
|
|
1526
1455
|
op: exports.OPERATION.DELETE,
|
|
1527
1456
|
field: "", // FIXME: remove this
|
|
1528
|
-
dynamicIndex:
|
|
1457
|
+
dynamicIndex: index,
|
|
1529
1458
|
value: undefined,
|
|
1530
|
-
previousValue
|
|
1459
|
+
previousValue,
|
|
1531
1460
|
});
|
|
1532
1461
|
return;
|
|
1533
1462
|
}
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
|
|
1463
|
+
const index = number(bytes, it);
|
|
1464
|
+
const type = ref[$childType];
|
|
1465
|
+
let dynamicIndex = index;
|
|
1466
|
+
const { value, previousValue } = decodeValue(decoder, operation, ref, index, type, bytes, it, allChanges);
|
|
1538
1467
|
if (value !== null && value !== undefined &&
|
|
1539
1468
|
value !== previousValue // avoid setting same value twice (if index === 0 it will result in a "unshift" for ArraySchema)
|
|
1540
1469
|
) {
|
|
@@ -1544,20 +1473,21 @@ var decodeArray = function (decoder, bytes, it, ref, allChanges) {
|
|
|
1544
1473
|
// add change
|
|
1545
1474
|
if (previousValue !== value) {
|
|
1546
1475
|
allChanges.push({
|
|
1547
|
-
ref
|
|
1476
|
+
ref,
|
|
1548
1477
|
refId: decoder.currentRefId,
|
|
1549
1478
|
op: operation,
|
|
1550
1479
|
field: "", // FIXME: remove this
|
|
1551
|
-
dynamicIndex
|
|
1552
|
-
value
|
|
1553
|
-
previousValue
|
|
1480
|
+
dynamicIndex,
|
|
1481
|
+
value,
|
|
1482
|
+
previousValue,
|
|
1554
1483
|
});
|
|
1555
1484
|
}
|
|
1556
1485
|
};
|
|
1557
1486
|
|
|
1558
|
-
var
|
|
1559
|
-
|
|
1560
|
-
|
|
1487
|
+
var _a$4, _b$4;
|
|
1488
|
+
const DEFAULT_SORT = (a, b) => {
|
|
1489
|
+
const A = a.toString();
|
|
1490
|
+
const B = b.toString();
|
|
1561
1491
|
if (A < B)
|
|
1562
1492
|
return -1;
|
|
1563
1493
|
else if (A > B)
|
|
@@ -1565,13 +1495,33 @@ var DEFAULT_SORT = function (a, b) {
|
|
|
1565
1495
|
else
|
|
1566
1496
|
return 0;
|
|
1567
1497
|
};
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1498
|
+
class ArraySchema {
|
|
1499
|
+
static { this[_a$4] = encodeArray; }
|
|
1500
|
+
static { this[_b$4] = decodeArray; }
|
|
1501
|
+
/**
|
|
1502
|
+
* Determine if a property must be filtered.
|
|
1503
|
+
* - If returns false, the property is NOT going to be encoded.
|
|
1504
|
+
* - If returns true, the property is going to be encoded.
|
|
1505
|
+
*
|
|
1506
|
+
* Encoding with "filters" happens in two steps:
|
|
1507
|
+
* - First, the encoder iterates over all "not owned" properties and encodes them.
|
|
1508
|
+
* - Then, the encoder iterates over all "owned" properties per instance and encodes them.
|
|
1509
|
+
*/
|
|
1510
|
+
static [(_a$4 = $encoder, _b$4 = $decoder, $filter)](ref, index, view) {
|
|
1511
|
+
// console.log("ArraSchema[$filter] VIEW??", !view)
|
|
1512
|
+
return (!view ||
|
|
1513
|
+
typeof (ref[$childType]) === "string" ||
|
|
1514
|
+
// view.items.has(ref[$getByIndex](index)[$changes])
|
|
1515
|
+
view.items.has(ref['tmpItems'][index]?.[$changes]));
|
|
1516
|
+
}
|
|
1517
|
+
static is(type) {
|
|
1518
|
+
return (
|
|
1519
|
+
// type format: ["string"]
|
|
1520
|
+
Array.isArray(type) ||
|
|
1521
|
+
// type format: { array: "string" }
|
|
1522
|
+
(type['array'] !== undefined));
|
|
1523
|
+
}
|
|
1524
|
+
constructor(...items) {
|
|
1575
1525
|
this.items = [];
|
|
1576
1526
|
this.tmpItems = [];
|
|
1577
1527
|
this.deletedIndexes = {};
|
|
@@ -1581,18 +1531,18 @@ var ArraySchema = /** @class */ (function () {
|
|
|
1581
1531
|
writable: true,
|
|
1582
1532
|
configurable: true,
|
|
1583
1533
|
});
|
|
1584
|
-
|
|
1585
|
-
get:
|
|
1534
|
+
const proxy = new Proxy(this, {
|
|
1535
|
+
get: (obj, prop) => {
|
|
1586
1536
|
if (typeof (prop) !== "symbol" &&
|
|
1587
1537
|
!isNaN(prop) // https://stackoverflow.com/a/175787/892698
|
|
1588
1538
|
) {
|
|
1589
|
-
return
|
|
1539
|
+
return this.items[prop];
|
|
1590
1540
|
}
|
|
1591
1541
|
else {
|
|
1592
1542
|
return Reflect.get(obj, prop);
|
|
1593
1543
|
}
|
|
1594
1544
|
},
|
|
1595
|
-
set:
|
|
1545
|
+
set: (obj, key, setValue) => {
|
|
1596
1546
|
if (typeof (key) !== "symbol" && !isNaN(key)) {
|
|
1597
1547
|
if (setValue === undefined || setValue === null) {
|
|
1598
1548
|
obj.$deleteAt(key);
|
|
@@ -1601,26 +1551,26 @@ var ArraySchema = /** @class */ (function () {
|
|
|
1601
1551
|
if (setValue[$changes]) {
|
|
1602
1552
|
if (obj.items[key] !== undefined) {
|
|
1603
1553
|
if (setValue[$changes][$isNew]) {
|
|
1604
|
-
|
|
1554
|
+
this[$changes].indexedOperation(Number(key), exports.OPERATION.MOVE_AND_ADD);
|
|
1605
1555
|
}
|
|
1606
1556
|
else {
|
|
1607
1557
|
if ((obj[$changes].getChange(Number(key)) & exports.OPERATION.DELETE) === exports.OPERATION.DELETE) {
|
|
1608
|
-
|
|
1558
|
+
this[$changes].indexedOperation(Number(key), exports.OPERATION.DELETE_AND_MOVE);
|
|
1609
1559
|
}
|
|
1610
1560
|
else {
|
|
1611
|
-
|
|
1561
|
+
this[$changes].indexedOperation(Number(key), exports.OPERATION.MOVE);
|
|
1612
1562
|
}
|
|
1613
1563
|
}
|
|
1614
1564
|
}
|
|
1615
1565
|
else if (setValue[$changes][$isNew]) {
|
|
1616
|
-
|
|
1566
|
+
this[$changes].indexedOperation(Number(key), exports.OPERATION.ADD);
|
|
1617
1567
|
}
|
|
1618
1568
|
}
|
|
1619
1569
|
else {
|
|
1620
1570
|
obj.$changeAt(Number(key), setValue);
|
|
1621
1571
|
}
|
|
1622
|
-
|
|
1623
|
-
|
|
1572
|
+
this.items[key] = setValue;
|
|
1573
|
+
this.tmpItems[key] = setValue;
|
|
1624
1574
|
}
|
|
1625
1575
|
return true;
|
|
1626
1576
|
}
|
|
@@ -1628,7 +1578,7 @@ var ArraySchema = /** @class */ (function () {
|
|
|
1628
1578
|
return Reflect.set(obj, key, setValue);
|
|
1629
1579
|
}
|
|
1630
1580
|
},
|
|
1631
|
-
deleteProperty:
|
|
1581
|
+
deleteProperty: (obj, prop) => {
|
|
1632
1582
|
if (typeof (prop) === "number") {
|
|
1633
1583
|
obj.$deleteAt(prop);
|
|
1634
1584
|
}
|
|
@@ -1637,9 +1587,9 @@ var ArraySchema = /** @class */ (function () {
|
|
|
1637
1587
|
}
|
|
1638
1588
|
return true;
|
|
1639
1589
|
},
|
|
1640
|
-
has:
|
|
1590
|
+
has: (obj, key) => {
|
|
1641
1591
|
if (typeof (key) !== "symbol" && !isNaN(Number(key))) {
|
|
1642
|
-
return Reflect.has(
|
|
1592
|
+
return Reflect.has(this.items, key);
|
|
1643
1593
|
}
|
|
1644
1594
|
return Reflect.has(obj, key);
|
|
1645
1595
|
}
|
|
@@ -1648,82 +1598,48 @@ var ArraySchema = /** @class */ (function () {
|
|
|
1648
1598
|
this.push.apply(this, items);
|
|
1649
1599
|
return proxy;
|
|
1650
1600
|
}
|
|
1651
|
-
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
return
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
ArraySchema.is = function (type) {
|
|
1669
|
-
return (
|
|
1670
|
-
// type format: ["string"]
|
|
1671
|
-
Array.isArray(type) ||
|
|
1672
|
-
// type format: { array: "string" }
|
|
1673
|
-
(type['array'] !== undefined));
|
|
1674
|
-
};
|
|
1675
|
-
Object.defineProperty(ArraySchema.prototype, "length", {
|
|
1676
|
-
get: function () {
|
|
1677
|
-
return this.items.length;
|
|
1678
|
-
},
|
|
1679
|
-
set: function (newLength) {
|
|
1680
|
-
if (newLength === 0) {
|
|
1681
|
-
this.clear();
|
|
1682
|
-
}
|
|
1683
|
-
else if (newLength < this.items.length) {
|
|
1684
|
-
this.splice(newLength, this.length - newLength);
|
|
1685
|
-
}
|
|
1686
|
-
else {
|
|
1687
|
-
console.warn("ArraySchema: can't set .length to a higher value than its length.");
|
|
1688
|
-
}
|
|
1689
|
-
},
|
|
1690
|
-
enumerable: false,
|
|
1691
|
-
configurable: true
|
|
1692
|
-
});
|
|
1693
|
-
ArraySchema.prototype.push = function () {
|
|
1694
|
-
var _this = this;
|
|
1695
|
-
var values = [];
|
|
1696
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
1697
|
-
values[_i] = arguments[_i];
|
|
1698
|
-
}
|
|
1699
|
-
var length = this.tmpItems.length;
|
|
1700
|
-
values.forEach(function (value, i) {
|
|
1701
|
-
var _c;
|
|
1601
|
+
set length(newLength) {
|
|
1602
|
+
if (newLength === 0) {
|
|
1603
|
+
this.clear();
|
|
1604
|
+
}
|
|
1605
|
+
else if (newLength < this.items.length) {
|
|
1606
|
+
this.splice(newLength, this.length - newLength);
|
|
1607
|
+
}
|
|
1608
|
+
else {
|
|
1609
|
+
console.warn("ArraySchema: can't set .length to a higher value than its length.");
|
|
1610
|
+
}
|
|
1611
|
+
}
|
|
1612
|
+
get length() {
|
|
1613
|
+
return this.items.length;
|
|
1614
|
+
}
|
|
1615
|
+
push(...values) {
|
|
1616
|
+
let length = this.tmpItems.length;
|
|
1617
|
+
values.forEach((value, i) => {
|
|
1702
1618
|
// skip null values
|
|
1703
1619
|
if (value === undefined || value === null) {
|
|
1704
1620
|
return;
|
|
1705
1621
|
}
|
|
1706
|
-
|
|
1707
|
-
changeTree.indexedOperation(length, exports.OPERATION.ADD,
|
|
1622
|
+
const changeTree = this[$changes];
|
|
1623
|
+
changeTree.indexedOperation(length, exports.OPERATION.ADD, this.items.length);
|
|
1708
1624
|
// changeTree.indexes[length] = length;
|
|
1709
|
-
|
|
1710
|
-
|
|
1625
|
+
this.items.push(value);
|
|
1626
|
+
this.tmpItems.push(value);
|
|
1711
1627
|
//
|
|
1712
1628
|
// set value's parent after the value is set
|
|
1713
1629
|
// (to avoid encoding "refId" operations before parent's "ADD" operation)
|
|
1714
1630
|
//
|
|
1715
|
-
|
|
1631
|
+
value[$changes]?.setParent(this, changeTree.root, length);
|
|
1716
1632
|
length++;
|
|
1717
1633
|
});
|
|
1718
1634
|
return length;
|
|
1719
|
-
}
|
|
1635
|
+
}
|
|
1720
1636
|
/**
|
|
1721
1637
|
* Removes the last element from an array and returns it.
|
|
1722
1638
|
*/
|
|
1723
|
-
|
|
1724
|
-
|
|
1639
|
+
pop() {
|
|
1640
|
+
let index = -1;
|
|
1725
1641
|
// find last non-undefined index
|
|
1726
|
-
for (
|
|
1642
|
+
for (let i = this.tmpItems.length - 1; i >= 0; i--) {
|
|
1727
1643
|
// if (this.tmpItems[i] !== undefined) {
|
|
1728
1644
|
if (this.deletedIndexes[i] !== true) {
|
|
1729
1645
|
index = i;
|
|
@@ -1737,16 +1653,15 @@ var ArraySchema = /** @class */ (function () {
|
|
|
1737
1653
|
// this.tmpItems[index] = undefined;
|
|
1738
1654
|
this.deletedIndexes[index] = true;
|
|
1739
1655
|
return this.items.pop();
|
|
1740
|
-
}
|
|
1741
|
-
|
|
1656
|
+
}
|
|
1657
|
+
at(index) {
|
|
1742
1658
|
// Allow negative indexing from the end
|
|
1743
1659
|
if (index < 0)
|
|
1744
1660
|
index += this.length;
|
|
1745
1661
|
return this.items[index];
|
|
1746
|
-
}
|
|
1662
|
+
}
|
|
1747
1663
|
// encoding only
|
|
1748
|
-
|
|
1749
|
-
var _c, _d, _e, _f;
|
|
1664
|
+
$changeAt(index, value) {
|
|
1750
1665
|
if (value === undefined || value === null) {
|
|
1751
1666
|
console.error("ArraySchema items cannot be null nor undefined; Use `deleteAt(index)` instead.");
|
|
1752
1667
|
return;
|
|
@@ -1755,21 +1670,21 @@ var ArraySchema = /** @class */ (function () {
|
|
|
1755
1670
|
if (this.items[index] === value) {
|
|
1756
1671
|
return;
|
|
1757
1672
|
}
|
|
1758
|
-
|
|
1759
|
-
|
|
1673
|
+
const changeTree = this[$changes];
|
|
1674
|
+
const operation = changeTree.indexes?.[index]?.op ?? exports.OPERATION.ADD;
|
|
1760
1675
|
changeTree.change(index, operation);
|
|
1761
1676
|
//
|
|
1762
1677
|
// set value's parent after the value is set
|
|
1763
1678
|
// (to avoid encoding "refId" operations before parent's "ADD" operation)
|
|
1764
1679
|
//
|
|
1765
|
-
|
|
1766
|
-
}
|
|
1680
|
+
value[$changes]?.setParent(this, changeTree.root, index);
|
|
1681
|
+
}
|
|
1767
1682
|
// encoding only
|
|
1768
|
-
|
|
1683
|
+
$deleteAt(index, operation) {
|
|
1769
1684
|
this[$changes].delete(index, operation);
|
|
1770
|
-
}
|
|
1685
|
+
}
|
|
1771
1686
|
// decoding only
|
|
1772
|
-
|
|
1687
|
+
$setAt(index, value, operation) {
|
|
1773
1688
|
if (index === 0 &&
|
|
1774
1689
|
operation === exports.OPERATION.ADD &&
|
|
1775
1690
|
this.items[index] !== undefined) {
|
|
@@ -1783,17 +1698,16 @@ var ArraySchema = /** @class */ (function () {
|
|
|
1783
1698
|
else {
|
|
1784
1699
|
this.items[index] = value;
|
|
1785
1700
|
}
|
|
1786
|
-
}
|
|
1787
|
-
|
|
1701
|
+
}
|
|
1702
|
+
clear() {
|
|
1788
1703
|
// skip if already clear
|
|
1789
1704
|
if (this.items.length === 0) {
|
|
1790
1705
|
return;
|
|
1791
1706
|
}
|
|
1792
1707
|
// discard previous operations.
|
|
1793
|
-
|
|
1708
|
+
const changeTree = this[$changes];
|
|
1794
1709
|
// discard children
|
|
1795
|
-
changeTree.forEachChild(
|
|
1796
|
-
var _c, _d, _e;
|
|
1710
|
+
changeTree.forEachChild((changeTree, _) => {
|
|
1797
1711
|
changeTree.discard(true);
|
|
1798
1712
|
//
|
|
1799
1713
|
// TODO: add tests with instance sharing + .clear()
|
|
@@ -1801,70 +1715,64 @@ var ArraySchema = /** @class */ (function () {
|
|
|
1801
1715
|
//
|
|
1802
1716
|
// TODO: do not use [$changes] at decoding time.
|
|
1803
1717
|
//
|
|
1804
|
-
|
|
1805
|
-
|
|
1806
|
-
|
|
1718
|
+
changeTree.root?.changes.delete(changeTree);
|
|
1719
|
+
changeTree.root?.allChanges.delete(changeTree);
|
|
1720
|
+
changeTree.root?.allFilteredChanges.delete(changeTree);
|
|
1807
1721
|
});
|
|
1808
1722
|
changeTree.discard(true);
|
|
1809
1723
|
changeTree.operation(exports.OPERATION.CLEAR);
|
|
1810
1724
|
this.items.length = 0;
|
|
1811
1725
|
this.tmpItems.length = 0;
|
|
1812
|
-
}
|
|
1726
|
+
}
|
|
1813
1727
|
/**
|
|
1814
1728
|
* Combines two or more arrays.
|
|
1815
1729
|
* @param items Additional items to add to the end of array1.
|
|
1816
1730
|
*/
|
|
1817
1731
|
// @ts-ignore
|
|
1818
|
-
|
|
1819
|
-
|
|
1820
|
-
|
|
1821
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
1822
|
-
items[_i] = arguments[_i];
|
|
1823
|
-
}
|
|
1824
|
-
return new (ArraySchema.bind.apply(ArraySchema, __spreadArray([void 0], (_c = this.items).concat.apply(_c, items), false)))();
|
|
1825
|
-
};
|
|
1732
|
+
concat(...items) {
|
|
1733
|
+
return new ArraySchema(...this.items.concat(...items));
|
|
1734
|
+
}
|
|
1826
1735
|
/**
|
|
1827
1736
|
* Adds all the elements of an array separated by the specified separator string.
|
|
1828
1737
|
* @param separator A string used to separate one element of an array from the next in the resulting String. If omitted, the array elements are separated with a comma.
|
|
1829
1738
|
*/
|
|
1830
|
-
|
|
1739
|
+
join(separator) {
|
|
1831
1740
|
return this.items.join(separator);
|
|
1832
|
-
}
|
|
1741
|
+
}
|
|
1833
1742
|
/**
|
|
1834
1743
|
* Reverses the elements in an Array.
|
|
1835
1744
|
*/
|
|
1836
1745
|
// @ts-ignore
|
|
1837
|
-
|
|
1746
|
+
reverse() {
|
|
1838
1747
|
this[$changes].operation(exports.OPERATION.REVERSE);
|
|
1839
1748
|
this.items.reverse();
|
|
1840
1749
|
this.tmpItems.reverse();
|
|
1841
1750
|
return this;
|
|
1842
|
-
}
|
|
1751
|
+
}
|
|
1843
1752
|
/**
|
|
1844
1753
|
* Removes the first element from an array and returns it.
|
|
1845
1754
|
*/
|
|
1846
|
-
|
|
1847
|
-
var _this = this;
|
|
1755
|
+
shift() {
|
|
1848
1756
|
if (this.items.length === 0) {
|
|
1849
1757
|
return undefined;
|
|
1850
1758
|
}
|
|
1851
1759
|
// const index = Number(Object.keys(changeTree.indexes)[0]);
|
|
1852
|
-
|
|
1853
|
-
|
|
1760
|
+
const index = this.tmpItems.findIndex((item, i) => item === this.items[0]);
|
|
1761
|
+
const changeTree = this[$changes];
|
|
1854
1762
|
changeTree.delete(index);
|
|
1855
1763
|
changeTree.shiftAllChangeIndexes(-1, index);
|
|
1856
1764
|
return this.items.shift();
|
|
1857
|
-
}
|
|
1765
|
+
}
|
|
1858
1766
|
/**
|
|
1859
1767
|
* Returns a section of an array.
|
|
1860
1768
|
* @param start The beginning of the specified portion of the array.
|
|
1861
1769
|
* @param end The end of the specified portion of the array. This is exclusive of the element at the index 'end'.
|
|
1862
1770
|
*/
|
|
1863
|
-
|
|
1864
|
-
|
|
1865
|
-
sliced.push
|
|
1771
|
+
slice(start, end) {
|
|
1772
|
+
const sliced = new ArraySchema();
|
|
1773
|
+
sliced.push(...this.items.slice(start, end));
|
|
1866
1774
|
return sliced;
|
|
1867
|
-
}
|
|
1775
|
+
}
|
|
1868
1776
|
/**
|
|
1869
1777
|
* Sorts an array.
|
|
1870
1778
|
* @param compareFn Function used to determine the order of the elements. It is expected to return
|
|
@@ -1874,53 +1782,45 @@ var ArraySchema = /** @class */ (function () {
|
|
|
1874
1782
|
* [11,2,22,1].sort((a, b) => a - b)
|
|
1875
1783
|
* ```
|
|
1876
1784
|
*/
|
|
1877
|
-
|
|
1878
|
-
|
|
1879
|
-
|
|
1880
|
-
var sortedItems = this.items.sort(compareFn);
|
|
1785
|
+
sort(compareFn = DEFAULT_SORT) {
|
|
1786
|
+
const changeTree = this[$changes];
|
|
1787
|
+
const sortedItems = this.items.sort(compareFn);
|
|
1881
1788
|
// wouldn't OPERATION.MOVE make more sense here?
|
|
1882
|
-
sortedItems.forEach(
|
|
1789
|
+
sortedItems.forEach((_, i) => changeTree.change(i, exports.OPERATION.REPLACE));
|
|
1883
1790
|
this.tmpItems.sort(compareFn);
|
|
1884
1791
|
return this;
|
|
1885
|
-
}
|
|
1792
|
+
}
|
|
1886
1793
|
/**
|
|
1887
1794
|
* Removes elements from an array and, if necessary, inserts new elements in their place, returning the deleted elements.
|
|
1888
1795
|
* @param start The zero-based location in the array from which to start removing elements.
|
|
1889
1796
|
* @param deleteCount The number of elements to remove.
|
|
1890
1797
|
* @param insertItems Elements to insert into the array in place of the deleted elements.
|
|
1891
1798
|
*/
|
|
1892
|
-
|
|
1893
|
-
|
|
1894
|
-
|
|
1895
|
-
|
|
1896
|
-
var insertItems = [];
|
|
1897
|
-
for (var _i = 2; _i < arguments.length; _i++) {
|
|
1898
|
-
insertItems[_i - 2] = arguments[_i];
|
|
1899
|
-
}
|
|
1900
|
-
var changeTree = this[$changes];
|
|
1901
|
-
var tmpItemsLength = this.tmpItems.length;
|
|
1902
|
-
var insertCount = insertItems.length;
|
|
1799
|
+
splice(start, deleteCount = this.items.length - start, ...insertItems) {
|
|
1800
|
+
const changeTree = this[$changes];
|
|
1801
|
+
const tmpItemsLength = this.tmpItems.length;
|
|
1802
|
+
const insertCount = insertItems.length;
|
|
1903
1803
|
// build up-to-date list of indexes, excluding removed values.
|
|
1904
|
-
|
|
1905
|
-
for (
|
|
1804
|
+
const indexes = [];
|
|
1805
|
+
for (let i = 0; i < tmpItemsLength; i++) {
|
|
1906
1806
|
// if (this.tmpItems[i] !== undefined) {
|
|
1907
1807
|
if (this.deletedIndexes[i] !== true) {
|
|
1908
1808
|
indexes.push(i);
|
|
1909
1809
|
}
|
|
1910
1810
|
}
|
|
1911
1811
|
// delete operations at correct index
|
|
1912
|
-
for (
|
|
1913
|
-
|
|
1812
|
+
for (let i = start; i < start + deleteCount; i++) {
|
|
1813
|
+
const index = indexes[i];
|
|
1914
1814
|
changeTree.delete(index);
|
|
1915
1815
|
// this.tmpItems[index] = undefined;
|
|
1916
1816
|
this.deletedIndexes[index] = true;
|
|
1917
1817
|
}
|
|
1918
1818
|
// force insert operations
|
|
1919
|
-
for (
|
|
1920
|
-
|
|
1819
|
+
for (let i = 0; i < insertCount; i++) {
|
|
1820
|
+
const addIndex = indexes[start] + i;
|
|
1921
1821
|
changeTree.indexedOperation(addIndex, exports.OPERATION.ADD);
|
|
1922
1822
|
// set value's parent/root
|
|
1923
|
-
|
|
1823
|
+
insertItems[i][$changes]?.setParent(this, changeTree.root, addIndex);
|
|
1924
1824
|
}
|
|
1925
1825
|
//
|
|
1926
1826
|
// delete exceeding indexes from "allChanges"
|
|
@@ -1929,19 +1829,14 @@ var ArraySchema = /** @class */ (function () {
|
|
|
1929
1829
|
if (deleteCount > insertCount) {
|
|
1930
1830
|
changeTree.shiftAllChangeIndexes(-(deleteCount - insertCount), indexes[start + insertCount]);
|
|
1931
1831
|
}
|
|
1932
|
-
return
|
|
1933
|
-
}
|
|
1832
|
+
return this.items.splice(start, deleteCount, ...insertItems);
|
|
1833
|
+
}
|
|
1934
1834
|
/**
|
|
1935
1835
|
* Inserts new elements at the start of an array.
|
|
1936
1836
|
* @param items Elements to insert at the start of the Array.
|
|
1937
1837
|
*/
|
|
1938
|
-
|
|
1939
|
-
|
|
1940
|
-
var items = [];
|
|
1941
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
1942
|
-
items[_i] = arguments[_i];
|
|
1943
|
-
}
|
|
1944
|
-
var changeTree = this[$changes];
|
|
1838
|
+
unshift(...items) {
|
|
1839
|
+
const changeTree = this[$changes];
|
|
1945
1840
|
// shift indexes
|
|
1946
1841
|
changeTree.shiftChangeIndexes(items.length);
|
|
1947
1842
|
// new index
|
|
@@ -1952,29 +1847,28 @@ var ArraySchema = /** @class */ (function () {
|
|
|
1952
1847
|
changeTree.allChanges.set(this.items.length, exports.OPERATION.ADD);
|
|
1953
1848
|
}
|
|
1954
1849
|
// FIXME: should we use OPERATION.MOVE here instead?
|
|
1955
|
-
items.forEach(
|
|
1850
|
+
items.forEach((_, index) => {
|
|
1956
1851
|
changeTree.change(index, exports.OPERATION.ADD);
|
|
1957
1852
|
});
|
|
1958
|
-
|
|
1959
|
-
return
|
|
1960
|
-
}
|
|
1853
|
+
this.tmpItems.unshift(...items);
|
|
1854
|
+
return this.items.unshift(...items);
|
|
1855
|
+
}
|
|
1961
1856
|
/**
|
|
1962
1857
|
* Returns the index of the first occurrence of a value in an array.
|
|
1963
1858
|
* @param searchElement The value to locate in the array.
|
|
1964
1859
|
* @param fromIndex The array index at which to begin the search. If fromIndex is omitted, the search starts at index 0.
|
|
1965
1860
|
*/
|
|
1966
|
-
|
|
1861
|
+
indexOf(searchElement, fromIndex) {
|
|
1967
1862
|
return this.items.indexOf(searchElement, fromIndex);
|
|
1968
|
-
}
|
|
1863
|
+
}
|
|
1969
1864
|
/**
|
|
1970
1865
|
* Returns the index of the last occurrence of a specified value in an array.
|
|
1971
1866
|
* @param searchElement The value to locate in the array.
|
|
1972
1867
|
* @param fromIndex The array index at which to begin the search. If fromIndex is omitted, the search starts at the last index in the array.
|
|
1973
1868
|
*/
|
|
1974
|
-
|
|
1975
|
-
if (fromIndex === void 0) { fromIndex = this.length - 1; }
|
|
1869
|
+
lastIndexOf(searchElement, fromIndex = this.length - 1) {
|
|
1976
1870
|
return this.items.lastIndexOf(searchElement, fromIndex);
|
|
1977
|
-
}
|
|
1871
|
+
}
|
|
1978
1872
|
/**
|
|
1979
1873
|
* Determines whether all the members of an array satisfy the specified test.
|
|
1980
1874
|
* @param callbackfn A function that accepts up to three arguments. The every method calls
|
|
@@ -1983,9 +1877,9 @@ var ArraySchema = /** @class */ (function () {
|
|
|
1983
1877
|
* @param thisArg An object to which the this keyword can refer in the callbackfn function.
|
|
1984
1878
|
* If thisArg is omitted, undefined is used as the this value.
|
|
1985
1879
|
*/
|
|
1986
|
-
|
|
1880
|
+
every(callbackfn, thisArg) {
|
|
1987
1881
|
return this.items.every(callbackfn, thisArg);
|
|
1988
|
-
}
|
|
1882
|
+
}
|
|
1989
1883
|
/**
|
|
1990
1884
|
* Determines whether the specified callback function returns true for any element of an array.
|
|
1991
1885
|
* @param callbackfn A function that accepts up to three arguments. The some method calls
|
|
@@ -1994,44 +1888,44 @@ var ArraySchema = /** @class */ (function () {
|
|
|
1994
1888
|
* @param thisArg An object to which the this keyword can refer in the callbackfn function.
|
|
1995
1889
|
* If thisArg is omitted, undefined is used as the this value.
|
|
1996
1890
|
*/
|
|
1997
|
-
|
|
1891
|
+
some(callbackfn, thisArg) {
|
|
1998
1892
|
return this.items.some(callbackfn, thisArg);
|
|
1999
|
-
}
|
|
1893
|
+
}
|
|
2000
1894
|
/**
|
|
2001
1895
|
* Performs the specified action for each element in an array.
|
|
2002
1896
|
* @param callbackfn A function that accepts up to three arguments. forEach calls the callbackfn function one time for each element in the array.
|
|
2003
1897
|
* @param thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
|
|
2004
1898
|
*/
|
|
2005
|
-
|
|
1899
|
+
forEach(callbackfn, thisArg) {
|
|
2006
1900
|
return this.items.forEach(callbackfn, thisArg);
|
|
2007
|
-
}
|
|
1901
|
+
}
|
|
2008
1902
|
/**
|
|
2009
1903
|
* Calls a defined callback function on each element of an array, and returns an array that contains the results.
|
|
2010
1904
|
* @param callbackfn A function that accepts up to three arguments. The map method calls the callbackfn function one time for each element in the array.
|
|
2011
1905
|
* @param thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
|
|
2012
1906
|
*/
|
|
2013
|
-
|
|
1907
|
+
map(callbackfn, thisArg) {
|
|
2014
1908
|
return this.items.map(callbackfn, thisArg);
|
|
2015
|
-
}
|
|
2016
|
-
|
|
1909
|
+
}
|
|
1910
|
+
filter(callbackfn, thisArg) {
|
|
2017
1911
|
return this.items.filter(callbackfn, thisArg);
|
|
2018
|
-
}
|
|
1912
|
+
}
|
|
2019
1913
|
/**
|
|
2020
1914
|
* Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.
|
|
2021
1915
|
* @param callbackfn A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array.
|
|
2022
1916
|
* @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value.
|
|
2023
1917
|
*/
|
|
2024
|
-
|
|
1918
|
+
reduce(callbackfn, initialValue) {
|
|
2025
1919
|
return this.items.reduce(callbackfn, initialValue);
|
|
2026
|
-
}
|
|
1920
|
+
}
|
|
2027
1921
|
/**
|
|
2028
1922
|
* Calls the specified callback function for all the elements in an array, in descending order. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.
|
|
2029
1923
|
* @param callbackfn A function that accepts up to four arguments. The reduceRight method calls the callbackfn function one time for each element in the array.
|
|
2030
1924
|
* @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value.
|
|
2031
1925
|
*/
|
|
2032
|
-
|
|
1926
|
+
reduceRight(callbackfn, initialValue) {
|
|
2033
1927
|
return this.items.reduceRight(callbackfn, initialValue);
|
|
2034
|
-
}
|
|
1928
|
+
}
|
|
2035
1929
|
/**
|
|
2036
1930
|
* Returns the value of the first element in the array where predicate is true, and undefined
|
|
2037
1931
|
* otherwise.
|
|
@@ -2041,9 +1935,9 @@ var ArraySchema = /** @class */ (function () {
|
|
|
2041
1935
|
* @param thisArg If provided, it will be used as the this value for each invocation of
|
|
2042
1936
|
* predicate. If it is not provided, undefined is used instead.
|
|
2043
1937
|
*/
|
|
2044
|
-
|
|
1938
|
+
find(predicate, thisArg) {
|
|
2045
1939
|
return this.items.find(predicate, thisArg);
|
|
2046
|
-
}
|
|
1940
|
+
}
|
|
2047
1941
|
/**
|
|
2048
1942
|
* Returns the index of the first element in the array where predicate is true, and -1
|
|
2049
1943
|
* otherwise.
|
|
@@ -2053,9 +1947,9 @@ var ArraySchema = /** @class */ (function () {
|
|
|
2053
1947
|
* @param thisArg If provided, it will be used as the this value for each invocation of
|
|
2054
1948
|
* predicate. If it is not provided, undefined is used instead.
|
|
2055
1949
|
*/
|
|
2056
|
-
|
|
1950
|
+
findIndex(predicate, thisArg) {
|
|
2057
1951
|
return this.items.findIndex(predicate, thisArg);
|
|
2058
|
-
}
|
|
1952
|
+
}
|
|
2059
1953
|
/**
|
|
2060
1954
|
* Returns the this object after filling the section identified by start and end with value
|
|
2061
1955
|
* @param value value to fill array section with
|
|
@@ -2064,12 +1958,12 @@ var ArraySchema = /** @class */ (function () {
|
|
|
2064
1958
|
* @param end index to stop filling the array at. If end is negative, it is treated as
|
|
2065
1959
|
* length+end.
|
|
2066
1960
|
*/
|
|
2067
|
-
|
|
1961
|
+
fill(value, start, end) {
|
|
2068
1962
|
//
|
|
2069
1963
|
// TODO
|
|
2070
1964
|
//
|
|
2071
1965
|
throw new Error("ArraySchema#fill() not implemented");
|
|
2072
|
-
}
|
|
1966
|
+
}
|
|
2073
1967
|
/**
|
|
2074
1968
|
* Returns the this object after copying a section of the array identified by start and end
|
|
2075
1969
|
* to the same array starting at position target
|
|
@@ -2079,55 +1973,52 @@ var ArraySchema = /** @class */ (function () {
|
|
|
2079
1973
|
* is treated as length+end.
|
|
2080
1974
|
* @param end If not specified, length of the this object is used as its default value.
|
|
2081
1975
|
*/
|
|
2082
|
-
|
|
1976
|
+
copyWithin(target, start, end) {
|
|
2083
1977
|
//
|
|
2084
1978
|
// TODO
|
|
2085
1979
|
//
|
|
2086
1980
|
throw new Error("ArraySchema#copyWithin() not implemented");
|
|
2087
|
-
}
|
|
1981
|
+
}
|
|
2088
1982
|
/**
|
|
2089
1983
|
* Returns a string representation of an array.
|
|
2090
1984
|
*/
|
|
2091
|
-
|
|
1985
|
+
toString() {
|
|
2092
1986
|
return this.items.toString();
|
|
2093
|
-
}
|
|
1987
|
+
}
|
|
2094
1988
|
/**
|
|
2095
1989
|
* Returns a string representation of an array. The elements are converted to string using their toLocalString methods.
|
|
2096
1990
|
*/
|
|
2097
|
-
|
|
1991
|
+
toLocaleString() {
|
|
2098
1992
|
return this.items.toLocaleString();
|
|
2099
|
-
}
|
|
1993
|
+
}
|
|
1994
|
+
;
|
|
2100
1995
|
/** Iterator */
|
|
2101
|
-
|
|
1996
|
+
[Symbol.iterator]() {
|
|
2102
1997
|
return this.items[Symbol.iterator]();
|
|
2103
|
-
}
|
|
2104
|
-
|
|
2105
|
-
|
|
2106
|
-
|
|
2107
|
-
},
|
|
2108
|
-
enumerable: false,
|
|
2109
|
-
configurable: true
|
|
2110
|
-
});
|
|
1998
|
+
}
|
|
1999
|
+
static get [Symbol.species]() {
|
|
2000
|
+
return ArraySchema;
|
|
2001
|
+
}
|
|
2111
2002
|
/**
|
|
2112
2003
|
* Returns an iterable of key, value pairs for every entry in the array
|
|
2113
2004
|
*/
|
|
2114
|
-
|
|
2005
|
+
entries() { return this.items.entries(); }
|
|
2115
2006
|
/**
|
|
2116
2007
|
* Returns an iterable of keys in the array
|
|
2117
2008
|
*/
|
|
2118
|
-
|
|
2009
|
+
keys() { return this.items.keys(); }
|
|
2119
2010
|
/**
|
|
2120
2011
|
* Returns an iterable of values in the array
|
|
2121
2012
|
*/
|
|
2122
|
-
|
|
2013
|
+
values() { return this.items.values(); }
|
|
2123
2014
|
/**
|
|
2124
2015
|
* Determines whether an array includes a certain element, returning true or false as appropriate.
|
|
2125
2016
|
* @param searchElement The element to search for.
|
|
2126
2017
|
* @param fromIndex The position in this array at which to begin searching for searchElement.
|
|
2127
2018
|
*/
|
|
2128
|
-
|
|
2019
|
+
includes(searchElement, fromIndex) {
|
|
2129
2020
|
return this.items.includes(searchElement, fromIndex);
|
|
2130
|
-
}
|
|
2021
|
+
}
|
|
2131
2022
|
//
|
|
2132
2023
|
// ES2022
|
|
2133
2024
|
//
|
|
@@ -2142,10 +2033,10 @@ var ArraySchema = /** @class */ (function () {
|
|
|
2142
2033
|
* thisArg is omitted, undefined is used as the this value.
|
|
2143
2034
|
*/
|
|
2144
2035
|
// @ts-ignore
|
|
2145
|
-
|
|
2036
|
+
flatMap(callback, thisArg) {
|
|
2146
2037
|
// @ts-ignore
|
|
2147
2038
|
throw new Error("ArraySchema#flatMap() is not supported.");
|
|
2148
|
-
}
|
|
2039
|
+
}
|
|
2149
2040
|
/**
|
|
2150
2041
|
* Returns a new array with all sub-array elements concatenated into it recursively up to the
|
|
2151
2042
|
* specified depth.
|
|
@@ -2153,44 +2044,43 @@ var ArraySchema = /** @class */ (function () {
|
|
|
2153
2044
|
* @param depth The maximum recursion depth
|
|
2154
2045
|
*/
|
|
2155
2046
|
// @ts-ignore
|
|
2156
|
-
|
|
2047
|
+
flat(depth) {
|
|
2157
2048
|
throw new Error("ArraySchema#flat() is not supported.");
|
|
2158
|
-
}
|
|
2159
|
-
|
|
2049
|
+
}
|
|
2050
|
+
findLast() {
|
|
2160
2051
|
// @ts-ignore
|
|
2161
2052
|
return this.items.findLast.apply(this.items, arguments);
|
|
2162
|
-
}
|
|
2163
|
-
|
|
2053
|
+
}
|
|
2054
|
+
findLastIndex(...args) {
|
|
2164
2055
|
// @ts-ignore
|
|
2165
2056
|
return this.items.findLastIndex.apply(this.items, arguments);
|
|
2166
|
-
}
|
|
2057
|
+
}
|
|
2167
2058
|
//
|
|
2168
2059
|
// ES2023
|
|
2169
2060
|
//
|
|
2170
|
-
|
|
2171
|
-
|
|
2061
|
+
with(index, value) {
|
|
2062
|
+
const copy = this.items.slice();
|
|
2172
2063
|
copy[index] = value;
|
|
2173
|
-
return new
|
|
2174
|
-
}
|
|
2175
|
-
|
|
2064
|
+
return new ArraySchema(...copy);
|
|
2065
|
+
}
|
|
2066
|
+
toReversed() {
|
|
2176
2067
|
return this.items.slice().reverse();
|
|
2177
|
-
}
|
|
2178
|
-
|
|
2068
|
+
}
|
|
2069
|
+
toSorted(compareFn) {
|
|
2179
2070
|
return this.items.slice().sort(compareFn);
|
|
2180
|
-
}
|
|
2071
|
+
}
|
|
2181
2072
|
// @ts-ignore
|
|
2182
|
-
|
|
2073
|
+
toSpliced(start, deleteCount, ...items) {
|
|
2183
2074
|
// @ts-ignore
|
|
2184
2075
|
return this.items.toSpliced.apply(copy, arguments);
|
|
2185
|
-
}
|
|
2186
|
-
|
|
2076
|
+
}
|
|
2077
|
+
[($getByIndex)](index, isEncodeAll = false) {
|
|
2187
2078
|
//
|
|
2188
2079
|
// TODO: avoid unecessary `this.tmpItems` check during decoding.
|
|
2189
2080
|
//
|
|
2190
2081
|
// ENCODING uses `this.tmpItems` (or `this.items` if `isEncodeAll` is true)
|
|
2191
2082
|
// DECODING uses `this.items`
|
|
2192
2083
|
//
|
|
2193
|
-
if (isEncodeAll === void 0) { isEncodeAll = false; }
|
|
2194
2084
|
return (isEncodeAll)
|
|
2195
2085
|
? this.items[index]
|
|
2196
2086
|
: this.deletedIndexes[index]
|
|
@@ -2199,63 +2089,79 @@ var ArraySchema = /** @class */ (function () {
|
|
|
2199
2089
|
// return (isEncodeAll)
|
|
2200
2090
|
// ? this.items[index]
|
|
2201
2091
|
// : this.tmpItems[index] ?? this.items[index];
|
|
2202
|
-
}
|
|
2203
|
-
|
|
2092
|
+
}
|
|
2093
|
+
[$deleteByIndex](index) {
|
|
2204
2094
|
this.items[index] = undefined;
|
|
2205
|
-
}
|
|
2206
|
-
|
|
2095
|
+
}
|
|
2096
|
+
[$onEncodeEnd]() {
|
|
2207
2097
|
this.tmpItems = this.items.slice();
|
|
2208
2098
|
this.deletedIndexes = {};
|
|
2209
|
-
}
|
|
2210
|
-
|
|
2211
|
-
this.items = this.items.filter(
|
|
2212
|
-
}
|
|
2213
|
-
|
|
2099
|
+
}
|
|
2100
|
+
[$onDecodeEnd]() {
|
|
2101
|
+
this.items = this.items.filter((item) => item !== undefined);
|
|
2102
|
+
}
|
|
2103
|
+
toArray() {
|
|
2214
2104
|
return this.items.slice(0);
|
|
2215
|
-
}
|
|
2216
|
-
|
|
2217
|
-
return this.toArray().map(
|
|
2105
|
+
}
|
|
2106
|
+
toJSON() {
|
|
2107
|
+
return this.toArray().map((value) => {
|
|
2218
2108
|
return (typeof (value['toJSON']) === "function")
|
|
2219
2109
|
? value['toJSON']()
|
|
2220
2110
|
: value;
|
|
2221
2111
|
});
|
|
2222
|
-
}
|
|
2112
|
+
}
|
|
2223
2113
|
//
|
|
2224
2114
|
// Decoding utilities
|
|
2225
2115
|
//
|
|
2226
|
-
|
|
2227
|
-
|
|
2116
|
+
clone(isDecoding) {
|
|
2117
|
+
let cloned;
|
|
2228
2118
|
if (isDecoding) {
|
|
2229
2119
|
cloned = new ArraySchema();
|
|
2230
|
-
cloned.push
|
|
2120
|
+
cloned.push(...this.items);
|
|
2231
2121
|
}
|
|
2232
2122
|
else {
|
|
2233
|
-
cloned = new
|
|
2123
|
+
cloned = new ArraySchema(...this.map(item => ((item[$changes])
|
|
2234
2124
|
? item.clone()
|
|
2235
|
-
: item)
|
|
2125
|
+
: item)));
|
|
2236
2126
|
}
|
|
2237
2127
|
return cloned;
|
|
2238
|
-
}
|
|
2239
|
-
|
|
2240
|
-
|
|
2241
|
-
ArraySchema[_b] = decodeArray;
|
|
2242
|
-
return ArraySchema;
|
|
2243
|
-
}());
|
|
2128
|
+
}
|
|
2129
|
+
;
|
|
2130
|
+
}
|
|
2244
2131
|
registerType("array", { constructor: ArraySchema });
|
|
2245
2132
|
|
|
2246
|
-
var
|
|
2247
|
-
|
|
2248
|
-
|
|
2249
|
-
|
|
2250
|
-
|
|
2133
|
+
var _a$3, _b$3;
|
|
2134
|
+
class MapSchema {
|
|
2135
|
+
static { this[_a$3] = encodeKeyValueOperation; }
|
|
2136
|
+
static { this[_b$3] = decodeKeyValueOperation; }
|
|
2137
|
+
/**
|
|
2138
|
+
* Determine if a property must be filtered.
|
|
2139
|
+
* - If returns false, the property is NOT going to be encoded.
|
|
2140
|
+
* - If returns true, the property is going to be encoded.
|
|
2141
|
+
*
|
|
2142
|
+
* Encoding with "filters" happens in two steps:
|
|
2143
|
+
* - First, the encoder iterates over all "not owned" properties and encodes them.
|
|
2144
|
+
* - Then, the encoder iterates over all "owned" properties per instance and encodes them.
|
|
2145
|
+
*/
|
|
2146
|
+
static [(_a$3 = $encoder, _b$3 = $decoder, $filter)](ref, index, view) {
|
|
2147
|
+
return (!view ||
|
|
2148
|
+
typeof (ref[$childType]) === "string" ||
|
|
2149
|
+
view.items.has(ref[$getByIndex](index)[$changes]));
|
|
2150
|
+
}
|
|
2151
|
+
static is(type) {
|
|
2152
|
+
return type['map'] !== undefined;
|
|
2153
|
+
}
|
|
2154
|
+
constructor(initialValues) {
|
|
2155
|
+
this.$items = new Map();
|
|
2156
|
+
this.$indexes = new Map();
|
|
2251
2157
|
this[$changes] = new ChangeTree(this);
|
|
2252
2158
|
if (initialValues) {
|
|
2253
2159
|
if (initialValues instanceof Map ||
|
|
2254
2160
|
initialValues instanceof MapSchema) {
|
|
2255
|
-
initialValues.forEach(
|
|
2161
|
+
initialValues.forEach((v, k) => this.set(k, v));
|
|
2256
2162
|
}
|
|
2257
2163
|
else {
|
|
2258
|
-
for (
|
|
2164
|
+
for (const k in initialValues) {
|
|
2259
2165
|
this.set(k, initialValues[k]);
|
|
2260
2166
|
}
|
|
2261
2167
|
}
|
|
@@ -2267,53 +2173,27 @@ var MapSchema = /** @class */ (function () {
|
|
|
2267
2173
|
configurable: true,
|
|
2268
2174
|
});
|
|
2269
2175
|
}
|
|
2270
|
-
/**
|
|
2271
|
-
* Determine if a property must be filtered.
|
|
2272
|
-
* - If returns false, the property is NOT going to be encoded.
|
|
2273
|
-
* - If returns true, the property is going to be encoded.
|
|
2274
|
-
*
|
|
2275
|
-
* Encoding with "filters" happens in two steps:
|
|
2276
|
-
* - First, the encoder iterates over all "not owned" properties and encodes them.
|
|
2277
|
-
* - Then, the encoder iterates over all "owned" properties per instance and encodes them.
|
|
2278
|
-
*/
|
|
2279
|
-
MapSchema[(_a = $encoder, _b = $decoder, $filter)] = function (ref, index, view) {
|
|
2280
|
-
return (!view ||
|
|
2281
|
-
typeof (ref[$childType]) === "string" ||
|
|
2282
|
-
view.items.has(ref[$getByIndex](index)[$changes]));
|
|
2283
|
-
};
|
|
2284
|
-
MapSchema.is = function (type) {
|
|
2285
|
-
return type['map'] !== undefined;
|
|
2286
|
-
};
|
|
2287
2176
|
/** Iterator */
|
|
2288
|
-
|
|
2289
|
-
|
|
2290
|
-
|
|
2291
|
-
|
|
2292
|
-
configurable: true
|
|
2293
|
-
});
|
|
2294
|
-
Object.defineProperty(MapSchema, Symbol.species, {
|
|
2295
|
-
get: function () { return MapSchema; },
|
|
2296
|
-
enumerable: false,
|
|
2297
|
-
configurable: true
|
|
2298
|
-
});
|
|
2299
|
-
MapSchema.prototype.set = function (key, value) {
|
|
2300
|
-
var _c;
|
|
2177
|
+
[Symbol.iterator]() { return this.$items[Symbol.iterator](); }
|
|
2178
|
+
get [Symbol.toStringTag]() { return this.$items[Symbol.toStringTag]; }
|
|
2179
|
+
static get [Symbol.species]() { return MapSchema; }
|
|
2180
|
+
set(key, value) {
|
|
2301
2181
|
if (value === undefined || value === null) {
|
|
2302
|
-
throw new Error(
|
|
2182
|
+
throw new Error(`MapSchema#set('${key}', ${value}): trying to set ${value} value on '${key}'.`);
|
|
2303
2183
|
}
|
|
2304
2184
|
// Force "key" as string
|
|
2305
2185
|
// See: https://github.com/colyseus/colyseus/issues/561#issuecomment-1646733468
|
|
2306
2186
|
key = key.toString();
|
|
2307
|
-
|
|
2187
|
+
const changeTree = this[$changes];
|
|
2308
2188
|
// get "index" for this value.
|
|
2309
|
-
|
|
2310
|
-
|
|
2189
|
+
const isReplace = typeof (changeTree.indexes[key]) !== "undefined";
|
|
2190
|
+
const index = (isReplace)
|
|
2311
2191
|
? changeTree.indexes[key]
|
|
2312
|
-
:
|
|
2313
|
-
|
|
2192
|
+
: changeTree.indexes[-1] ?? 0;
|
|
2193
|
+
let operation = (isReplace)
|
|
2314
2194
|
? exports.OPERATION.REPLACE
|
|
2315
2195
|
: exports.OPERATION.ADD;
|
|
2316
|
-
|
|
2196
|
+
const isRef = (value[$changes]) !== undefined;
|
|
2317
2197
|
//
|
|
2318
2198
|
// (encoding)
|
|
2319
2199
|
// set a unique id to relate directly with this key/value.
|
|
@@ -2342,17 +2222,17 @@ var MapSchema = /** @class */ (function () {
|
|
|
2342
2222
|
value[$changes].setParent(this, changeTree.root, index);
|
|
2343
2223
|
}
|
|
2344
2224
|
return this;
|
|
2345
|
-
}
|
|
2346
|
-
|
|
2225
|
+
}
|
|
2226
|
+
get(key) {
|
|
2347
2227
|
return this.$items.get(key);
|
|
2348
|
-
}
|
|
2349
|
-
|
|
2350
|
-
|
|
2228
|
+
}
|
|
2229
|
+
delete(key) {
|
|
2230
|
+
const index = this[$changes].indexes[key];
|
|
2351
2231
|
this[$changes].delete(index);
|
|
2352
2232
|
return this.$items.delete(key);
|
|
2353
|
-
}
|
|
2354
|
-
|
|
2355
|
-
|
|
2233
|
+
}
|
|
2234
|
+
clear() {
|
|
2235
|
+
const changeTree = this[$changes];
|
|
2356
2236
|
// discard previous operations.
|
|
2357
2237
|
changeTree.discard(true);
|
|
2358
2238
|
changeTree.indexes = {};
|
|
@@ -2361,69 +2241,64 @@ var MapSchema = /** @class */ (function () {
|
|
|
2361
2241
|
// clear items
|
|
2362
2242
|
this.$items.clear();
|
|
2363
2243
|
changeTree.operation(exports.OPERATION.CLEAR);
|
|
2364
|
-
}
|
|
2365
|
-
|
|
2244
|
+
}
|
|
2245
|
+
has(key) {
|
|
2366
2246
|
return this.$items.has(key);
|
|
2367
|
-
}
|
|
2368
|
-
|
|
2247
|
+
}
|
|
2248
|
+
forEach(callbackfn) {
|
|
2369
2249
|
this.$items.forEach(callbackfn);
|
|
2370
|
-
}
|
|
2371
|
-
|
|
2250
|
+
}
|
|
2251
|
+
entries() {
|
|
2372
2252
|
return this.$items.entries();
|
|
2373
|
-
}
|
|
2374
|
-
|
|
2253
|
+
}
|
|
2254
|
+
keys() {
|
|
2375
2255
|
return this.$items.keys();
|
|
2376
|
-
}
|
|
2377
|
-
|
|
2256
|
+
}
|
|
2257
|
+
values() {
|
|
2378
2258
|
return this.$items.values();
|
|
2379
|
-
}
|
|
2380
|
-
|
|
2381
|
-
|
|
2382
|
-
|
|
2383
|
-
|
|
2384
|
-
enumerable: false,
|
|
2385
|
-
configurable: true
|
|
2386
|
-
});
|
|
2387
|
-
MapSchema.prototype.setIndex = function (index, key) {
|
|
2259
|
+
}
|
|
2260
|
+
get size() {
|
|
2261
|
+
return this.$items.size;
|
|
2262
|
+
}
|
|
2263
|
+
setIndex(index, key) {
|
|
2388
2264
|
this.$indexes.set(index, key);
|
|
2389
|
-
}
|
|
2390
|
-
|
|
2265
|
+
}
|
|
2266
|
+
getIndex(index) {
|
|
2391
2267
|
return this.$indexes.get(index);
|
|
2392
|
-
}
|
|
2393
|
-
|
|
2268
|
+
}
|
|
2269
|
+
[$getByIndex](index) {
|
|
2394
2270
|
return this.$items.get(this.$indexes.get(index));
|
|
2395
|
-
}
|
|
2396
|
-
|
|
2397
|
-
|
|
2271
|
+
}
|
|
2272
|
+
[$deleteByIndex](index) {
|
|
2273
|
+
const key = this.$indexes.get(index);
|
|
2398
2274
|
this.$items.delete(key);
|
|
2399
2275
|
this.$indexes.delete(index);
|
|
2400
|
-
}
|
|
2401
|
-
|
|
2402
|
-
|
|
2403
|
-
|
|
2404
|
-
for (
|
|
2405
|
-
var _c = changes_1[_i], fieldIndex = _c[0], operation = _c[1];
|
|
2276
|
+
}
|
|
2277
|
+
[$onEncodeEnd]() {
|
|
2278
|
+
const changeTree = this[$changes];
|
|
2279
|
+
const changes = changeTree.changes.entries();
|
|
2280
|
+
for (const [fieldIndex, operation] of changes) {
|
|
2406
2281
|
if (operation === exports.OPERATION.DELETE) {
|
|
2407
|
-
|
|
2282
|
+
const index = this[$getByIndex](fieldIndex);
|
|
2408
2283
|
delete changeTree.indexes[index];
|
|
2409
2284
|
}
|
|
2410
2285
|
}
|
|
2411
|
-
}
|
|
2412
|
-
|
|
2413
|
-
|
|
2414
|
-
this.forEach(
|
|
2286
|
+
}
|
|
2287
|
+
toJSON() {
|
|
2288
|
+
const map = {};
|
|
2289
|
+
this.forEach((value, key) => {
|
|
2415
2290
|
map[key] = (typeof (value['toJSON']) === "function")
|
|
2416
2291
|
? value['toJSON']()
|
|
2417
2292
|
: value;
|
|
2418
2293
|
});
|
|
2419
2294
|
return map;
|
|
2420
|
-
}
|
|
2295
|
+
}
|
|
2421
2296
|
//
|
|
2422
2297
|
// Decoding utilities
|
|
2423
2298
|
//
|
|
2424
2299
|
// @ts-ignore
|
|
2425
|
-
|
|
2426
|
-
|
|
2300
|
+
clone(isDecoding) {
|
|
2301
|
+
let cloned;
|
|
2427
2302
|
if (isDecoding) {
|
|
2428
2303
|
// client-side
|
|
2429
2304
|
cloned = Object.assign(new MapSchema(), this);
|
|
@@ -2431,7 +2306,7 @@ var MapSchema = /** @class */ (function () {
|
|
|
2431
2306
|
else {
|
|
2432
2307
|
// server-side
|
|
2433
2308
|
cloned = new MapSchema();
|
|
2434
|
-
this.forEach(
|
|
2309
|
+
this.forEach((value, key) => {
|
|
2435
2310
|
if (value[$changes]) {
|
|
2436
2311
|
cloned.set(key, value['clone']());
|
|
2437
2312
|
}
|
|
@@ -2441,43 +2316,43 @@ var MapSchema = /** @class */ (function () {
|
|
|
2441
2316
|
});
|
|
2442
2317
|
}
|
|
2443
2318
|
return cloned;
|
|
2444
|
-
}
|
|
2445
|
-
|
|
2446
|
-
MapSchema[_a] = encodeKeyValueOperation;
|
|
2447
|
-
MapSchema[_b] = decodeKeyValueOperation;
|
|
2448
|
-
return MapSchema;
|
|
2449
|
-
}());
|
|
2319
|
+
}
|
|
2320
|
+
}
|
|
2450
2321
|
registerType("map", { constructor: MapSchema });
|
|
2451
2322
|
|
|
2452
|
-
|
|
2453
|
-
|
|
2454
|
-
|
|
2455
|
-
|
|
2456
|
-
|
|
2457
|
-
|
|
2458
|
-
|
|
2459
|
-
|
|
2460
|
-
|
|
2461
|
-
}
|
|
2462
|
-
TypeContext.register = function (target) {
|
|
2463
|
-
var parent = Object.getPrototypeOf(target);
|
|
2323
|
+
const DEFAULT_VIEW_TAG = -1;
|
|
2324
|
+
class TypeContext {
|
|
2325
|
+
/**
|
|
2326
|
+
* For inheritance support
|
|
2327
|
+
* Keeps track of which classes extends which. (parent -> children)
|
|
2328
|
+
*/
|
|
2329
|
+
static { this.inheritedTypes = new Map(); }
|
|
2330
|
+
static register(target) {
|
|
2331
|
+
const parent = Object.getPrototypeOf(target);
|
|
2464
2332
|
if (parent !== Schema) {
|
|
2465
|
-
|
|
2333
|
+
let inherits = TypeContext.inheritedTypes.get(parent);
|
|
2466
2334
|
if (!inherits) {
|
|
2467
2335
|
inherits = new Set();
|
|
2468
2336
|
TypeContext.inheritedTypes.set(parent, inherits);
|
|
2469
2337
|
}
|
|
2470
2338
|
inherits.add(target);
|
|
2471
2339
|
}
|
|
2472
|
-
}
|
|
2473
|
-
|
|
2340
|
+
}
|
|
2341
|
+
constructor(rootClass) {
|
|
2342
|
+
this.types = {};
|
|
2343
|
+
this.schemas = new Map();
|
|
2344
|
+
this.hasFilters = false;
|
|
2345
|
+
if (rootClass) {
|
|
2346
|
+
this.discoverTypes(rootClass);
|
|
2347
|
+
}
|
|
2348
|
+
}
|
|
2349
|
+
has(schema) {
|
|
2474
2350
|
return this.schemas.has(schema);
|
|
2475
|
-
}
|
|
2476
|
-
|
|
2351
|
+
}
|
|
2352
|
+
get(typeid) {
|
|
2477
2353
|
return this.types[typeid];
|
|
2478
|
-
}
|
|
2479
|
-
|
|
2480
|
-
if (typeid === void 0) { typeid = this.schemas.size; }
|
|
2354
|
+
}
|
|
2355
|
+
add(schema, typeid = this.schemas.size) {
|
|
2481
2356
|
// skip if already registered
|
|
2482
2357
|
if (this.schemas.has(schema)) {
|
|
2483
2358
|
return false;
|
|
@@ -2485,62 +2360,54 @@ var TypeContext = /** @class */ (function () {
|
|
|
2485
2360
|
this.types[typeid] = schema;
|
|
2486
2361
|
this.schemas.set(schema, typeid);
|
|
2487
2362
|
return true;
|
|
2488
|
-
}
|
|
2489
|
-
|
|
2363
|
+
}
|
|
2364
|
+
getTypeId(klass) {
|
|
2490
2365
|
return this.schemas.get(klass);
|
|
2491
|
-
}
|
|
2492
|
-
|
|
2493
|
-
var _this = this;
|
|
2494
|
-
var _a;
|
|
2366
|
+
}
|
|
2367
|
+
discoverTypes(klass) {
|
|
2495
2368
|
if (!this.add(klass)) {
|
|
2496
2369
|
return;
|
|
2497
2370
|
}
|
|
2498
2371
|
// add classes inherited from this base class
|
|
2499
|
-
|
|
2500
|
-
|
|
2372
|
+
TypeContext.inheritedTypes.get(klass)?.forEach((child) => {
|
|
2373
|
+
this.discoverTypes(child);
|
|
2501
2374
|
});
|
|
2502
2375
|
// skip if no fields are defined for this class.
|
|
2503
2376
|
if (klass[Symbol.metadata] === undefined) {
|
|
2504
2377
|
klass[Symbol.metadata] = {};
|
|
2505
2378
|
}
|
|
2506
2379
|
// const metadata = Metadata.getFor(klass);
|
|
2507
|
-
|
|
2380
|
+
const metadata = klass[Symbol.metadata];
|
|
2508
2381
|
// if any schema/field has filters, mark "context" as having filters.
|
|
2509
2382
|
if (metadata[-2]) {
|
|
2510
2383
|
this.hasFilters = true;
|
|
2511
2384
|
}
|
|
2512
|
-
for (
|
|
2513
|
-
|
|
2385
|
+
for (const field in metadata) {
|
|
2386
|
+
const fieldType = metadata[field].type;
|
|
2514
2387
|
if (typeof (fieldType) === "string") {
|
|
2515
2388
|
continue;
|
|
2516
2389
|
}
|
|
2517
2390
|
if (Array.isArray(fieldType)) {
|
|
2518
|
-
|
|
2519
|
-
if (
|
|
2391
|
+
const type = fieldType[0];
|
|
2392
|
+
if (type === "string") {
|
|
2520
2393
|
continue;
|
|
2521
2394
|
}
|
|
2522
|
-
this.discoverTypes(
|
|
2395
|
+
this.discoverTypes(type);
|
|
2523
2396
|
}
|
|
2524
2397
|
else if (typeof (fieldType) === "function") {
|
|
2525
2398
|
this.discoverTypes(fieldType);
|
|
2526
2399
|
}
|
|
2527
2400
|
else {
|
|
2528
|
-
|
|
2401
|
+
const type = Object.values(fieldType)[0];
|
|
2529
2402
|
// skip primitive types
|
|
2530
|
-
if (typeof (
|
|
2403
|
+
if (typeof (type) === "string") {
|
|
2531
2404
|
continue;
|
|
2532
2405
|
}
|
|
2533
|
-
this.discoverTypes(
|
|
2406
|
+
this.discoverTypes(type);
|
|
2534
2407
|
}
|
|
2535
2408
|
}
|
|
2536
|
-
}
|
|
2537
|
-
|
|
2538
|
-
* For inheritance support
|
|
2539
|
-
* Keeps track of which classes extends which. (parent -> children)
|
|
2540
|
-
*/
|
|
2541
|
-
TypeContext.inheritedTypes = new Map();
|
|
2542
|
-
return TypeContext;
|
|
2543
|
-
}());
|
|
2409
|
+
}
|
|
2410
|
+
}
|
|
2544
2411
|
/**
|
|
2545
2412
|
* [See documentation](https://docs.colyseus.io/state/schema/)
|
|
2546
2413
|
*
|
|
@@ -2683,24 +2550,21 @@ var TypeContext = /** @class */ (function () {
|
|
|
2683
2550
|
// };
|
|
2684
2551
|
// }
|
|
2685
2552
|
// }
|
|
2686
|
-
function view(tag) {
|
|
2687
|
-
if (tag === void 0) { tag = DEFAULT_VIEW_TAG; }
|
|
2553
|
+
function view(tag = DEFAULT_VIEW_TAG) {
|
|
2688
2554
|
return function (target, fieldName) {
|
|
2689
|
-
|
|
2690
|
-
|
|
2691
|
-
|
|
2692
|
-
|
|
2693
|
-
var parentMetadata = parentClass[Symbol.metadata];
|
|
2694
|
-
var metadata = ((_a = constructor[_d = Symbol.metadata]) !== null && _a !== void 0 ? _a : (constructor[_d] = Object.assign({}, constructor[Symbol.metadata], parentMetadata !== null && parentMetadata !== void 0 ? parentMetadata : Object.create(null))));
|
|
2555
|
+
const constructor = target.constructor;
|
|
2556
|
+
const parentClass = Object.getPrototypeOf(constructor);
|
|
2557
|
+
const parentMetadata = parentClass[Symbol.metadata];
|
|
2558
|
+
const metadata = (constructor[Symbol.metadata] ??= Object.assign({}, constructor[Symbol.metadata], parentMetadata ?? Object.create(null)));
|
|
2695
2559
|
if (!metadata[fieldName]) {
|
|
2696
2560
|
//
|
|
2697
2561
|
// detect index for this field, considering inheritance
|
|
2698
2562
|
//
|
|
2699
2563
|
metadata[fieldName] = {
|
|
2700
2564
|
type: undefined,
|
|
2701
|
-
index: (
|
|
2702
|
-
|
|
2703
|
-
|
|
2565
|
+
index: (metadata[-1] // current structure already has fields defined
|
|
2566
|
+
?? (parentMetadata && parentMetadata[-1]) // parent structure has fields defined
|
|
2567
|
+
?? -1) + 1 // no fields defined
|
|
2704
2568
|
};
|
|
2705
2569
|
}
|
|
2706
2570
|
Metadata.setTag(metadata, fieldName, tag);
|
|
@@ -2708,18 +2572,16 @@ function view(tag) {
|
|
|
2708
2572
|
}
|
|
2709
2573
|
function type(type, options) {
|
|
2710
2574
|
return function (target, field) {
|
|
2711
|
-
|
|
2712
|
-
var _d;
|
|
2713
|
-
var constructor = target.constructor;
|
|
2575
|
+
const constructor = target.constructor;
|
|
2714
2576
|
if (!type) {
|
|
2715
|
-
throw new Error(
|
|
2577
|
+
throw new Error(`${constructor.name}: @type() reference provided for "${field}" is undefined. Make sure you don't have any circular dependencies.`);
|
|
2716
2578
|
}
|
|
2717
2579
|
// for inheritance support
|
|
2718
2580
|
TypeContext.register(constructor);
|
|
2719
|
-
|
|
2720
|
-
|
|
2721
|
-
|
|
2722
|
-
|
|
2581
|
+
const parentClass = Object.getPrototypeOf(constructor);
|
|
2582
|
+
const parentMetadata = parentClass[Symbol.metadata];
|
|
2583
|
+
const metadata = (constructor[Symbol.metadata] ??= Object.assign({}, constructor[Symbol.metadata], parentMetadata ?? Object.create(null)));
|
|
2584
|
+
let fieldIndex;
|
|
2723
2585
|
/**
|
|
2724
2586
|
* skip if descriptor already exists for this field (`@deprecated()`)
|
|
2725
2587
|
*/
|
|
@@ -2732,11 +2594,11 @@ function type(type, options) {
|
|
|
2732
2594
|
// trying to define same property multiple times across inheritance.
|
|
2733
2595
|
// https://github.com/colyseus/colyseus-unity3d/issues/131#issuecomment-814308572
|
|
2734
2596
|
try {
|
|
2735
|
-
throw new Error(
|
|
2597
|
+
throw new Error(`@colyseus/schema: Duplicate '${field}' definition on '${constructor.name}'.\nCheck @type() annotation`);
|
|
2736
2598
|
}
|
|
2737
2599
|
catch (e) {
|
|
2738
|
-
|
|
2739
|
-
throw new Error(
|
|
2600
|
+
const definitionAtLine = e.stack.split("\n")[4].trim();
|
|
2601
|
+
throw new Error(`${e.message} ${definitionAtLine}`);
|
|
2740
2602
|
}
|
|
2741
2603
|
}
|
|
2742
2604
|
else {
|
|
@@ -2747,9 +2609,9 @@ function type(type, options) {
|
|
|
2747
2609
|
//
|
|
2748
2610
|
// detect index for this field, considering inheritance
|
|
2749
2611
|
//
|
|
2750
|
-
fieldIndex =
|
|
2751
|
-
|
|
2752
|
-
|
|
2612
|
+
fieldIndex = metadata[-1] // current structure already has fields defined
|
|
2613
|
+
?? (parentMetadata && parentMetadata[-1]) // parent structure has fields defined
|
|
2614
|
+
?? -1; // no fields defined
|
|
2753
2615
|
fieldIndex++;
|
|
2754
2616
|
}
|
|
2755
2617
|
if (options && options.manual) {
|
|
@@ -2761,13 +2623,13 @@ function type(type, options) {
|
|
|
2761
2623
|
});
|
|
2762
2624
|
}
|
|
2763
2625
|
else {
|
|
2764
|
-
|
|
2626
|
+
const complexTypeKlass = (Array.isArray(type))
|
|
2765
2627
|
? getType("array")
|
|
2766
2628
|
: (typeof (Object.keys(type)[0]) === "string") && getType(Object.keys(type)[0]);
|
|
2767
|
-
|
|
2629
|
+
const childType = (complexTypeKlass)
|
|
2768
2630
|
? Object.values(type)[0]
|
|
2769
2631
|
: type;
|
|
2770
|
-
Metadata.addField(metadata, fieldIndex, field, type, getPropertyDescriptor(
|
|
2632
|
+
Metadata.addField(metadata, fieldIndex, field, type, getPropertyDescriptor(`_${field}`, fieldIndex, childType, complexTypeKlass, metadata, field));
|
|
2771
2633
|
}
|
|
2772
2634
|
};
|
|
2773
2635
|
}
|
|
@@ -2775,8 +2637,7 @@ function getPropertyDescriptor(fieldCached, fieldIndex, type, complexTypeKlass,
|
|
|
2775
2637
|
return {
|
|
2776
2638
|
get: function () { return this[fieldCached]; },
|
|
2777
2639
|
set: function (value) {
|
|
2778
|
-
|
|
2779
|
-
var previousValue = this[fieldCached] || undefined;
|
|
2640
|
+
const previousValue = this[fieldCached] || undefined;
|
|
2780
2641
|
// skip if value is the same as cached.
|
|
2781
2642
|
if (value === previousValue) {
|
|
2782
2643
|
return;
|
|
@@ -2786,7 +2647,7 @@ function getPropertyDescriptor(fieldCached, fieldIndex, type, complexTypeKlass,
|
|
|
2786
2647
|
if (complexTypeKlass) {
|
|
2787
2648
|
// automaticallty transform Array into ArraySchema
|
|
2788
2649
|
if (complexTypeKlass.constructor === ArraySchema && !(value instanceof ArraySchema)) {
|
|
2789
|
-
value = new
|
|
2650
|
+
value = new ArraySchema(...value);
|
|
2790
2651
|
}
|
|
2791
2652
|
// automaticallty transform Map into MapSchema
|
|
2792
2653
|
if (complexTypeKlass.constructor === MapSchema && !(value instanceof MapSchema)) {
|
|
@@ -2799,7 +2660,7 @@ function getPropertyDescriptor(fieldCached, fieldIndex, type, complexTypeKlass,
|
|
|
2799
2660
|
// TODO: if there are other references to this instance, we should not remove it from root.
|
|
2800
2661
|
//
|
|
2801
2662
|
if (previousValue !== undefined && previousValue[$changes]) {
|
|
2802
|
-
|
|
2663
|
+
this[$changes].root?.remove(previousValue[$changes]);
|
|
2803
2664
|
}
|
|
2804
2665
|
// flag the change for encoding.
|
|
2805
2666
|
this.constructor[$track](this[$changes], fieldIndex, exports.OPERATION.ADD);
|
|
@@ -2827,33 +2688,30 @@ function getPropertyDescriptor(fieldCached, fieldIndex, type, complexTypeKlass,
|
|
|
2827
2688
|
* `@deprecated()` flag a field as deprecated.
|
|
2828
2689
|
* The previous `@type()` annotation should remain along with this one.
|
|
2829
2690
|
*/
|
|
2830
|
-
function deprecated(throws) {
|
|
2831
|
-
if (throws === void 0) { throws = true; }
|
|
2691
|
+
function deprecated(throws = true) {
|
|
2832
2692
|
return function (klass, field) {
|
|
2833
|
-
var _a, _b, _c;
|
|
2834
|
-
var _d;
|
|
2835
2693
|
//
|
|
2836
2694
|
// FIXME: the following block of code is repeated across `@type()`, `@deprecated()` and `@unreliable()` decorators.
|
|
2837
2695
|
//
|
|
2838
|
-
|
|
2839
|
-
|
|
2840
|
-
|
|
2841
|
-
|
|
2696
|
+
const constructor = klass.constructor;
|
|
2697
|
+
const parentClass = Object.getPrototypeOf(constructor);
|
|
2698
|
+
const parentMetadata = parentClass[Symbol.metadata];
|
|
2699
|
+
const metadata = (constructor[Symbol.metadata] ??= Object.assign({}, constructor[Symbol.metadata], parentMetadata ?? Object.create(null)));
|
|
2842
2700
|
if (!metadata[field]) {
|
|
2843
2701
|
//
|
|
2844
2702
|
// detect index for this field, considering inheritance
|
|
2845
2703
|
//
|
|
2846
2704
|
metadata[field] = {
|
|
2847
2705
|
type: undefined,
|
|
2848
|
-
index: (
|
|
2849
|
-
|
|
2850
|
-
|
|
2706
|
+
index: (metadata[-1] // current structure already has fields defined
|
|
2707
|
+
?? (parentMetadata && parentMetadata[-1]) // parent structure has fields defined
|
|
2708
|
+
?? -1) + 1 // no fields defined
|
|
2851
2709
|
};
|
|
2852
2710
|
}
|
|
2853
2711
|
metadata[field].deprecated = true;
|
|
2854
2712
|
if (throws) {
|
|
2855
2713
|
metadata[field].descriptor = {
|
|
2856
|
-
get: function () { throw new Error(
|
|
2714
|
+
get: function () { throw new Error(`${field} is deprecated.`); },
|
|
2857
2715
|
set: function (value) { },
|
|
2858
2716
|
enumerable: false,
|
|
2859
2717
|
configurable: true
|
|
@@ -2868,27 +2726,25 @@ function deprecated(throws) {
|
|
|
2868
2726
|
};
|
|
2869
2727
|
}
|
|
2870
2728
|
function defineTypes(target, fields, options) {
|
|
2871
|
-
for (
|
|
2729
|
+
for (let field in fields) {
|
|
2872
2730
|
type(fields[field], options)(target.prototype, field);
|
|
2873
2731
|
}
|
|
2874
2732
|
return target;
|
|
2875
2733
|
}
|
|
2876
2734
|
|
|
2877
2735
|
function getIndent(level) {
|
|
2878
|
-
return (new Array(level).fill(0)).map(
|
|
2879
|
-
return (i === level - 1) ? "\u2514\u2500 " : " ";
|
|
2880
|
-
}).join("");
|
|
2736
|
+
return (new Array(level).fill(0)).map((_, i) => (i === level - 1) ? `└─ ` : ` `).join("");
|
|
2881
2737
|
}
|
|
2882
2738
|
function dumpChanges(schema) {
|
|
2883
|
-
|
|
2884
|
-
|
|
2739
|
+
const $root = schema[$changes].root;
|
|
2740
|
+
const dump = {
|
|
2885
2741
|
ops: {},
|
|
2886
2742
|
refs: []
|
|
2887
2743
|
};
|
|
2888
|
-
$root.changes.forEach(
|
|
2889
|
-
dump.refs.push(
|
|
2890
|
-
operations.forEach(
|
|
2891
|
-
|
|
2744
|
+
$root.changes.forEach((operations, changeTree) => {
|
|
2745
|
+
dump.refs.push(`refId#${changeTree.refId}`);
|
|
2746
|
+
operations.forEach((op, index) => {
|
|
2747
|
+
const opName = exports.OPERATION[op];
|
|
2892
2748
|
if (!dump.ops[opName]) {
|
|
2893
2749
|
dump.ops[opName] = 0;
|
|
2894
2750
|
}
|
|
@@ -2903,7 +2759,7 @@ function getNextPowerOf2(number) {
|
|
|
2903
2759
|
return number;
|
|
2904
2760
|
}
|
|
2905
2761
|
// Find the position of the most significant bit
|
|
2906
|
-
|
|
2762
|
+
let msbPosition = 0;
|
|
2907
2763
|
while (number > 0) {
|
|
2908
2764
|
number >>= 1;
|
|
2909
2765
|
msbPosition++;
|
|
@@ -2912,40 +2768,29 @@ function getNextPowerOf2(number) {
|
|
|
2912
2768
|
return 1 << msbPosition;
|
|
2913
2769
|
}
|
|
2914
2770
|
|
|
2771
|
+
var _a$2, _b$2;
|
|
2915
2772
|
/**
|
|
2916
2773
|
* Schema encoder / decoder
|
|
2917
2774
|
*/
|
|
2918
|
-
|
|
2919
|
-
|
|
2920
|
-
|
|
2921
|
-
var args = [];
|
|
2922
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
2923
|
-
args[_i] = arguments[_i];
|
|
2924
|
-
}
|
|
2925
|
-
Schema.initialize(this);
|
|
2926
|
-
//
|
|
2927
|
-
// Assign initial values
|
|
2928
|
-
//
|
|
2929
|
-
if (args[0]) {
|
|
2930
|
-
this.assign(args[0]);
|
|
2931
|
-
}
|
|
2932
|
-
}
|
|
2775
|
+
class Schema {
|
|
2776
|
+
static { this[_a$2] = encodeSchemaOperation; }
|
|
2777
|
+
static { this[_b$2] = decodeSchemaOperation; }
|
|
2933
2778
|
/**
|
|
2934
2779
|
* Assign the property descriptors required to track changes on this instance.
|
|
2935
2780
|
* @param instance
|
|
2936
2781
|
*/
|
|
2937
|
-
|
|
2782
|
+
static initialize(instance) {
|
|
2938
2783
|
Object.defineProperty(instance, $changes, {
|
|
2939
2784
|
value: new ChangeTree(instance),
|
|
2940
2785
|
enumerable: false,
|
|
2941
2786
|
writable: true
|
|
2942
2787
|
});
|
|
2943
|
-
|
|
2788
|
+
const metadata = instance.constructor[Symbol.metadata];
|
|
2944
2789
|
// Define property descriptors
|
|
2945
|
-
for (
|
|
2790
|
+
for (const field in metadata) {
|
|
2946
2791
|
if (metadata[field].descriptor) {
|
|
2947
2792
|
// for encoder
|
|
2948
|
-
Object.defineProperty(instance,
|
|
2793
|
+
Object.defineProperty(instance, `_${field}`, {
|
|
2949
2794
|
value: undefined,
|
|
2950
2795
|
writable: true,
|
|
2951
2796
|
enumerable: false,
|
|
@@ -2969,19 +2814,18 @@ var Schema = /** @class */ (function () {
|
|
|
2969
2814
|
// instance[field] = args[0][field];
|
|
2970
2815
|
// }
|
|
2971
2816
|
}
|
|
2972
|
-
}
|
|
2973
|
-
|
|
2817
|
+
}
|
|
2818
|
+
static is(type) {
|
|
2974
2819
|
return typeof (type[Symbol.metadata]) === "object";
|
|
2975
2820
|
// const metadata = type[Symbol.metadata];
|
|
2976
2821
|
// return metadata && Object.prototype.hasOwnProperty.call(metadata, -1);
|
|
2977
|
-
}
|
|
2822
|
+
}
|
|
2978
2823
|
/**
|
|
2979
2824
|
* Track property changes
|
|
2980
2825
|
*/
|
|
2981
|
-
|
|
2982
|
-
if (operation === void 0) { operation = exports.OPERATION.ADD; }
|
|
2826
|
+
static [(_a$2 = $encoder, _b$2 = $decoder, $track)](changeTree, index, operation = exports.OPERATION.ADD) {
|
|
2983
2827
|
changeTree.change(index, operation);
|
|
2984
|
-
}
|
|
2828
|
+
}
|
|
2985
2829
|
/**
|
|
2986
2830
|
* Determine if a property must be filtered.
|
|
2987
2831
|
* - If returns false, the property is NOT going to be encoded.
|
|
@@ -2991,10 +2835,9 @@ var Schema = /** @class */ (function () {
|
|
|
2991
2835
|
* - First, the encoder iterates over all "not owned" properties and encodes them.
|
|
2992
2836
|
* - Then, the encoder iterates over all "owned" properties per instance and encodes them.
|
|
2993
2837
|
*/
|
|
2994
|
-
|
|
2995
|
-
|
|
2996
|
-
|
|
2997
|
-
var tag = metadata[metadata[index]].tag;
|
|
2838
|
+
static [$filter](ref, index, view) {
|
|
2839
|
+
const metadata = ref.constructor[Symbol.metadata];
|
|
2840
|
+
const tag = metadata[metadata[index]].tag;
|
|
2998
2841
|
if (view === undefined) {
|
|
2999
2842
|
// shared pass/encode: encode if doesn't have a tag
|
|
3000
2843
|
return tag === undefined;
|
|
@@ -3009,34 +2852,43 @@ var Schema = /** @class */ (function () {
|
|
|
3009
2852
|
}
|
|
3010
2853
|
else {
|
|
3011
2854
|
// view pass: custom tag
|
|
3012
|
-
|
|
2855
|
+
const tags = view.tags?.get(ref[$changes]);
|
|
3013
2856
|
return tags && tags.has(tag);
|
|
3014
2857
|
}
|
|
3015
|
-
}
|
|
3016
|
-
|
|
2858
|
+
}
|
|
2859
|
+
// allow inherited classes to have a constructor
|
|
2860
|
+
constructor(...args) {
|
|
2861
|
+
Schema.initialize(this);
|
|
2862
|
+
//
|
|
2863
|
+
// Assign initial values
|
|
2864
|
+
//
|
|
2865
|
+
if (args[0]) {
|
|
2866
|
+
this.assign(args[0]);
|
|
2867
|
+
}
|
|
2868
|
+
}
|
|
2869
|
+
assign(props) {
|
|
3017
2870
|
Object.assign(this, props);
|
|
3018
2871
|
return this;
|
|
3019
|
-
}
|
|
2872
|
+
}
|
|
3020
2873
|
/**
|
|
3021
2874
|
* (Server-side): Flag a property to be encoded for the next patch.
|
|
3022
2875
|
* @param instance Schema instance
|
|
3023
2876
|
* @param property string representing the property name, or number representing the index of the property.
|
|
3024
2877
|
* @param operation OPERATION to perform (detected automatically)
|
|
3025
2878
|
*/
|
|
3026
|
-
|
|
2879
|
+
setDirty(property, operation) {
|
|
3027
2880
|
this[$changes].change(this.constructor[Symbol.metadata][property].index, operation);
|
|
3028
|
-
}
|
|
3029
|
-
|
|
3030
|
-
|
|
3031
|
-
|
|
3032
|
-
var metadata = this.constructor[Symbol.metadata];
|
|
2881
|
+
}
|
|
2882
|
+
clone() {
|
|
2883
|
+
const cloned = new (this.constructor);
|
|
2884
|
+
const metadata = this.constructor[Symbol.metadata];
|
|
3033
2885
|
//
|
|
3034
2886
|
// TODO: clone all properties, not only annotated ones
|
|
3035
2887
|
//
|
|
3036
2888
|
// for (const field in this) {
|
|
3037
|
-
for (
|
|
2889
|
+
for (const field in metadata) {
|
|
3038
2890
|
if (typeof (this[field]) === "object" &&
|
|
3039
|
-
typeof (
|
|
2891
|
+
typeof (this[field]?.clone) === "function") {
|
|
3040
2892
|
// deep clone
|
|
3041
2893
|
cloned[field] = this[field].clone();
|
|
3042
2894
|
}
|
|
@@ -3046,12 +2898,12 @@ var Schema = /** @class */ (function () {
|
|
|
3046
2898
|
}
|
|
3047
2899
|
}
|
|
3048
2900
|
return cloned;
|
|
3049
|
-
}
|
|
3050
|
-
|
|
3051
|
-
|
|
3052
|
-
|
|
3053
|
-
for (
|
|
3054
|
-
|
|
2901
|
+
}
|
|
2902
|
+
toJSON() {
|
|
2903
|
+
const metadata = this.constructor[Symbol.metadata];
|
|
2904
|
+
const obj = {};
|
|
2905
|
+
for (const fieldName in metadata) {
|
|
2906
|
+
const field = metadata[fieldName];
|
|
3055
2907
|
if (!field.deprecated && this[fieldName] !== null && typeof (this[fieldName]) !== "undefined") {
|
|
3056
2908
|
obj[fieldName] = (typeof (this[fieldName]['toJSON']) === "function")
|
|
3057
2909
|
? this[fieldName]['toJSON']()
|
|
@@ -3059,30 +2911,25 @@ var Schema = /** @class */ (function () {
|
|
|
3059
2911
|
}
|
|
3060
2912
|
}
|
|
3061
2913
|
return obj;
|
|
3062
|
-
}
|
|
3063
|
-
|
|
2914
|
+
}
|
|
2915
|
+
discardAllChanges() {
|
|
3064
2916
|
this[$changes].discardAll();
|
|
3065
|
-
}
|
|
3066
|
-
|
|
2917
|
+
}
|
|
2918
|
+
[$getByIndex](index) {
|
|
3067
2919
|
return this[this.constructor[Symbol.metadata][index]];
|
|
3068
|
-
}
|
|
3069
|
-
|
|
2920
|
+
}
|
|
2921
|
+
[$deleteByIndex](index) {
|
|
3070
2922
|
this[this.constructor[Symbol.metadata][index]] = undefined;
|
|
3071
|
-
}
|
|
3072
|
-
|
|
3073
|
-
|
|
3074
|
-
|
|
3075
|
-
|
|
3076
|
-
|
|
3077
|
-
|
|
3078
|
-
|
|
3079
|
-
var output = "";
|
|
3080
|
-
output += "".concat(getIndent(level)).concat(ref.constructor.name, " (").concat(ref[$changes].refId, ")").concat(contents, "\n");
|
|
3081
|
-
changeTree.forEachChild(function (childChangeTree) {
|
|
3082
|
-
return output += _this.debugRefIds(childChangeTree.ref, jsonContents, level + 1);
|
|
3083
|
-
});
|
|
2923
|
+
}
|
|
2924
|
+
static debugRefIds(instance, jsonContents = true, level = 0) {
|
|
2925
|
+
const ref = instance;
|
|
2926
|
+
const changeTree = ref[$changes];
|
|
2927
|
+
const contents = (jsonContents) ? ` - ${JSON.stringify(ref.toJSON())}` : "";
|
|
2928
|
+
let output = "";
|
|
2929
|
+
output += `${getIndent(level)}${ref.constructor.name} (${ref[$changes].refId})${contents}\n`;
|
|
2930
|
+
changeTree.forEachChild((childChangeTree) => output += this.debugRefIds(childChangeTree.ref, jsonContents, level + 1));
|
|
3084
2931
|
return output;
|
|
3085
|
-
}
|
|
2932
|
+
}
|
|
3086
2933
|
/**
|
|
3087
2934
|
* Return a string representation of the changes on a Schema instance.
|
|
3088
2935
|
* The list of changes is cleared after each encode.
|
|
@@ -3091,46 +2938,39 @@ var Schema = /** @class */ (function () {
|
|
|
3091
2938
|
* @param isEncodeAll Return "full encode" instead of current change set.
|
|
3092
2939
|
* @returns
|
|
3093
2940
|
*/
|
|
3094
|
-
|
|
3095
|
-
|
|
3096
|
-
|
|
3097
|
-
|
|
3098
|
-
|
|
3099
|
-
var changeSetName = (isEncodeAll) ? "allChanges" : "changes";
|
|
3100
|
-
var output = "".concat(instance.constructor.name, " (").concat(changeTree.refId, ") -> .").concat(changeSetName, ":\n");
|
|
2941
|
+
static debugChanges(instance, isEncodeAll = false) {
|
|
2942
|
+
const changeTree = instance[$changes];
|
|
2943
|
+
const changeSet = (isEncodeAll) ? changeTree.allChanges : changeTree.changes;
|
|
2944
|
+
const changeSetName = (isEncodeAll) ? "allChanges" : "changes";
|
|
2945
|
+
let output = `${instance.constructor.name} (${changeTree.refId}) -> .${changeSetName}:\n`;
|
|
3101
2946
|
function dumpChangeSet(changeSet) {
|
|
3102
2947
|
Array.from(changeSet)
|
|
3103
|
-
.sort(
|
|
3104
|
-
.forEach(
|
|
3105
|
-
var index = _c[0], operation = _c[1];
|
|
3106
|
-
return output += "- [".concat(index, "]: ").concat(exports.OPERATION[operation], " (").concat(JSON.stringify(changeTree.getValue(index, isEncodeAll)), ")\n");
|
|
3107
|
-
});
|
|
2948
|
+
.sort((a, b) => a[0] - b[0])
|
|
2949
|
+
.forEach(([index, operation]) => output += `- [${index}]: ${exports.OPERATION[operation]} (${JSON.stringify(changeTree.getValue(index, isEncodeAll))})\n`);
|
|
3108
2950
|
}
|
|
3109
2951
|
dumpChangeSet(changeSet);
|
|
3110
2952
|
// display filtered changes
|
|
3111
|
-
if (!isEncodeAll &&
|
|
3112
|
-
output +=
|
|
2953
|
+
if (!isEncodeAll && changeTree.filteredChanges?.size > 0) {
|
|
2954
|
+
output += `${instance.constructor.name} (${changeTree.refId}) -> .filteredChanges:\n`;
|
|
3113
2955
|
dumpChangeSet(changeTree.filteredChanges);
|
|
3114
2956
|
}
|
|
3115
2957
|
// display filtered changes
|
|
3116
|
-
if (isEncodeAll &&
|
|
3117
|
-
output +=
|
|
2958
|
+
if (isEncodeAll && changeTree.allFilteredChanges?.size > 0) {
|
|
2959
|
+
output += `${instance.constructor.name} (${changeTree.refId}) -> .allFilteredChanges:\n`;
|
|
3118
2960
|
dumpChangeSet(changeTree.allFilteredChanges);
|
|
3119
2961
|
}
|
|
3120
2962
|
return output;
|
|
3121
|
-
}
|
|
3122
|
-
|
|
3123
|
-
|
|
3124
|
-
|
|
3125
|
-
|
|
3126
|
-
|
|
3127
|
-
|
|
3128
|
-
|
|
3129
|
-
|
|
3130
|
-
|
|
3131
|
-
|
|
3132
|
-
var parentChangeTrees = [];
|
|
3133
|
-
var parentChangeTree = (_c = changeTree.parent) === null || _c === void 0 ? void 0 : _c[$changes];
|
|
2963
|
+
}
|
|
2964
|
+
static debugChangesDeep(ref) {
|
|
2965
|
+
let output = "";
|
|
2966
|
+
const rootChangeTree = ref[$changes];
|
|
2967
|
+
const changeTrees = new Map();
|
|
2968
|
+
let totalInstances = 0;
|
|
2969
|
+
let totalOperations = 0;
|
|
2970
|
+
for (const [changeTree, changes] of (rootChangeTree.root.changes.entries())) {
|
|
2971
|
+
let includeChangeTree = false;
|
|
2972
|
+
let parentChangeTrees = [];
|
|
2973
|
+
let parentChangeTree = changeTree.parent?.[$changes];
|
|
3134
2974
|
if (changeTree === rootChangeTree) {
|
|
3135
2975
|
includeChangeTree = true;
|
|
3136
2976
|
}
|
|
@@ -3141,7 +2981,7 @@ var Schema = /** @class */ (function () {
|
|
|
3141
2981
|
includeChangeTree = true;
|
|
3142
2982
|
break;
|
|
3143
2983
|
}
|
|
3144
|
-
parentChangeTree =
|
|
2984
|
+
parentChangeTree = parentChangeTree.parent?.[$changes];
|
|
3145
2985
|
}
|
|
3146
2986
|
}
|
|
3147
2987
|
if (includeChangeTree) {
|
|
@@ -3151,55 +2991,36 @@ var Schema = /** @class */ (function () {
|
|
|
3151
2991
|
}
|
|
3152
2992
|
}
|
|
3153
2993
|
output += "---\n";
|
|
3154
|
-
output +=
|
|
3155
|
-
output +=
|
|
3156
|
-
output +=
|
|
2994
|
+
output += `root refId: ${rootChangeTree.refId}\n`;
|
|
2995
|
+
output += `Total instances: ${totalInstances}\n`;
|
|
2996
|
+
output += `Total changes: ${totalOperations}\n`;
|
|
3157
2997
|
output += "---\n";
|
|
3158
2998
|
// based on root.changes, display a tree of changes that has the "ref" instance as parent
|
|
3159
|
-
|
|
3160
|
-
for (
|
|
3161
|
-
|
|
3162
|
-
parentChangeTrees.forEach(function (parentChangeTree, level) {
|
|
2999
|
+
const visitedParents = new WeakSet();
|
|
3000
|
+
for (const [changeTree, parentChangeTrees] of changeTrees.entries()) {
|
|
3001
|
+
parentChangeTrees.forEach((parentChangeTree, level) => {
|
|
3163
3002
|
if (!visitedParents.has(parentChangeTree)) {
|
|
3164
|
-
output +=
|
|
3003
|
+
output += `${getIndent(level)}${parentChangeTree.ref.constructor.name} (refId: ${parentChangeTree.refId})\n`;
|
|
3165
3004
|
visitedParents.add(parentChangeTree);
|
|
3166
3005
|
}
|
|
3167
3006
|
});
|
|
3168
|
-
|
|
3169
|
-
|
|
3170
|
-
|
|
3171
|
-
|
|
3172
|
-
output +=
|
|
3173
|
-
for (
|
|
3174
|
-
|
|
3175
|
-
output += "".concat(getIndent(level + 1)).concat(exports.OPERATION[operation], ": ").concat(index, "\n");
|
|
3007
|
+
const changes = changeTree.changes;
|
|
3008
|
+
const level = parentChangeTrees.length;
|
|
3009
|
+
const indent = getIndent(level);
|
|
3010
|
+
const parentIndex = (level > 0) ? `(${changeTree.parentIndex}) ` : "";
|
|
3011
|
+
output += `${indent}${parentIndex}${changeTree.ref.constructor.name} (refId: ${changeTree.refId}) - changes: ${changes.size}\n`;
|
|
3012
|
+
for (const [index, operation] of changes) {
|
|
3013
|
+
output += `${getIndent(level + 1)}${exports.OPERATION[operation]}: ${index}\n`;
|
|
3176
3014
|
}
|
|
3177
3015
|
}
|
|
3178
|
-
return
|
|
3179
|
-
};
|
|
3180
|
-
var _a, _b;
|
|
3181
|
-
Schema[_a] = encodeSchemaOperation;
|
|
3182
|
-
Schema[_b] = decodeSchemaOperation;
|
|
3183
|
-
return Schema;
|
|
3184
|
-
}());
|
|
3185
|
-
|
|
3186
|
-
var CollectionSchema = /** @class */ (function () {
|
|
3187
|
-
function CollectionSchema(initialValues) {
|
|
3188
|
-
var _this = this;
|
|
3189
|
-
this.$items = new Map();
|
|
3190
|
-
this.$indexes = new Map();
|
|
3191
|
-
this.$refId = 0;
|
|
3192
|
-
this[$changes] = new ChangeTree(this);
|
|
3193
|
-
if (initialValues) {
|
|
3194
|
-
initialValues.forEach(function (v) { return _this.add(v); });
|
|
3195
|
-
}
|
|
3196
|
-
Object.defineProperty(this, $childType, {
|
|
3197
|
-
value: undefined,
|
|
3198
|
-
enumerable: false,
|
|
3199
|
-
writable: true,
|
|
3200
|
-
configurable: true,
|
|
3201
|
-
});
|
|
3016
|
+
return `${output}`;
|
|
3202
3017
|
}
|
|
3018
|
+
}
|
|
3019
|
+
|
|
3020
|
+
var _a$1, _b$1;
|
|
3021
|
+
class CollectionSchema {
|
|
3022
|
+
static { this[_a$1] = encodeKeyValueOperation; }
|
|
3023
|
+
static { this[_b$1] = decodeKeyValueOperation; }
|
|
3203
3024
|
/**
|
|
3204
3025
|
* Determine if a property must be filtered.
|
|
3205
3026
|
* - If returns false, the property is NOT going to be encoded.
|
|
@@ -3209,18 +3030,33 @@ var CollectionSchema = /** @class */ (function () {
|
|
|
3209
3030
|
* - First, the encoder iterates over all "not owned" properties and encodes them.
|
|
3210
3031
|
* - Then, the encoder iterates over all "owned" properties per instance and encodes them.
|
|
3211
3032
|
*/
|
|
3212
|
-
|
|
3033
|
+
static [(_a$1 = $encoder, _b$1 = $decoder, $filter)](ref, index, view) {
|
|
3213
3034
|
return (!view ||
|
|
3214
3035
|
typeof (ref[$childType]) === "string" ||
|
|
3215
3036
|
view.items.has(ref[$getByIndex](index)[$changes]));
|
|
3216
|
-
}
|
|
3217
|
-
|
|
3037
|
+
}
|
|
3038
|
+
static is(type) {
|
|
3218
3039
|
return type['collection'] !== undefined;
|
|
3219
|
-
}
|
|
3220
|
-
|
|
3040
|
+
}
|
|
3041
|
+
constructor(initialValues) {
|
|
3042
|
+
this.$items = new Map();
|
|
3043
|
+
this.$indexes = new Map();
|
|
3044
|
+
this.$refId = 0;
|
|
3045
|
+
this[$changes] = new ChangeTree(this);
|
|
3046
|
+
if (initialValues) {
|
|
3047
|
+
initialValues.forEach((v) => this.add(v));
|
|
3048
|
+
}
|
|
3049
|
+
Object.defineProperty(this, $childType, {
|
|
3050
|
+
value: undefined,
|
|
3051
|
+
enumerable: false,
|
|
3052
|
+
writable: true,
|
|
3053
|
+
configurable: true,
|
|
3054
|
+
});
|
|
3055
|
+
}
|
|
3056
|
+
add(value) {
|
|
3221
3057
|
// set "index" for reference.
|
|
3222
|
-
|
|
3223
|
-
|
|
3058
|
+
const index = this.$refId++;
|
|
3059
|
+
const isRef = (value[$changes]) !== undefined;
|
|
3224
3060
|
if (isRef) {
|
|
3225
3061
|
value[$changes].setParent(this, this[$changes].root, index);
|
|
3226
3062
|
}
|
|
@@ -3229,18 +3065,18 @@ var CollectionSchema = /** @class */ (function () {
|
|
|
3229
3065
|
this.$items.set(index, value);
|
|
3230
3066
|
this[$changes].change(index);
|
|
3231
3067
|
return index;
|
|
3232
|
-
}
|
|
3233
|
-
|
|
3234
|
-
|
|
3068
|
+
}
|
|
3069
|
+
at(index) {
|
|
3070
|
+
const key = Array.from(this.$items.keys())[index];
|
|
3235
3071
|
return this.$items.get(key);
|
|
3236
|
-
}
|
|
3237
|
-
|
|
3072
|
+
}
|
|
3073
|
+
entries() {
|
|
3238
3074
|
return this.$items.entries();
|
|
3239
|
-
}
|
|
3240
|
-
|
|
3241
|
-
|
|
3242
|
-
|
|
3243
|
-
|
|
3075
|
+
}
|
|
3076
|
+
delete(item) {
|
|
3077
|
+
const entries = this.$items.entries();
|
|
3078
|
+
let index;
|
|
3079
|
+
let entry;
|
|
3244
3080
|
while (entry = entries.next()) {
|
|
3245
3081
|
if (entry.done) {
|
|
3246
3082
|
break;
|
|
@@ -3256,9 +3092,9 @@ var CollectionSchema = /** @class */ (function () {
|
|
|
3256
3092
|
this[$changes].delete(index);
|
|
3257
3093
|
this.$indexes.delete(index);
|
|
3258
3094
|
return this.$items.delete(index);
|
|
3259
|
-
}
|
|
3260
|
-
|
|
3261
|
-
|
|
3095
|
+
}
|
|
3096
|
+
clear() {
|
|
3097
|
+
const changeTree = this[$changes];
|
|
3262
3098
|
// discard previous operations.
|
|
3263
3099
|
changeTree.discard(true);
|
|
3264
3100
|
changeTree.indexes = {};
|
|
@@ -3267,59 +3103,54 @@ var CollectionSchema = /** @class */ (function () {
|
|
|
3267
3103
|
// clear items
|
|
3268
3104
|
this.$items.clear();
|
|
3269
3105
|
changeTree.operation(exports.OPERATION.CLEAR);
|
|
3270
|
-
}
|
|
3271
|
-
|
|
3272
|
-
return Array.from(this.$items.values()).some(
|
|
3273
|
-
}
|
|
3274
|
-
|
|
3275
|
-
|
|
3276
|
-
|
|
3277
|
-
|
|
3278
|
-
CollectionSchema.prototype.values = function () {
|
|
3106
|
+
}
|
|
3107
|
+
has(value) {
|
|
3108
|
+
return Array.from(this.$items.values()).some((v) => v === value);
|
|
3109
|
+
}
|
|
3110
|
+
forEach(callbackfn) {
|
|
3111
|
+
this.$items.forEach((value, key, _) => callbackfn(value, key, this));
|
|
3112
|
+
}
|
|
3113
|
+
values() {
|
|
3279
3114
|
return this.$items.values();
|
|
3280
|
-
}
|
|
3281
|
-
|
|
3282
|
-
|
|
3283
|
-
|
|
3284
|
-
},
|
|
3285
|
-
enumerable: false,
|
|
3286
|
-
configurable: true
|
|
3287
|
-
});
|
|
3115
|
+
}
|
|
3116
|
+
get size() {
|
|
3117
|
+
return this.$items.size;
|
|
3118
|
+
}
|
|
3288
3119
|
/** Iterator */
|
|
3289
|
-
|
|
3120
|
+
[Symbol.iterator]() {
|
|
3290
3121
|
return this.$items.values();
|
|
3291
|
-
}
|
|
3292
|
-
|
|
3122
|
+
}
|
|
3123
|
+
setIndex(index, key) {
|
|
3293
3124
|
this.$indexes.set(index, key);
|
|
3294
|
-
}
|
|
3295
|
-
|
|
3125
|
+
}
|
|
3126
|
+
getIndex(index) {
|
|
3296
3127
|
return this.$indexes.get(index);
|
|
3297
|
-
}
|
|
3298
|
-
|
|
3128
|
+
}
|
|
3129
|
+
[$getByIndex](index) {
|
|
3299
3130
|
return this.$items.get(this.$indexes.get(index));
|
|
3300
|
-
}
|
|
3301
|
-
|
|
3302
|
-
|
|
3131
|
+
}
|
|
3132
|
+
[$deleteByIndex](index) {
|
|
3133
|
+
const key = this.$indexes.get(index);
|
|
3303
3134
|
this.$items.delete(key);
|
|
3304
3135
|
this.$indexes.delete(index);
|
|
3305
|
-
}
|
|
3306
|
-
|
|
3136
|
+
}
|
|
3137
|
+
toArray() {
|
|
3307
3138
|
return Array.from(this.$items.values());
|
|
3308
|
-
}
|
|
3309
|
-
|
|
3310
|
-
|
|
3311
|
-
this.forEach(
|
|
3139
|
+
}
|
|
3140
|
+
toJSON() {
|
|
3141
|
+
const values = [];
|
|
3142
|
+
this.forEach((value, key) => {
|
|
3312
3143
|
values.push((typeof (value['toJSON']) === "function")
|
|
3313
3144
|
? value['toJSON']()
|
|
3314
3145
|
: value);
|
|
3315
3146
|
});
|
|
3316
3147
|
return values;
|
|
3317
|
-
}
|
|
3148
|
+
}
|
|
3318
3149
|
//
|
|
3319
3150
|
// Decoding utilities
|
|
3320
3151
|
//
|
|
3321
|
-
|
|
3322
|
-
|
|
3152
|
+
clone(isDecoding) {
|
|
3153
|
+
let cloned;
|
|
3323
3154
|
if (isDecoding) {
|
|
3324
3155
|
// client-side
|
|
3325
3156
|
cloned = Object.assign(new CollectionSchema(), this);
|
|
@@ -3327,7 +3158,7 @@ var CollectionSchema = /** @class */ (function () {
|
|
|
3327
3158
|
else {
|
|
3328
3159
|
// server-side
|
|
3329
3160
|
cloned = new CollectionSchema();
|
|
3330
|
-
this.forEach(
|
|
3161
|
+
this.forEach((value) => {
|
|
3331
3162
|
if (value[$changes]) {
|
|
3332
3163
|
cloned.add(value['clone']());
|
|
3333
3164
|
}
|
|
@@ -3337,31 +3168,14 @@ var CollectionSchema = /** @class */ (function () {
|
|
|
3337
3168
|
});
|
|
3338
3169
|
}
|
|
3339
3170
|
return cloned;
|
|
3340
|
-
}
|
|
3341
|
-
|
|
3342
|
-
CollectionSchema[_a] = encodeKeyValueOperation;
|
|
3343
|
-
CollectionSchema[_b] = decodeKeyValueOperation;
|
|
3344
|
-
return CollectionSchema;
|
|
3345
|
-
}());
|
|
3171
|
+
}
|
|
3172
|
+
}
|
|
3346
3173
|
registerType("collection", { constructor: CollectionSchema, });
|
|
3347
3174
|
|
|
3348
|
-
var
|
|
3349
|
-
|
|
3350
|
-
|
|
3351
|
-
|
|
3352
|
-
this.$indexes = new Map();
|
|
3353
|
-
this.$refId = 0;
|
|
3354
|
-
this[$changes] = new ChangeTree(this);
|
|
3355
|
-
if (initialValues) {
|
|
3356
|
-
initialValues.forEach(function (v) { return _this.add(v); });
|
|
3357
|
-
}
|
|
3358
|
-
Object.defineProperty(this, $childType, {
|
|
3359
|
-
value: undefined,
|
|
3360
|
-
enumerable: false,
|
|
3361
|
-
writable: true,
|
|
3362
|
-
configurable: true,
|
|
3363
|
-
});
|
|
3364
|
-
}
|
|
3175
|
+
var _a, _b;
|
|
3176
|
+
class SetSchema {
|
|
3177
|
+
static { this[_a] = encodeKeyValueOperation; }
|
|
3178
|
+
static { this[_b] = decodeKeyValueOperation; }
|
|
3365
3179
|
/**
|
|
3366
3180
|
* Determine if a property must be filtered.
|
|
3367
3181
|
* - If returns false, the property is NOT going to be encoded.
|
|
@@ -3371,39 +3185,53 @@ var SetSchema = /** @class */ (function () {
|
|
|
3371
3185
|
* - First, the encoder iterates over all "not owned" properties and encodes them.
|
|
3372
3186
|
* - Then, the encoder iterates over all "owned" properties per instance and encodes them.
|
|
3373
3187
|
*/
|
|
3374
|
-
|
|
3188
|
+
static [(_a = $encoder, _b = $decoder, $filter)](ref, index, view) {
|
|
3375
3189
|
return (!view ||
|
|
3376
3190
|
typeof (ref[$childType]) === "string" ||
|
|
3377
3191
|
view.items.has(ref[$getByIndex](index)[$changes]));
|
|
3378
|
-
}
|
|
3379
|
-
|
|
3192
|
+
}
|
|
3193
|
+
static is(type) {
|
|
3380
3194
|
return type['set'] !== undefined;
|
|
3381
|
-
}
|
|
3382
|
-
|
|
3383
|
-
|
|
3195
|
+
}
|
|
3196
|
+
constructor(initialValues) {
|
|
3197
|
+
this.$items = new Map();
|
|
3198
|
+
this.$indexes = new Map();
|
|
3199
|
+
this.$refId = 0;
|
|
3200
|
+
this[$changes] = new ChangeTree(this);
|
|
3201
|
+
if (initialValues) {
|
|
3202
|
+
initialValues.forEach((v) => this.add(v));
|
|
3203
|
+
}
|
|
3204
|
+
Object.defineProperty(this, $childType, {
|
|
3205
|
+
value: undefined,
|
|
3206
|
+
enumerable: false,
|
|
3207
|
+
writable: true,
|
|
3208
|
+
configurable: true,
|
|
3209
|
+
});
|
|
3210
|
+
}
|
|
3211
|
+
add(value) {
|
|
3384
3212
|
// immediatelly return false if value already added.
|
|
3385
3213
|
if (this.has(value)) {
|
|
3386
3214
|
return false;
|
|
3387
3215
|
}
|
|
3388
3216
|
// set "index" for reference.
|
|
3389
|
-
|
|
3217
|
+
const index = this.$refId++;
|
|
3390
3218
|
if ((value[$changes]) !== undefined) {
|
|
3391
3219
|
value[$changes].setParent(this, this[$changes].root, index);
|
|
3392
3220
|
}
|
|
3393
|
-
|
|
3221
|
+
const operation = this[$changes].indexes[index]?.op ?? exports.OPERATION.ADD;
|
|
3394
3222
|
this[$changes].indexes[index] = index;
|
|
3395
3223
|
this.$indexes.set(index, index);
|
|
3396
3224
|
this.$items.set(index, value);
|
|
3397
3225
|
this[$changes].change(index, operation);
|
|
3398
3226
|
return index;
|
|
3399
|
-
}
|
|
3400
|
-
|
|
3227
|
+
}
|
|
3228
|
+
entries() {
|
|
3401
3229
|
return this.$items.entries();
|
|
3402
|
-
}
|
|
3403
|
-
|
|
3404
|
-
|
|
3405
|
-
|
|
3406
|
-
|
|
3230
|
+
}
|
|
3231
|
+
delete(item) {
|
|
3232
|
+
const entries = this.$items.entries();
|
|
3233
|
+
let index;
|
|
3234
|
+
let entry;
|
|
3407
3235
|
while (entry = entries.next()) {
|
|
3408
3236
|
if (entry.done) {
|
|
3409
3237
|
break;
|
|
@@ -3419,9 +3247,9 @@ var SetSchema = /** @class */ (function () {
|
|
|
3419
3247
|
this[$changes].delete(index);
|
|
3420
3248
|
this.$indexes.delete(index);
|
|
3421
3249
|
return this.$items.delete(index);
|
|
3422
|
-
}
|
|
3423
|
-
|
|
3424
|
-
|
|
3250
|
+
}
|
|
3251
|
+
clear() {
|
|
3252
|
+
const changeTree = this[$changes];
|
|
3425
3253
|
// discard previous operations.
|
|
3426
3254
|
changeTree.discard(true);
|
|
3427
3255
|
changeTree.indexes = {};
|
|
@@ -3430,11 +3258,11 @@ var SetSchema = /** @class */ (function () {
|
|
|
3430
3258
|
// clear items
|
|
3431
3259
|
this.$items.clear();
|
|
3432
3260
|
changeTree.operation(exports.OPERATION.CLEAR);
|
|
3433
|
-
}
|
|
3434
|
-
|
|
3435
|
-
|
|
3436
|
-
|
|
3437
|
-
|
|
3261
|
+
}
|
|
3262
|
+
has(value) {
|
|
3263
|
+
const values = this.$items.values();
|
|
3264
|
+
let has = false;
|
|
3265
|
+
let entry;
|
|
3438
3266
|
while (entry = values.next()) {
|
|
3439
3267
|
if (entry.done) {
|
|
3440
3268
|
break;
|
|
@@ -3445,56 +3273,51 @@ var SetSchema = /** @class */ (function () {
|
|
|
3445
3273
|
}
|
|
3446
3274
|
}
|
|
3447
3275
|
return has;
|
|
3448
|
-
}
|
|
3449
|
-
|
|
3450
|
-
|
|
3451
|
-
|
|
3452
|
-
|
|
3453
|
-
SetSchema.prototype.values = function () {
|
|
3276
|
+
}
|
|
3277
|
+
forEach(callbackfn) {
|
|
3278
|
+
this.$items.forEach((value, key, _) => callbackfn(value, key, this));
|
|
3279
|
+
}
|
|
3280
|
+
values() {
|
|
3454
3281
|
return this.$items.values();
|
|
3455
|
-
}
|
|
3456
|
-
|
|
3457
|
-
|
|
3458
|
-
|
|
3459
|
-
},
|
|
3460
|
-
enumerable: false,
|
|
3461
|
-
configurable: true
|
|
3462
|
-
});
|
|
3282
|
+
}
|
|
3283
|
+
get size() {
|
|
3284
|
+
return this.$items.size;
|
|
3285
|
+
}
|
|
3463
3286
|
/** Iterator */
|
|
3464
|
-
|
|
3287
|
+
[Symbol.iterator]() {
|
|
3465
3288
|
return this.$items.values();
|
|
3466
|
-
}
|
|
3467
|
-
|
|
3289
|
+
}
|
|
3290
|
+
setIndex(index, key) {
|
|
3468
3291
|
this.$indexes.set(index, key);
|
|
3469
|
-
}
|
|
3470
|
-
|
|
3292
|
+
}
|
|
3293
|
+
getIndex(index) {
|
|
3471
3294
|
return this.$indexes.get(index);
|
|
3472
|
-
}
|
|
3473
|
-
|
|
3295
|
+
}
|
|
3296
|
+
[$getByIndex](index) {
|
|
3474
3297
|
return this.$items.get(this.$indexes.get(index));
|
|
3475
|
-
}
|
|
3476
|
-
|
|
3477
|
-
|
|
3298
|
+
}
|
|
3299
|
+
[$deleteByIndex](index) {
|
|
3300
|
+
const key = this.$indexes.get(index);
|
|
3478
3301
|
this.$items.delete(key);
|
|
3479
3302
|
this.$indexes.delete(index);
|
|
3480
|
-
}
|
|
3481
|
-
|
|
3303
|
+
}
|
|
3304
|
+
toArray() {
|
|
3482
3305
|
return Array.from(this.$items.values());
|
|
3483
|
-
}
|
|
3484
|
-
|
|
3485
|
-
|
|
3486
|
-
this.forEach(
|
|
3306
|
+
}
|
|
3307
|
+
toJSON() {
|
|
3308
|
+
const values = [];
|
|
3309
|
+
this.forEach((value, key) => {
|
|
3487
3310
|
values.push((typeof (value['toJSON']) === "function")
|
|
3488
3311
|
? value['toJSON']()
|
|
3489
3312
|
: value);
|
|
3490
3313
|
});
|
|
3491
3314
|
return values;
|
|
3492
|
-
}
|
|
3315
|
+
}
|
|
3493
3316
|
//
|
|
3494
3317
|
// Decoding utilities
|
|
3495
3318
|
//
|
|
3496
|
-
|
|
3497
|
-
|
|
3319
|
+
clone(isDecoding) {
|
|
3320
|
+
let cloned;
|
|
3498
3321
|
if (isDecoding) {
|
|
3499
3322
|
// client-side
|
|
3500
3323
|
cloned = Object.assign(new SetSchema(), this);
|
|
@@ -3502,7 +3325,7 @@ var SetSchema = /** @class */ (function () {
|
|
|
3502
3325
|
else {
|
|
3503
3326
|
// server-side
|
|
3504
3327
|
cloned = new SetSchema();
|
|
3505
|
-
this.forEach(
|
|
3328
|
+
this.forEach((value) => {
|
|
3506
3329
|
if (value[$changes]) {
|
|
3507
3330
|
cloned.add(value['clone']());
|
|
3508
3331
|
}
|
|
@@ -3512,16 +3335,40 @@ var SetSchema = /** @class */ (function () {
|
|
|
3512
3335
|
});
|
|
3513
3336
|
}
|
|
3514
3337
|
return cloned;
|
|
3515
|
-
}
|
|
3516
|
-
|
|
3517
|
-
SetSchema[_a] = encodeKeyValueOperation;
|
|
3518
|
-
SetSchema[_b] = decodeKeyValueOperation;
|
|
3519
|
-
return SetSchema;
|
|
3520
|
-
}());
|
|
3338
|
+
}
|
|
3339
|
+
}
|
|
3521
3340
|
registerType("set", { constructor: SetSchema });
|
|
3522
3341
|
|
|
3523
|
-
|
|
3524
|
-
|
|
3342
|
+
/******************************************************************************
|
|
3343
|
+
Copyright (c) Microsoft Corporation.
|
|
3344
|
+
|
|
3345
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
3346
|
+
purpose with or without fee is hereby granted.
|
|
3347
|
+
|
|
3348
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
3349
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
3350
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
3351
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
3352
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
3353
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
3354
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
3355
|
+
***************************************************************************** */
|
|
3356
|
+
|
|
3357
|
+
function __decorate(decorators, target, key, desc) {
|
|
3358
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3359
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
3360
|
+
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;
|
|
3361
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
3362
|
+
}
|
|
3363
|
+
|
|
3364
|
+
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
3365
|
+
var e = new Error(message);
|
|
3366
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
3367
|
+
};
|
|
3368
|
+
|
|
3369
|
+
class Encoder {
|
|
3370
|
+
static { this.BUFFER_SIZE = 8 * 1024; } // 8KB
|
|
3371
|
+
constructor(root) {
|
|
3525
3372
|
this.sharedBuffer = Buffer.allocUnsafeSlow(Encoder.BUFFER_SIZE);
|
|
3526
3373
|
this.setRoot(root);
|
|
3527
3374
|
//
|
|
@@ -3534,26 +3381,22 @@ var Encoder = /** @class */ (function () {
|
|
|
3534
3381
|
// console.log("type:", id, schema.name, Object.keys(schema[Symbol.metadata]));
|
|
3535
3382
|
// });
|
|
3536
3383
|
}
|
|
3537
|
-
|
|
3538
|
-
this
|
|
3384
|
+
setRoot(state) {
|
|
3385
|
+
this.root = new Root();
|
|
3539
3386
|
this.state = state;
|
|
3540
|
-
state[$changes].setRoot(this
|
|
3541
|
-
}
|
|
3542
|
-
|
|
3543
|
-
|
|
3544
|
-
|
|
3545
|
-
|
|
3546
|
-
|
|
3547
|
-
|
|
3548
|
-
|
|
3549
|
-
|
|
3550
|
-
|
|
3551
|
-
|
|
3552
|
-
|
|
3553
|
-
var ref = changeTree.ref;
|
|
3554
|
-
var ctor = ref['constructor'];
|
|
3555
|
-
var encoder = ctor[$encoder];
|
|
3556
|
-
var filter = ctor[$filter];
|
|
3387
|
+
state[$changes].setRoot(this.root);
|
|
3388
|
+
}
|
|
3389
|
+
encode(it = { offset: 0 }, view, bytes = this.sharedBuffer, changeTrees = this.root.changes) {
|
|
3390
|
+
const initialOffset = it.offset; // cache current offset in case we need to resize the buffer
|
|
3391
|
+
const isEncodeAll = this.root.allChanges === changeTrees;
|
|
3392
|
+
const hasView = (view !== undefined);
|
|
3393
|
+
const rootChangeTree = this.state[$changes];
|
|
3394
|
+
const changeTreesIterator = changeTrees.entries();
|
|
3395
|
+
for (const [changeTree, changes] of changeTreesIterator) {
|
|
3396
|
+
const ref = changeTree.ref;
|
|
3397
|
+
const ctor = ref['constructor'];
|
|
3398
|
+
const encoder = ctor[$encoder];
|
|
3399
|
+
const filter = ctor[$filter];
|
|
3557
3400
|
if (hasView) {
|
|
3558
3401
|
if (!view.items.has(changeTree)) {
|
|
3559
3402
|
view.invisible.add(changeTree);
|
|
@@ -3568,9 +3411,8 @@ var Encoder = /** @class */ (function () {
|
|
|
3568
3411
|
bytes[it.offset++] = SWITCH_TO_STRUCTURE & 255;
|
|
3569
3412
|
number$1(bytes, changeTree.refId, it);
|
|
3570
3413
|
}
|
|
3571
|
-
|
|
3572
|
-
for (
|
|
3573
|
-
var _c = changesIterator_1[_b], fieldIndex = _c[0], operation = _c[1];
|
|
3414
|
+
const changesIterator = changes.entries();
|
|
3415
|
+
for (const [fieldIndex, operation] of changesIterator) {
|
|
3574
3416
|
//
|
|
3575
3417
|
// first pass (encodeAll), identify "filtered" operations without encoding them
|
|
3576
3418
|
// they will be encoded per client, based on their view.
|
|
@@ -3593,7 +3435,7 @@ var Encoder = /** @class */ (function () {
|
|
|
3593
3435
|
}
|
|
3594
3436
|
}
|
|
3595
3437
|
if (it.offset > bytes.byteLength) {
|
|
3596
|
-
|
|
3438
|
+
const newSize = getNextPowerOf2(this.sharedBuffer.byteLength * 2);
|
|
3597
3439
|
console.warn("@colyseus/schema encode buffer overflow. Current buffer size: " + bytes.byteLength + ", encoding offset: " + it.offset + ", new size: " + newSize);
|
|
3598
3440
|
//
|
|
3599
3441
|
// resize buffer and re-encode (TODO: can we avoid re-encoding here?)
|
|
@@ -3614,27 +3456,25 @@ var Encoder = /** @class */ (function () {
|
|
|
3614
3456
|
// return bytes;
|
|
3615
3457
|
return bytes.slice(0, it.offset);
|
|
3616
3458
|
}
|
|
3617
|
-
}
|
|
3618
|
-
|
|
3459
|
+
}
|
|
3460
|
+
encodeAll(it = { offset: 0 }) {
|
|
3619
3461
|
// console.log(`encodeAll(), this.$root.allChanges (${this.$root.allChanges.size})`);
|
|
3620
|
-
if (it === void 0) { it = { offset: 0 }; }
|
|
3621
3462
|
// Array.from(this.$root.allChanges.entries()).map((item) => {
|
|
3622
3463
|
// console.log("->", item[0].refId, item[0].ref.toJSON());
|
|
3623
3464
|
// });
|
|
3624
|
-
return this.encode(it, undefined, this.sharedBuffer, this
|
|
3625
|
-
}
|
|
3626
|
-
|
|
3627
|
-
|
|
3628
|
-
var viewOffset = it.offset;
|
|
3465
|
+
return this.encode(it, undefined, this.sharedBuffer, this.root.allChanges);
|
|
3466
|
+
}
|
|
3467
|
+
encodeAllView(view, sharedOffset, it, bytes = this.sharedBuffer) {
|
|
3468
|
+
const viewOffset = it.offset;
|
|
3629
3469
|
// console.log(`encodeAllView(), this.$root.allFilteredChanges (${this.$root.allFilteredChanges.size})`);
|
|
3630
3470
|
// this.debugAllFilteredChanges();
|
|
3631
3471
|
// try to encode "filtered" changes
|
|
3632
|
-
this.encode(it, view, bytes, this
|
|
3472
|
+
this.encode(it, view, bytes, this.root.allFilteredChanges);
|
|
3633
3473
|
return Buffer.concat([
|
|
3634
3474
|
bytes.slice(0, sharedOffset),
|
|
3635
3475
|
bytes.slice(viewOffset, it.offset)
|
|
3636
3476
|
]);
|
|
3637
|
-
}
|
|
3477
|
+
}
|
|
3638
3478
|
// debugAllFilteredChanges() {
|
|
3639
3479
|
// Array.from(this.$root.allFilteredChanges.entries()).map((item) => {
|
|
3640
3480
|
// console.log("->", { refId: item[0].refId }, item[0].ref.toJSON());
|
|
@@ -3645,28 +3485,25 @@ var Encoder = /** @class */ (function () {
|
|
|
3645
3485
|
// }
|
|
3646
3486
|
// });
|
|
3647
3487
|
// }
|
|
3648
|
-
|
|
3649
|
-
|
|
3650
|
-
var viewOffset = it.offset;
|
|
3488
|
+
encodeView(view, sharedOffset, it, bytes = this.sharedBuffer) {
|
|
3489
|
+
const viewOffset = it.offset;
|
|
3651
3490
|
// try to encode "filtered" changes
|
|
3652
|
-
this.encode(it, view, bytes, this
|
|
3491
|
+
this.encode(it, view, bytes, this.root.filteredChanges);
|
|
3653
3492
|
// encode visibility changes (add/remove for this view)
|
|
3654
|
-
|
|
3655
|
-
for (
|
|
3656
|
-
var _a = viewChangesIterator_1[_i], changeTree = _a[0], changes = _a[1];
|
|
3493
|
+
const viewChangesIterator = view.changes.entries();
|
|
3494
|
+
for (const [changeTree, changes] of viewChangesIterator) {
|
|
3657
3495
|
if (changes.size === 0) {
|
|
3658
3496
|
// FIXME: avoid having empty changes if no changes were made
|
|
3659
3497
|
// console.log("changes.size === 0", changeTree.ref.constructor.name);
|
|
3660
3498
|
continue;
|
|
3661
3499
|
}
|
|
3662
|
-
|
|
3663
|
-
|
|
3664
|
-
|
|
3500
|
+
const ref = changeTree.ref;
|
|
3501
|
+
const ctor = ref['constructor'];
|
|
3502
|
+
const encoder = ctor[$encoder];
|
|
3665
3503
|
bytes[it.offset++] = SWITCH_TO_STRUCTURE & 255;
|
|
3666
3504
|
number$1(bytes, changeTree.refId, it);
|
|
3667
|
-
|
|
3668
|
-
for (
|
|
3669
|
-
var _c = changesIterator_2[_b], fieldIndex = _c[0], operation = _c[1];
|
|
3505
|
+
const changesIterator = changes.entries();
|
|
3506
|
+
for (const [fieldIndex, operation] of changesIterator) {
|
|
3670
3507
|
// isEncodeAll = false
|
|
3671
3508
|
// hasView = true
|
|
3672
3509
|
encoder(this, bytes, changeTree, fieldIndex, operation, it, false, true);
|
|
@@ -3682,63 +3519,56 @@ var Encoder = /** @class */ (function () {
|
|
|
3682
3519
|
bytes.slice(0, sharedOffset),
|
|
3683
3520
|
bytes.slice(viewOffset, it.offset)
|
|
3684
3521
|
]);
|
|
3685
|
-
}
|
|
3686
|
-
|
|
3687
|
-
|
|
3688
|
-
|
|
3689
|
-
for (var _i = 0, changeTreesIterator_2 = changeTreesIterator; _i < changeTreesIterator_2.length; _i++) {
|
|
3690
|
-
var _a = changeTreesIterator_2[_i], changeTree = _a[0]; _a[1];
|
|
3522
|
+
}
|
|
3523
|
+
onEndEncode(changeTrees = this.root.changes) {
|
|
3524
|
+
const changeTreesIterator = changeTrees.entries();
|
|
3525
|
+
for (const [changeTree, _] of changeTreesIterator) {
|
|
3691
3526
|
changeTree.endEncode();
|
|
3692
3527
|
}
|
|
3693
|
-
}
|
|
3694
|
-
|
|
3528
|
+
}
|
|
3529
|
+
discardChanges() {
|
|
3695
3530
|
// discard shared changes
|
|
3696
|
-
if (this
|
|
3697
|
-
this.onEndEncode(this
|
|
3698
|
-
this
|
|
3531
|
+
if (this.root.changes.size > 0) {
|
|
3532
|
+
this.onEndEncode(this.root.changes);
|
|
3533
|
+
this.root.changes.clear();
|
|
3699
3534
|
}
|
|
3700
3535
|
// discard filtered changes
|
|
3701
|
-
if (this
|
|
3702
|
-
this.onEndEncode(this
|
|
3703
|
-
this
|
|
3536
|
+
if (this.root.filteredChanges.size > 0) {
|
|
3537
|
+
this.onEndEncode(this.root.filteredChanges);
|
|
3538
|
+
this.root.filteredChanges.clear();
|
|
3704
3539
|
}
|
|
3705
|
-
}
|
|
3706
|
-
|
|
3707
|
-
|
|
3708
|
-
|
|
3540
|
+
}
|
|
3541
|
+
tryEncodeTypeId(bytes, baseType, targetType, it) {
|
|
3542
|
+
const baseTypeId = this.context.getTypeId(baseType);
|
|
3543
|
+
const targetTypeId = this.context.getTypeId(targetType);
|
|
3709
3544
|
if (baseTypeId !== targetTypeId) {
|
|
3710
3545
|
bytes[it.offset++] = TYPE_ID & 255;
|
|
3711
3546
|
number$1(bytes, targetTypeId, it);
|
|
3712
3547
|
}
|
|
3713
|
-
}
|
|
3714
|
-
|
|
3715
|
-
return Encoder;
|
|
3716
|
-
}());
|
|
3548
|
+
}
|
|
3549
|
+
}
|
|
3717
3550
|
|
|
3718
3551
|
function spliceOne(arr, index) {
|
|
3719
3552
|
// manually splice an array
|
|
3720
3553
|
if (index === -1 || index >= arr.length) {
|
|
3721
3554
|
return false;
|
|
3722
3555
|
}
|
|
3723
|
-
|
|
3724
|
-
for (
|
|
3556
|
+
const len = arr.length - 1;
|
|
3557
|
+
for (let i = index; i < len; i++) {
|
|
3725
3558
|
arr[i] = arr[i + 1];
|
|
3726
3559
|
}
|
|
3727
3560
|
arr.length = len;
|
|
3728
3561
|
return true;
|
|
3729
3562
|
}
|
|
3730
3563
|
|
|
3731
|
-
|
|
3732
|
-
|
|
3733
|
-
|
|
3734
|
-
|
|
3735
|
-
|
|
3736
|
-
|
|
3737
|
-
|
|
3738
|
-
|
|
3739
|
-
}(Error));
|
|
3740
|
-
var ReferenceTracker = /** @class */ (function () {
|
|
3741
|
-
function ReferenceTracker() {
|
|
3564
|
+
class DecodingWarning extends Error {
|
|
3565
|
+
constructor(message) {
|
|
3566
|
+
super(message);
|
|
3567
|
+
this.name = "DecodingWarning";
|
|
3568
|
+
}
|
|
3569
|
+
}
|
|
3570
|
+
class ReferenceTracker {
|
|
3571
|
+
constructor() {
|
|
3742
3572
|
//
|
|
3743
3573
|
// Relation of refId => Schema structure
|
|
3744
3574
|
// For direct access of structures during decoding time.
|
|
@@ -3750,12 +3580,11 @@ var ReferenceTracker = /** @class */ (function () {
|
|
|
3750
3580
|
this.callbacks = {};
|
|
3751
3581
|
this.nextUniqueId = 0;
|
|
3752
3582
|
}
|
|
3753
|
-
|
|
3583
|
+
getNextUniqueId() {
|
|
3754
3584
|
return this.nextUniqueId++;
|
|
3755
|
-
}
|
|
3585
|
+
}
|
|
3756
3586
|
// for decoding
|
|
3757
|
-
|
|
3758
|
-
if (incrementCount === void 0) { incrementCount = true; }
|
|
3587
|
+
addRef(refId, ref, incrementCount = true) {
|
|
3759
3588
|
this.refs.set(refId, ref);
|
|
3760
3589
|
this.refIds.set(ref, refId);
|
|
3761
3590
|
if (incrementCount) {
|
|
@@ -3764,10 +3593,10 @@ var ReferenceTracker = /** @class */ (function () {
|
|
|
3764
3593
|
if (this.deletedRefs.has(refId)) {
|
|
3765
3594
|
this.deletedRefs.delete(refId);
|
|
3766
3595
|
}
|
|
3767
|
-
}
|
|
3596
|
+
}
|
|
3768
3597
|
// for decoding
|
|
3769
|
-
|
|
3770
|
-
|
|
3598
|
+
removeRef(refId) {
|
|
3599
|
+
const refCount = this.refCounts[refId];
|
|
3771
3600
|
if (refCount === undefined) {
|
|
3772
3601
|
try {
|
|
3773
3602
|
throw new DecodingWarning("trying to remove refId that doesn't exist");
|
|
@@ -3779,8 +3608,8 @@ var ReferenceTracker = /** @class */ (function () {
|
|
|
3779
3608
|
}
|
|
3780
3609
|
if (refCount === 0) {
|
|
3781
3610
|
try {
|
|
3782
|
-
|
|
3783
|
-
throw new DecodingWarning(
|
|
3611
|
+
const ref = this.refs.get(refId);
|
|
3612
|
+
throw new DecodingWarning(`trying to remove refId '${refId}' with 0 refCount (${ref.constructor.name}: ${JSON.stringify(ref)})`);
|
|
3784
3613
|
}
|
|
3785
3614
|
catch (e) {
|
|
3786
3615
|
console.warn(e);
|
|
@@ -3790,55 +3619,53 @@ var ReferenceTracker = /** @class */ (function () {
|
|
|
3790
3619
|
if ((this.refCounts[refId] = refCount - 1) <= 0) {
|
|
3791
3620
|
this.deletedRefs.add(refId);
|
|
3792
3621
|
}
|
|
3793
|
-
}
|
|
3794
|
-
|
|
3622
|
+
}
|
|
3623
|
+
clearRefs() {
|
|
3795
3624
|
this.refs.clear();
|
|
3796
3625
|
this.deletedRefs.clear();
|
|
3797
3626
|
this.refCounts = {};
|
|
3798
|
-
}
|
|
3627
|
+
}
|
|
3799
3628
|
// for decoding
|
|
3800
|
-
|
|
3801
|
-
|
|
3802
|
-
this.deletedRefs.forEach(function (refId) {
|
|
3629
|
+
garbageCollectDeletedRefs() {
|
|
3630
|
+
this.deletedRefs.forEach((refId) => {
|
|
3803
3631
|
//
|
|
3804
3632
|
// Skip active references.
|
|
3805
3633
|
//
|
|
3806
|
-
if (
|
|
3634
|
+
if (this.refCounts[refId] > 0) {
|
|
3807
3635
|
return;
|
|
3808
3636
|
}
|
|
3809
|
-
|
|
3637
|
+
const ref = this.refs.get(refId);
|
|
3810
3638
|
//
|
|
3811
3639
|
// Ensure child schema instances have their references removed as well.
|
|
3812
3640
|
//
|
|
3813
3641
|
if (Metadata.isValidInstance(ref)) {
|
|
3814
|
-
|
|
3815
|
-
for (
|
|
3816
|
-
|
|
3642
|
+
const metadata = ref['constructor'][Symbol.metadata];
|
|
3643
|
+
for (const field in metadata) {
|
|
3644
|
+
const childRefId = typeof (ref[field]) === "object" && this.refIds.get(ref[field]);
|
|
3817
3645
|
if (childRefId) {
|
|
3818
|
-
|
|
3646
|
+
this.removeRef(childRefId);
|
|
3819
3647
|
}
|
|
3820
3648
|
}
|
|
3821
3649
|
}
|
|
3822
3650
|
else {
|
|
3823
3651
|
if (typeof (Object.values(ref[$childType])[0]) === "function") {
|
|
3824
3652
|
Array.from(ref.values())
|
|
3825
|
-
.forEach(
|
|
3653
|
+
.forEach((child) => this.removeRef(this.refIds.get(child)));
|
|
3826
3654
|
}
|
|
3827
3655
|
}
|
|
3828
|
-
|
|
3829
|
-
delete
|
|
3830
|
-
delete
|
|
3656
|
+
this.refs.delete(refId); // remove ref
|
|
3657
|
+
delete this.refCounts[refId]; // remove ref count
|
|
3658
|
+
delete this.callbacks[refId]; // remove callbacks
|
|
3831
3659
|
});
|
|
3832
3660
|
// clear deleted refs.
|
|
3833
3661
|
this.deletedRefs.clear();
|
|
3834
|
-
}
|
|
3835
|
-
|
|
3836
|
-
var _this = this;
|
|
3662
|
+
}
|
|
3663
|
+
addCallback(refId, fieldOrOperation, callback) {
|
|
3837
3664
|
if (refId === undefined) {
|
|
3838
|
-
|
|
3665
|
+
const name = (typeof (fieldOrOperation) === "number")
|
|
3839
3666
|
? exports.OPERATION[fieldOrOperation]
|
|
3840
3667
|
: fieldOrOperation;
|
|
3841
|
-
throw new Error(
|
|
3668
|
+
throw new Error(`Can't addCallback on '${name}' (refId is undefined)`);
|
|
3842
3669
|
}
|
|
3843
3670
|
if (!this.callbacks[refId]) {
|
|
3844
3671
|
this.callbacks[refId] = {};
|
|
@@ -3847,20 +3674,18 @@ var ReferenceTracker = /** @class */ (function () {
|
|
|
3847
3674
|
this.callbacks[refId][fieldOrOperation] = [];
|
|
3848
3675
|
}
|
|
3849
3676
|
this.callbacks[refId][fieldOrOperation].push(callback);
|
|
3850
|
-
return
|
|
3851
|
-
}
|
|
3852
|
-
|
|
3853
|
-
|
|
3854
|
-
var index = (_c = (_b = (_a = this.callbacks) === null || _a === void 0 ? void 0 : _a[refId]) === null || _b === void 0 ? void 0 : _b[field]) === null || _c === void 0 ? void 0 : _c.indexOf(callback);
|
|
3677
|
+
return () => this.removeCallback(refId, fieldOrOperation, callback);
|
|
3678
|
+
}
|
|
3679
|
+
removeCallback(refId, field, callback) {
|
|
3680
|
+
const index = this.callbacks?.[refId]?.[field]?.indexOf(callback);
|
|
3855
3681
|
if (index !== -1) {
|
|
3856
3682
|
spliceOne(this.callbacks[refId][field], index);
|
|
3857
3683
|
}
|
|
3858
|
-
}
|
|
3859
|
-
|
|
3860
|
-
}());
|
|
3684
|
+
}
|
|
3685
|
+
}
|
|
3861
3686
|
|
|
3862
|
-
|
|
3863
|
-
|
|
3687
|
+
class Decoder {
|
|
3688
|
+
constructor(root, context) {
|
|
3864
3689
|
this.currentRefId = 0;
|
|
3865
3690
|
this.setRoot(root);
|
|
3866
3691
|
this.context = context || new TypeContext(root.constructor);
|
|
@@ -3869,19 +3694,16 @@ var Decoder = /** @class */ (function () {
|
|
|
3869
3694
|
// console.log("type:", id, schema.name, Object.keys(schema[Symbol.metadata]));
|
|
3870
3695
|
// });
|
|
3871
3696
|
}
|
|
3872
|
-
|
|
3697
|
+
setRoot(root) {
|
|
3873
3698
|
this.state = root;
|
|
3874
3699
|
this.$root = new ReferenceTracker();
|
|
3875
3700
|
this.$root.addRef(0, root);
|
|
3876
|
-
}
|
|
3877
|
-
|
|
3878
|
-
|
|
3879
|
-
|
|
3880
|
-
|
|
3881
|
-
|
|
3882
|
-
var $root = this.$root;
|
|
3883
|
-
var totalBytes = bytes.byteLength;
|
|
3884
|
-
var decoder = ref['constructor'][$decoder];
|
|
3701
|
+
}
|
|
3702
|
+
decode(bytes, it = { offset: 0 }, ref = this.state) {
|
|
3703
|
+
const allChanges = [];
|
|
3704
|
+
const $root = this.$root;
|
|
3705
|
+
const totalBytes = bytes.byteLength;
|
|
3706
|
+
let decoder = ref['constructor'][$decoder];
|
|
3885
3707
|
this.currentRefId = 0;
|
|
3886
3708
|
while (it.offset < totalBytes) {
|
|
3887
3709
|
//
|
|
@@ -3890,26 +3712,26 @@ var Decoder = /** @class */ (function () {
|
|
|
3890
3712
|
if (bytes[it.offset] == SWITCH_TO_STRUCTURE) {
|
|
3891
3713
|
it.offset++;
|
|
3892
3714
|
this.currentRefId = number(bytes, it);
|
|
3893
|
-
|
|
3715
|
+
const nextRef = $root.refs.get(this.currentRefId);
|
|
3894
3716
|
//
|
|
3895
3717
|
// Trying to access a reference that haven't been decoded yet.
|
|
3896
3718
|
//
|
|
3897
3719
|
if (!nextRef) {
|
|
3898
|
-
throw new Error("
|
|
3720
|
+
throw new Error(`"refId" not found: ${this.currentRefId}`);
|
|
3899
3721
|
}
|
|
3900
|
-
|
|
3722
|
+
ref[$onDecodeEnd]?.();
|
|
3901
3723
|
ref = nextRef;
|
|
3902
3724
|
decoder = ref['constructor'][$decoder];
|
|
3903
3725
|
continue;
|
|
3904
3726
|
}
|
|
3905
|
-
|
|
3727
|
+
const result = decoder(this, bytes, it, ref, allChanges);
|
|
3906
3728
|
if (result === DEFINITION_MISMATCH) {
|
|
3907
3729
|
console.warn("@colyseus/schema: definition mismatch");
|
|
3908
3730
|
//
|
|
3909
3731
|
// keep skipping next bytes until reaches a known structure
|
|
3910
3732
|
// by local decoder.
|
|
3911
3733
|
//
|
|
3912
|
-
|
|
3734
|
+
const nextIterator = { offset: it.offset };
|
|
3913
3735
|
while (it.offset < totalBytes) {
|
|
3914
3736
|
if (switchStructureCheck(bytes, it)) {
|
|
3915
3737
|
nextIterator.offset = it.offset + 1;
|
|
@@ -3923,130 +3745,118 @@ var Decoder = /** @class */ (function () {
|
|
|
3923
3745
|
}
|
|
3924
3746
|
}
|
|
3925
3747
|
// FIXME: DRY with SWITCH_TO_STRUCTURE block.
|
|
3926
|
-
|
|
3748
|
+
ref[$onDecodeEnd]?.();
|
|
3927
3749
|
// trigger changes
|
|
3928
|
-
|
|
3750
|
+
this.triggerChanges?.(allChanges);
|
|
3929
3751
|
// drop references of unused schemas
|
|
3930
3752
|
$root.garbageCollectDeletedRefs();
|
|
3931
3753
|
return allChanges;
|
|
3932
|
-
}
|
|
3933
|
-
|
|
3934
|
-
|
|
3754
|
+
}
|
|
3755
|
+
getInstanceType(bytes, it, defaultType) {
|
|
3756
|
+
let type;
|
|
3935
3757
|
if (bytes[it.offset] === TYPE_ID) {
|
|
3936
3758
|
it.offset++;
|
|
3937
|
-
|
|
3759
|
+
const type_id = number(bytes, it);
|
|
3938
3760
|
type = this.context.get(type_id);
|
|
3939
3761
|
}
|
|
3940
3762
|
return type || defaultType;
|
|
3941
|
-
}
|
|
3942
|
-
|
|
3763
|
+
}
|
|
3764
|
+
createInstanceOfType(type) {
|
|
3943
3765
|
// let instance: Schema = new (type as any)();
|
|
3944
3766
|
// // assign root on $changes
|
|
3945
3767
|
// instance[$changes].root = this.root[$changes].root;
|
|
3946
3768
|
// return instance;
|
|
3947
3769
|
return new type();
|
|
3948
|
-
}
|
|
3949
|
-
|
|
3950
|
-
|
|
3951
|
-
|
|
3952
|
-
|
|
3953
|
-
|
|
3954
|
-
ref.forEach(function (value, key) {
|
|
3770
|
+
}
|
|
3771
|
+
removeChildRefs(ref, allChanges) {
|
|
3772
|
+
const changeTree = ref[$changes];
|
|
3773
|
+
const needRemoveRef = typeof (ref[$childType]) !== "string";
|
|
3774
|
+
const refId = changeTree.refId;
|
|
3775
|
+
ref.forEach((value, key) => {
|
|
3955
3776
|
allChanges.push({
|
|
3956
3777
|
ref: value,
|
|
3957
|
-
refId
|
|
3778
|
+
refId,
|
|
3958
3779
|
op: exports.OPERATION.DELETE,
|
|
3959
3780
|
field: key,
|
|
3960
3781
|
value: undefined,
|
|
3961
3782
|
previousValue: value
|
|
3962
3783
|
});
|
|
3963
3784
|
if (needRemoveRef) {
|
|
3964
|
-
|
|
3785
|
+
this.$root.removeRef(this.$root.refIds.get(value));
|
|
3965
3786
|
}
|
|
3966
3787
|
});
|
|
3967
|
-
}
|
|
3968
|
-
|
|
3969
|
-
}());
|
|
3788
|
+
}
|
|
3789
|
+
}
|
|
3970
3790
|
|
|
3971
3791
|
/**
|
|
3972
3792
|
* Reflection
|
|
3973
3793
|
*/
|
|
3974
|
-
|
|
3975
|
-
|
|
3976
|
-
|
|
3977
|
-
|
|
3978
|
-
|
|
3979
|
-
|
|
3980
|
-
|
|
3981
|
-
|
|
3982
|
-
|
|
3983
|
-
|
|
3984
|
-
|
|
3985
|
-
|
|
3986
|
-
|
|
3987
|
-
|
|
3988
|
-
|
|
3989
|
-
}
|
|
3990
|
-
|
|
3991
|
-
|
|
3992
|
-
|
|
3993
|
-
|
|
3994
|
-
|
|
3995
|
-
|
|
3996
|
-
|
|
3997
|
-
|
|
3998
|
-
|
|
3999
|
-
|
|
4000
|
-
|
|
4001
|
-
|
|
4002
|
-
|
|
4003
|
-
|
|
4004
|
-
|
|
4005
|
-
|
|
4006
|
-
return ReflectionType;
|
|
4007
|
-
}(Schema));
|
|
4008
|
-
var Reflection = /** @class */ (function (_super) {
|
|
4009
|
-
__extends(Reflection, _super);
|
|
4010
|
-
function Reflection() {
|
|
4011
|
-
var _this = _super !== null && _super.apply(this, arguments) || this;
|
|
4012
|
-
_this.types = new ArraySchema();
|
|
4013
|
-
return _this;
|
|
4014
|
-
}
|
|
4015
|
-
Reflection.encode = function (instance, context) {
|
|
3794
|
+
class ReflectionField extends Schema {
|
|
3795
|
+
}
|
|
3796
|
+
__decorate([
|
|
3797
|
+
type("string")
|
|
3798
|
+
], ReflectionField.prototype, "name", void 0);
|
|
3799
|
+
__decorate([
|
|
3800
|
+
type("string")
|
|
3801
|
+
], ReflectionField.prototype, "type", void 0);
|
|
3802
|
+
__decorate([
|
|
3803
|
+
type("number")
|
|
3804
|
+
], ReflectionField.prototype, "referencedType", void 0);
|
|
3805
|
+
class ReflectionType extends Schema {
|
|
3806
|
+
constructor() {
|
|
3807
|
+
super(...arguments);
|
|
3808
|
+
this.fields = new ArraySchema();
|
|
3809
|
+
}
|
|
3810
|
+
}
|
|
3811
|
+
__decorate([
|
|
3812
|
+
type("number")
|
|
3813
|
+
], ReflectionType.prototype, "id", void 0);
|
|
3814
|
+
__decorate([
|
|
3815
|
+
type("number")
|
|
3816
|
+
], ReflectionType.prototype, "extendsId", void 0);
|
|
3817
|
+
__decorate([
|
|
3818
|
+
type([ReflectionField])
|
|
3819
|
+
], ReflectionType.prototype, "fields", void 0);
|
|
3820
|
+
class Reflection extends Schema {
|
|
3821
|
+
constructor() {
|
|
3822
|
+
super(...arguments);
|
|
3823
|
+
this.types = new ArraySchema();
|
|
3824
|
+
}
|
|
3825
|
+
static encode(instance, context) {
|
|
4016
3826
|
if (!context) {
|
|
4017
3827
|
context = new TypeContext(instance.constructor);
|
|
4018
3828
|
}
|
|
4019
|
-
|
|
4020
|
-
|
|
4021
|
-
|
|
4022
|
-
for (
|
|
3829
|
+
const reflection = new Reflection();
|
|
3830
|
+
const encoder = new Encoder(reflection);
|
|
3831
|
+
const buildType = (currentType, metadata) => {
|
|
3832
|
+
for (const fieldName in metadata) {
|
|
4023
3833
|
// skip fields from parent classes
|
|
4024
3834
|
if (!Object.prototype.hasOwnProperty.call(metadata, fieldName)) {
|
|
4025
3835
|
continue;
|
|
4026
3836
|
}
|
|
4027
|
-
|
|
3837
|
+
const field = new ReflectionField();
|
|
4028
3838
|
field.name = fieldName;
|
|
4029
|
-
|
|
4030
|
-
|
|
4031
|
-
if (typeof (
|
|
4032
|
-
fieldType =
|
|
3839
|
+
let fieldType;
|
|
3840
|
+
const type = metadata[fieldName].type;
|
|
3841
|
+
if (typeof (type) === "string") {
|
|
3842
|
+
fieldType = type;
|
|
4033
3843
|
}
|
|
4034
3844
|
else {
|
|
4035
|
-
|
|
3845
|
+
let childTypeSchema;
|
|
4036
3846
|
//
|
|
4037
3847
|
// TODO: refactor below.
|
|
4038
3848
|
//
|
|
4039
|
-
if (Schema.is(
|
|
3849
|
+
if (Schema.is(type)) {
|
|
4040
3850
|
fieldType = "ref";
|
|
4041
|
-
childTypeSchema =
|
|
3851
|
+
childTypeSchema = type;
|
|
4042
3852
|
}
|
|
4043
3853
|
else {
|
|
4044
|
-
fieldType = Object.keys(
|
|
4045
|
-
if (typeof (
|
|
4046
|
-
fieldType += ":" +
|
|
3854
|
+
fieldType = Object.keys(type)[0];
|
|
3855
|
+
if (typeof (type[fieldType]) === "string") {
|
|
3856
|
+
fieldType += ":" + type[fieldType]; // array:string
|
|
4047
3857
|
}
|
|
4048
3858
|
else {
|
|
4049
|
-
childTypeSchema =
|
|
3859
|
+
childTypeSchema = type[fieldType];
|
|
4050
3860
|
}
|
|
4051
3861
|
}
|
|
4052
3862
|
field.referencedType = (childTypeSchema)
|
|
@@ -4058,59 +3868,53 @@ var Reflection = /** @class */ (function (_super) {
|
|
|
4058
3868
|
}
|
|
4059
3869
|
reflection.types.push(currentType);
|
|
4060
3870
|
};
|
|
4061
|
-
for (
|
|
4062
|
-
|
|
4063
|
-
|
|
4064
|
-
|
|
3871
|
+
for (let typeid in context.types) {
|
|
3872
|
+
const klass = context.types[typeid];
|
|
3873
|
+
const type = new ReflectionType();
|
|
3874
|
+
type.id = Number(typeid);
|
|
4065
3875
|
// support inheritance
|
|
4066
|
-
|
|
3876
|
+
const inheritFrom = Object.getPrototypeOf(klass);
|
|
4067
3877
|
if (inheritFrom !== Schema) {
|
|
4068
|
-
|
|
3878
|
+
type.extendsId = context.schemas.get(inheritFrom);
|
|
4069
3879
|
}
|
|
4070
|
-
buildType(
|
|
3880
|
+
buildType(type, klass[Symbol.metadata]);
|
|
4071
3881
|
}
|
|
4072
|
-
|
|
4073
|
-
|
|
3882
|
+
const it = { offset: 0 };
|
|
3883
|
+
const buf = encoder.encodeAll(it);
|
|
4074
3884
|
return Buffer.from(buf, 0, it.offset);
|
|
4075
|
-
}
|
|
4076
|
-
|
|
4077
|
-
|
|
4078
|
-
|
|
3885
|
+
}
|
|
3886
|
+
static decode(bytes, it) {
|
|
3887
|
+
const reflection = new Reflection();
|
|
3888
|
+
const reflectionDecoder = new Decoder(reflection);
|
|
4079
3889
|
reflectionDecoder.decode(bytes, it);
|
|
4080
|
-
|
|
4081
|
-
|
|
4082
|
-
|
|
4083
|
-
|
|
4084
|
-
|
|
4085
|
-
function _() {
|
|
4086
|
-
return _super !== null && _super.apply(this, arguments) || this;
|
|
4087
|
-
}
|
|
4088
|
-
return _;
|
|
4089
|
-
}(parentKlass));
|
|
3890
|
+
const context = new TypeContext();
|
|
3891
|
+
const schemaTypes = reflection.types.reduce((types, reflectionType) => {
|
|
3892
|
+
const parentKlass = types[reflectionType.extendsId] || Schema;
|
|
3893
|
+
const schema = class _ extends parentKlass {
|
|
3894
|
+
};
|
|
4090
3895
|
// const _metadata = Object.create(_classSuper[Symbol.metadata] ?? null);
|
|
4091
|
-
|
|
3896
|
+
const _metadata = parentKlass && parentKlass[Symbol.metadata] || Object.create(null);
|
|
4092
3897
|
Object.defineProperty(schema, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
|
4093
3898
|
// register for inheritance support
|
|
4094
3899
|
TypeContext.register(schema);
|
|
4095
|
-
|
|
3900
|
+
const typeid = reflectionType.id;
|
|
4096
3901
|
types[typeid] = schema;
|
|
4097
3902
|
context.add(schema, typeid);
|
|
4098
3903
|
return types;
|
|
4099
3904
|
}, {});
|
|
4100
|
-
reflection.types.forEach(
|
|
4101
|
-
|
|
4102
|
-
|
|
4103
|
-
|
|
4104
|
-
|
|
4105
|
-
reflectionType.fields.forEach(
|
|
4106
|
-
|
|
4107
|
-
var fieldIndex = parentFieldIndex + i;
|
|
3905
|
+
reflection.types.forEach((reflectionType) => {
|
|
3906
|
+
const schemaType = schemaTypes[reflectionType.id];
|
|
3907
|
+
const metadata = schemaType[Symbol.metadata];
|
|
3908
|
+
const parentKlass = reflection.types[reflectionType.extendsId];
|
|
3909
|
+
const parentFieldIndex = parentKlass && parentKlass.fields.length || 0;
|
|
3910
|
+
reflectionType.fields.forEach((field, i) => {
|
|
3911
|
+
const fieldIndex = parentFieldIndex + i;
|
|
4108
3912
|
if (field.referencedType !== undefined) {
|
|
4109
|
-
|
|
4110
|
-
|
|
3913
|
+
let fieldType = field.type;
|
|
3914
|
+
let refType = schemaTypes[field.referencedType];
|
|
4111
3915
|
// map or array of primitive type (-1)
|
|
4112
3916
|
if (!refType) {
|
|
4113
|
-
|
|
3917
|
+
const typeInfo = field.type.split(":");
|
|
4114
3918
|
fieldType = typeInfo[0];
|
|
4115
3919
|
refType = typeInfo[1];
|
|
4116
3920
|
}
|
|
@@ -4120,7 +3924,7 @@ var Reflection = /** @class */ (function (_super) {
|
|
|
4120
3924
|
}
|
|
4121
3925
|
else {
|
|
4122
3926
|
// type({ [fieldType]: refType } as DefinitionType)(schemaType.prototype, field.name);
|
|
4123
|
-
Metadata.addField(metadata, fieldIndex, field.name,
|
|
3927
|
+
Metadata.addField(metadata, fieldIndex, field.name, { [fieldType]: refType });
|
|
4124
3928
|
}
|
|
4125
3929
|
}
|
|
4126
3930
|
else {
|
|
@@ -4130,15 +3934,14 @@ var Reflection = /** @class */ (function (_super) {
|
|
|
4130
3934
|
});
|
|
4131
3935
|
});
|
|
4132
3936
|
return new (schemaTypes[0])();
|
|
4133
|
-
}
|
|
4134
|
-
|
|
4135
|
-
|
|
4136
|
-
]
|
|
4137
|
-
|
|
4138
|
-
}(Schema));
|
|
3937
|
+
}
|
|
3938
|
+
}
|
|
3939
|
+
__decorate([
|
|
3940
|
+
type([ReflectionType])
|
|
3941
|
+
], Reflection.prototype, "types", void 0);
|
|
4139
3942
|
|
|
4140
|
-
|
|
4141
|
-
|
|
3943
|
+
class StateView {
|
|
3944
|
+
constructor() {
|
|
4142
3945
|
/**
|
|
4143
3946
|
* List of ChangeTree's that are visible to this view
|
|
4144
3947
|
*/
|
|
@@ -4154,22 +3957,17 @@ var StateView = /** @class */ (function () {
|
|
|
4154
3957
|
this.changes = new Map();
|
|
4155
3958
|
}
|
|
4156
3959
|
// TODO: allow to set multiple tags at once
|
|
4157
|
-
|
|
4158
|
-
var _this = this;
|
|
4159
|
-
var _a, _b;
|
|
4160
|
-
if (tag === void 0) { tag = DEFAULT_VIEW_TAG; }
|
|
3960
|
+
add(obj, tag = DEFAULT_VIEW_TAG) {
|
|
4161
3961
|
if (!obj[$changes]) {
|
|
4162
3962
|
console.warn("StateView#add(), invalid object:", obj);
|
|
4163
3963
|
return this;
|
|
4164
3964
|
}
|
|
4165
|
-
|
|
3965
|
+
let changeTree = obj[$changes];
|
|
4166
3966
|
this.items.add(changeTree);
|
|
4167
3967
|
// Add children of this ChangeTree to this view
|
|
4168
|
-
changeTree.forEachChild(
|
|
4169
|
-
return _this.add(change.ref, tag);
|
|
4170
|
-
});
|
|
3968
|
+
changeTree.forEachChild((change, _) => this.add(change.ref, tag));
|
|
4171
3969
|
// FIXME: ArraySchema/MapSchema does not have metadata
|
|
4172
|
-
|
|
3970
|
+
const metadata = obj.constructor[Symbol.metadata];
|
|
4173
3971
|
// add parent ChangeTree's, if they are invisible to this view
|
|
4174
3972
|
// TODO: REFACTOR addParent()
|
|
4175
3973
|
this.addParent(changeTree, tag);
|
|
@@ -4177,7 +3975,7 @@ var StateView = /** @class */ (function () {
|
|
|
4177
3975
|
// TODO: when adding an item of a MapSchema, the changes may not
|
|
4178
3976
|
// be set (only the parent's changes are set)
|
|
4179
3977
|
//
|
|
4180
|
-
|
|
3978
|
+
let changes = this.changes.get(changeTree);
|
|
4181
3979
|
if (changes === undefined) {
|
|
4182
3980
|
changes = new Map();
|
|
4183
3981
|
this.changes.set(changeTree, changes);
|
|
@@ -4187,7 +3985,7 @@ var StateView = /** @class */ (function () {
|
|
|
4187
3985
|
if (!this.tags) {
|
|
4188
3986
|
this.tags = new WeakMap();
|
|
4189
3987
|
}
|
|
4190
|
-
|
|
3988
|
+
let tags;
|
|
4191
3989
|
if (!this.tags.has(changeTree)) {
|
|
4192
3990
|
tags = new Set();
|
|
4193
3991
|
this.tags.set(changeTree, tags);
|
|
@@ -4198,7 +3996,7 @@ var StateView = /** @class */ (function () {
|
|
|
4198
3996
|
tags.add(tag);
|
|
4199
3997
|
// console.log("BY TAG:", tag);
|
|
4200
3998
|
// Ref: add tagged properties
|
|
4201
|
-
|
|
3999
|
+
metadata?.[-3]?.[tag]?.forEach((index) => {
|
|
4202
4000
|
if (changeTree.getChange(index) !== exports.OPERATION.DELETE) {
|
|
4203
4001
|
changes.set(index, exports.OPERATION.ADD);
|
|
4204
4002
|
}
|
|
@@ -4212,14 +4010,13 @@ var StateView = /** @class */ (function () {
|
|
|
4212
4010
|
// changes.set(index, OPERATION.ADD);
|
|
4213
4011
|
// }
|
|
4214
4012
|
// });
|
|
4215
|
-
|
|
4013
|
+
const allChangesSet = (changeTree.isFiltered || changeTree.isPartiallyFiltered)
|
|
4216
4014
|
? changeTree.allFilteredChanges
|
|
4217
4015
|
: changeTree.allChanges;
|
|
4218
|
-
|
|
4219
|
-
|
|
4220
|
-
for (
|
|
4221
|
-
|
|
4222
|
-
if ((isInvisible || (metadata === null || metadata === void 0 ? void 0 : metadata[metadata === null || metadata === void 0 ? void 0 : metadata[index]].tag) === tag) &&
|
|
4016
|
+
const it = allChangesSet.keys();
|
|
4017
|
+
const isInvisible = this.invisible.has(changeTree);
|
|
4018
|
+
for (const index of it) {
|
|
4019
|
+
if ((isInvisible || metadata?.[metadata?.[index]].tag === tag) &&
|
|
4223
4020
|
changeTree.getChange(index) !== exports.OPERATION.DELETE) {
|
|
4224
4021
|
changes.set(index, exports.OPERATION.ADD);
|
|
4225
4022
|
}
|
|
@@ -4232,14 +4029,14 @@ var StateView = /** @class */ (function () {
|
|
|
4232
4029
|
this.items.add(changeTree);
|
|
4233
4030
|
}
|
|
4234
4031
|
return this;
|
|
4235
|
-
}
|
|
4236
|
-
|
|
4237
|
-
|
|
4032
|
+
}
|
|
4033
|
+
addParent(changeTree, tag) {
|
|
4034
|
+
const parentRef = changeTree.parent;
|
|
4238
4035
|
if (!parentRef) {
|
|
4239
4036
|
return;
|
|
4240
4037
|
}
|
|
4241
|
-
|
|
4242
|
-
|
|
4038
|
+
const parentChangeTree = parentRef[$changes];
|
|
4039
|
+
const parentIndex = changeTree.parentIndex;
|
|
4243
4040
|
if (!this.invisible.has(parentChangeTree)) {
|
|
4244
4041
|
// parent is already available, no need to add it!
|
|
4245
4042
|
return;
|
|
@@ -4247,7 +4044,7 @@ var StateView = /** @class */ (function () {
|
|
|
4247
4044
|
this.addParent(parentChangeTree, tag);
|
|
4248
4045
|
// add parent's tag properties
|
|
4249
4046
|
if (parentChangeTree.getChange(parentIndex) !== exports.OPERATION.DELETE) {
|
|
4250
|
-
|
|
4047
|
+
let parentChanges = this.changes.get(parentChangeTree);
|
|
4251
4048
|
if (parentChanges === undefined) {
|
|
4252
4049
|
parentChanges = new Map();
|
|
4253
4050
|
this.changes.set(parentChangeTree, parentChanges);
|
|
@@ -4263,7 +4060,7 @@ var StateView = /** @class */ (function () {
|
|
|
4263
4060
|
if (!this.tags) {
|
|
4264
4061
|
this.tags = new WeakMap();
|
|
4265
4062
|
}
|
|
4266
|
-
|
|
4063
|
+
let tags;
|
|
4267
4064
|
if (!this.tags.has(parentChangeTree)) {
|
|
4268
4065
|
tags = new Set();
|
|
4269
4066
|
this.tags.set(parentChangeTree, tags);
|
|
@@ -4274,51 +4071,46 @@ var StateView = /** @class */ (function () {
|
|
|
4274
4071
|
tags.add(tag);
|
|
4275
4072
|
parentChanges.set(parentIndex, exports.OPERATION.ADD);
|
|
4276
4073
|
}
|
|
4277
|
-
}
|
|
4278
|
-
|
|
4279
|
-
|
|
4280
|
-
var changeTree = obj[$changes];
|
|
4074
|
+
}
|
|
4075
|
+
remove(obj, tag = DEFAULT_VIEW_TAG) {
|
|
4076
|
+
const changeTree = obj[$changes];
|
|
4281
4077
|
if (!changeTree) {
|
|
4282
4078
|
console.warn("StateView#remove(), invalid object:", obj);
|
|
4283
4079
|
return this;
|
|
4284
4080
|
}
|
|
4285
4081
|
this.items.delete(changeTree);
|
|
4286
|
-
|
|
4287
|
-
|
|
4288
|
-
|
|
4082
|
+
const ref = changeTree.ref;
|
|
4083
|
+
const metadata = ref.constructor[Symbol.metadata];
|
|
4084
|
+
let changes = this.changes.get(changeTree);
|
|
4289
4085
|
if (changes === undefined) {
|
|
4290
4086
|
changes = new Map();
|
|
4291
4087
|
this.changes.set(changeTree, changes);
|
|
4292
4088
|
}
|
|
4293
4089
|
if (tag === DEFAULT_VIEW_TAG) {
|
|
4294
4090
|
// parent is collection (Map/Array)
|
|
4295
|
-
|
|
4091
|
+
const parent = changeTree.parent;
|
|
4296
4092
|
if (!Metadata.isValidInstance(parent)) {
|
|
4297
|
-
|
|
4298
|
-
|
|
4299
|
-
if (
|
|
4300
|
-
|
|
4301
|
-
this.changes.set(parentChangeTree,
|
|
4093
|
+
const parentChangeTree = parent[$changes];
|
|
4094
|
+
let changes = this.changes.get(parentChangeTree);
|
|
4095
|
+
if (changes === undefined) {
|
|
4096
|
+
changes = new Map();
|
|
4097
|
+
this.changes.set(parentChangeTree, changes);
|
|
4302
4098
|
}
|
|
4303
4099
|
// DELETE / DELETE BY REF ID
|
|
4304
|
-
|
|
4100
|
+
changes.set(changeTree.parentIndex, exports.OPERATION.DELETE);
|
|
4305
4101
|
}
|
|
4306
4102
|
else {
|
|
4307
4103
|
// delete all "tagged" properties.
|
|
4308
|
-
metadata[-2].forEach(
|
|
4309
|
-
return changes.set(index, exports.OPERATION.DELETE);
|
|
4310
|
-
});
|
|
4104
|
+
metadata[-2].forEach((index) => changes.set(index, exports.OPERATION.DELETE));
|
|
4311
4105
|
}
|
|
4312
4106
|
}
|
|
4313
4107
|
else {
|
|
4314
4108
|
// delete only tagged properties
|
|
4315
|
-
metadata[-3][tag].forEach(
|
|
4316
|
-
return changes.set(index, exports.OPERATION.DELETE);
|
|
4317
|
-
});
|
|
4109
|
+
metadata[-3][tag].forEach((index) => changes.set(index, exports.OPERATION.DELETE));
|
|
4318
4110
|
}
|
|
4319
4111
|
// remove tag
|
|
4320
4112
|
if (this.tags && this.tags.has(changeTree)) {
|
|
4321
|
-
|
|
4113
|
+
const tags = this.tags.get(changeTree);
|
|
4322
4114
|
if (tag === undefined) {
|
|
4323
4115
|
// delete all tags
|
|
4324
4116
|
this.tags.delete(changeTree);
|
|
@@ -4333,9 +4125,8 @@ var StateView = /** @class */ (function () {
|
|
|
4333
4125
|
}
|
|
4334
4126
|
}
|
|
4335
4127
|
return this;
|
|
4336
|
-
}
|
|
4337
|
-
|
|
4338
|
-
}());
|
|
4128
|
+
}
|
|
4129
|
+
}
|
|
4339
4130
|
|
|
4340
4131
|
registerType("map", { constructor: MapSchema });
|
|
4341
4132
|
registerType("array", { constructor: ArraySchema });
|