@colyseus/schema 1.0.34 → 1.0.38

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.
Files changed (76) hide show
  1. package/README.md +0 -4
  2. package/build/cjs/index.js +35 -33
  3. package/build/cjs/index.js.map +1 -1
  4. package/build/esm/index.mjs +113 -83
  5. package/build/esm/index.mjs.map +1 -1
  6. package/build/umd/index.js +37 -35
  7. package/lib/Reflection.js +11 -11
  8. package/lib/Reflection.js.map +1 -1
  9. package/lib/Schema.js +15 -13
  10. package/lib/Schema.js.map +1 -1
  11. package/lib/annotations.js +17 -13
  12. package/lib/annotations.js.map +1 -1
  13. package/lib/changes/ChangeTree.js +2 -2
  14. package/lib/changes/ChangeTree.js.map +1 -1
  15. package/lib/codegen/api.js +1 -1
  16. package/lib/codegen/api.js.map +1 -1
  17. package/lib/codegen/argv.d.ts +1 -1
  18. package/lib/codegen/cli.js +5 -5
  19. package/lib/codegen/cli.js.map +1 -1
  20. package/lib/codegen/languages/cpp.js +38 -38
  21. package/lib/codegen/languages/cpp.js.map +1 -1
  22. package/lib/codegen/languages/csharp.js +25 -21
  23. package/lib/codegen/languages/csharp.js.map +1 -1
  24. package/lib/codegen/languages/haxe.js +13 -13
  25. package/lib/codegen/languages/haxe.js.map +1 -1
  26. package/lib/codegen/languages/java.js +11 -11
  27. package/lib/codegen/languages/java.js.map +1 -1
  28. package/lib/codegen/languages/js.js +16 -16
  29. package/lib/codegen/languages/js.js.map +1 -1
  30. package/lib/codegen/languages/lua.js +12 -12
  31. package/lib/codegen/languages/lua.js.map +1 -1
  32. package/lib/codegen/languages/ts.js +37 -33
  33. package/lib/codegen/languages/ts.js.map +1 -1
  34. package/lib/codegen/parser.js +3 -3
  35. package/lib/codegen/parser.js.map +1 -1
  36. package/lib/codegen/types.js +2 -2
  37. package/lib/codegen/types.js.map +1 -1
  38. package/lib/events/EventEmitter.js +10 -6
  39. package/lib/events/EventEmitter.js.map +1 -1
  40. package/lib/index.js +4 -4
  41. package/lib/index.js.map +1 -1
  42. package/lib/types/ArraySchema.js +12 -8
  43. package/lib/types/ArraySchema.js.map +1 -1
  44. package/lib/types/MapSchema.d.ts +19 -19
  45. package/lib/types/MapSchema.js +2 -2
  46. package/lib/types/MapSchema.js.map +1 -1
  47. package/package.json +2 -1
  48. package/src/Reflection.ts +159 -0
  49. package/src/Schema.ts +1085 -0
  50. package/src/annotations.ts +357 -0
  51. package/src/changes/ChangeTree.ts +373 -0
  52. package/src/codegen/api.ts +46 -0
  53. package/src/codegen/argv.ts +40 -0
  54. package/src/codegen/cli.ts +65 -0
  55. package/src/codegen/languages/cpp.ts +297 -0
  56. package/src/codegen/languages/csharp.ts +119 -0
  57. package/src/codegen/languages/haxe.ts +110 -0
  58. package/src/codegen/languages/java.ts +115 -0
  59. package/src/codegen/languages/js.ts +115 -0
  60. package/src/codegen/languages/lua.ts +125 -0
  61. package/src/codegen/languages/ts.ts +129 -0
  62. package/src/codegen/parser.ts +251 -0
  63. package/src/codegen/types.ts +164 -0
  64. package/src/encoding/decode.ts +278 -0
  65. package/src/encoding/encode.ts +283 -0
  66. package/src/events/EventEmitter.ts +32 -0
  67. package/src/filters/index.ts +23 -0
  68. package/src/index.ts +59 -0
  69. package/src/spec.ts +49 -0
  70. package/src/types/ArraySchema.ts +608 -0
  71. package/src/types/CollectionSchema.ts +188 -0
  72. package/src/types/HelperTypes.ts +34 -0
  73. package/src/types/MapSchema.ts +255 -0
  74. package/src/types/SetSchema.ts +197 -0
  75. package/src/types/index.ts +19 -0
  76. package/src/utils.ts +28 -0
@@ -0,0 +1,608 @@
1
+ import { ChangeTree } from "../changes/ChangeTree";
2
+ import { OPERATION } from "../spec";
3
+ import { SchemaDecoderCallbacks, Schema } from "../Schema";
4
+
5
+ //
6
+ // Notes:
7
+ // -----
8
+ //
9
+ // - The tsconfig.json of @colyseus/schema uses ES2018.
10
+ // - ES2019 introduces `flatMap` / `flat`, which is not currently relevant, and caused other issues.
11
+ //
12
+
13
+ const DEFAULT_SORT = (a: any, b: any) => {
14
+ const A = a.toString();
15
+ const B = b.toString();
16
+ if (A < B) return -1;
17
+ else if (A > B) return 1;
18
+ else return 0
19
+ }
20
+
21
+ export function getArrayProxy(value: ArraySchema) {
22
+ value['$proxy'] = true;
23
+
24
+ //
25
+ // compatibility with @colyseus/schema 0.5.x
26
+ // - allow `map["key"]`
27
+ // - allow `map["key"] = "xxx"`
28
+ // - allow `delete map["key"]`
29
+ //
30
+ value = new Proxy(value, {
31
+ get: (obj, prop) => {
32
+ if (
33
+ typeof (prop) !== "symbol" &&
34
+ !isNaN(prop as any) // https://stackoverflow.com/a/175787/892698
35
+ ) {
36
+ return obj.at(prop as unknown as number);
37
+
38
+ } else {
39
+ return obj[prop];
40
+ }
41
+ },
42
+
43
+ set: (obj, prop, setValue) => {
44
+ if (
45
+ typeof (prop) !== "symbol" &&
46
+ !isNaN(prop as any)
47
+ ) {
48
+ const indexes = Array.from(obj['$items'].keys());
49
+ const key = parseInt(indexes[prop] || prop);
50
+ if (setValue === undefined || setValue === null) {
51
+ obj.deleteAt(key);
52
+
53
+ } else {
54
+ obj.setAt(key, setValue);
55
+ }
56
+
57
+ } else {
58
+ obj[prop] = setValue;
59
+ }
60
+
61
+ return true;
62
+ },
63
+
64
+ deleteProperty: (obj, prop) => {
65
+ if (typeof (prop) === "number") {
66
+ obj.deleteAt(prop);
67
+
68
+ } else {
69
+ delete obj[prop];
70
+ }
71
+
72
+ return true;
73
+ },
74
+ });
75
+
76
+ return value;
77
+ }
78
+
79
+ export class ArraySchema<V=any> implements Array<V>, SchemaDecoderCallbacks {
80
+ protected $changes: ChangeTree = new ChangeTree(this);
81
+
82
+ protected $items: Map<number, V> = new Map<number, V>();
83
+ protected $indexes: Map<number, number> = new Map<number, number>();
84
+
85
+ protected $refId: number = 0;
86
+
87
+ [n: number]: V;
88
+
89
+ //
90
+ // Decoding callbacks
91
+ //
92
+ public onAdd?: (item: V, key: number) => void;
93
+ public onRemove?: (item: V, key: number) => void;
94
+ public onChange?: (item: V, key: number) => void;
95
+
96
+ static is(type: any) {
97
+ return (
98
+ // type format: ["string"]
99
+ Array.isArray(type) ||
100
+
101
+ // type format: { array: "string" }
102
+ (type['array'] !== undefined)
103
+ );
104
+ }
105
+
106
+ constructor (...items: V[]) {
107
+ this.push.apply(this, items);
108
+ }
109
+
110
+ set length (value: number) {
111
+ if (value === 0) {
112
+ this.clear();
113
+
114
+ } else {
115
+ this.splice(value, this.length - value);
116
+ }
117
+ }
118
+
119
+ get length() {
120
+ return this.$items.size;
121
+ }
122
+
123
+ push(...values: V[]) {
124
+ let lastIndex: number;
125
+
126
+ values.forEach(value => {
127
+ // set "index" for reference.
128
+ lastIndex = this.$refId++;
129
+
130
+ this.setAt(lastIndex, value);
131
+ });
132
+
133
+ return lastIndex;
134
+ }
135
+
136
+ /**
137
+ * Removes the last element from an array and returns it.
138
+ */
139
+ pop(): V | undefined {
140
+ const key = Array.from(this.$indexes.values()).pop();
141
+ if (key === undefined) { return undefined; }
142
+
143
+ this.$changes.delete(key);
144
+ this.$indexes.delete(key);
145
+
146
+ const value = this.$items.get(key);
147
+ this.$items.delete(key);
148
+
149
+ return value;
150
+ }
151
+
152
+ at(index: number) {
153
+ //
154
+ // FIXME: this should be O(1)
155
+ //
156
+ const key = Array.from(this.$items.keys())[index];
157
+ return this.$items.get(key);
158
+ }
159
+
160
+ setAt(index: number, value: V) {
161
+ if (value['$changes'] !== undefined) {
162
+ (value['$changes'] as ChangeTree).setParent(this, this.$changes.root, index);
163
+ }
164
+
165
+ const operation = this.$changes.indexes[index]?.op ?? OPERATION.ADD;
166
+
167
+ this.$changes.indexes[index] = index;
168
+
169
+ this.$indexes.set(index, index);
170
+ this.$items.set(index, value);
171
+
172
+ this.$changes.change(index, operation);
173
+ }
174
+
175
+ deleteAt(index: number) {
176
+ const key = Array.from(this.$items.keys())[index];
177
+ if (key === undefined) { return false; }
178
+ return this.$deleteAt(key);
179
+ }
180
+
181
+ protected $deleteAt(index) {
182
+ // delete at internal index
183
+ this.$changes.delete(index);
184
+ this.$indexes.delete(index);
185
+
186
+ return this.$items.delete(index);
187
+ }
188
+
189
+ clear(isDecoding?: boolean) {
190
+ // discard previous operations.
191
+ this.$changes.discard(true, true);
192
+ this.$changes.indexes = {};
193
+
194
+ // clear previous indexes
195
+ this.$indexes.clear();
196
+
197
+ // flag child items for garbage collection.
198
+ if (isDecoding && typeof (this.$changes.getType()) !== "string") {
199
+ this.$items.forEach((item: V) => {
200
+ this.$changes.root.removeRef(item['$changes'].refId);
201
+ });
202
+ }
203
+
204
+ // clear items
205
+ this.$items.clear();
206
+
207
+ this.$changes.operation({ index: 0, op: OPERATION.CLEAR });
208
+
209
+ // touch all structures until reach root
210
+ this.$changes.touchParents();
211
+ }
212
+
213
+ /**
214
+ * Combines two or more arrays.
215
+ * @param items Additional items to add to the end of array1.
216
+ */
217
+ concat(...items: (V | ConcatArray<V>)[]): ArraySchema<V> {
218
+ return new ArraySchema(...Array.from(this.$items.values()).concat(...items));
219
+ }
220
+
221
+ /**
222
+ * Adds all the elements of an array separated by the specified separator string.
223
+ * @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.
224
+ */
225
+ join(separator?: string): string {
226
+ return Array.from(this.$items.values()).join(separator);
227
+ }
228
+
229
+ /**
230
+ * Reverses the elements in an Array.
231
+ */
232
+ reverse(): ArraySchema<V> {
233
+ const indexes = Array.from(this.$items.keys());
234
+ const reversedItems = Array.from(this.$items.values()).reverse();
235
+
236
+ reversedItems.forEach((item, i) => {
237
+ this.setAt(indexes[i], item);
238
+ });
239
+
240
+ return this;
241
+ }
242
+
243
+ /**
244
+ * Removes the first element from an array and returns it.
245
+ */
246
+ shift(): V | undefined {
247
+ const indexes = Array.from(this.$items.keys());
248
+
249
+ const shiftAt = indexes.shift();
250
+ if (shiftAt === undefined) { return undefined; }
251
+
252
+ const value = this.$items.get(shiftAt);
253
+ this.$deleteAt(shiftAt);
254
+
255
+ return value;
256
+ }
257
+
258
+ /**
259
+ * Returns a section of an array.
260
+ * @param start The beginning of the specified portion of the array.
261
+ * @param end The end of the specified portion of the array. This is exclusive of the element at the index 'end'.
262
+ */
263
+ slice(start?: number, end?: number): V[] {
264
+ return new ArraySchema(...Array.from(this.$items.values()).slice(start, end));
265
+ }
266
+
267
+ /**
268
+ * Sorts an array.
269
+ * @param compareFn Function used to determine the order of the elements. It is expected to return
270
+ * a negative value if first argument is less than second argument, zero if they're equal and a positive
271
+ * value otherwise. If omitted, the elements are sorted in ascending, ASCII character order.
272
+ * ```ts
273
+ * [11,2,22,1].sort((a, b) => a - b)
274
+ * ```
275
+ */
276
+ sort(compareFn: (a: V, b: V) => number = DEFAULT_SORT): this {
277
+ const indexes = Array.from(this.$items.keys());
278
+ const sortedItems = Array.from(this.$items.values()).sort(compareFn);
279
+
280
+ sortedItems.forEach((item, i) => {
281
+ this.setAt(indexes[i], item);
282
+ });
283
+
284
+ return this;
285
+ }
286
+
287
+ /**
288
+ * Removes elements from an array and, if necessary, inserts new elements in their place, returning the deleted elements.
289
+ * @param start The zero-based location in the array from which to start removing elements.
290
+ * @param deleteCount The number of elements to remove.
291
+ * @param items Elements to insert into the array in place of the deleted elements.
292
+ */
293
+ splice(
294
+ start: number,
295
+ deleteCount: number = this.length - start,
296
+ ...items: V[]
297
+ ): V[] {
298
+ const indexes = Array.from(this.$items.keys());
299
+ const removedItems: V[] = [];
300
+
301
+ for (let i = start; i < start + deleteCount; i++) {
302
+ removedItems.push(this.$items.get(indexes[i]));
303
+ this.$deleteAt(indexes[i]);
304
+ }
305
+
306
+ return removedItems;
307
+ }
308
+
309
+ /**
310
+ * Inserts new elements at the start of an array.
311
+ * @param items Elements to insert at the start of the Array.
312
+ */
313
+ unshift(...items: V[]): number {
314
+ const length = this.length;
315
+ const addedLength = items.length;
316
+
317
+ // const indexes = Array.from(this.$items.keys());
318
+ const previousValues = Array.from(this.$items.values());
319
+
320
+ items.forEach((item, i) => {
321
+ this.setAt(i, item);
322
+ });
323
+
324
+ previousValues.forEach((previousValue, i) => {
325
+ this.setAt(addedLength + i, previousValue);
326
+ });
327
+
328
+ return length + addedLength;
329
+ }
330
+
331
+ /**
332
+ * Returns the index of the first occurrence of a value in an array.
333
+ * @param searchElement The value to locate in the array.
334
+ * @param fromIndex The array index at which to begin the search. If fromIndex is omitted, the search starts at index 0.
335
+ */
336
+ indexOf(searchElement: V, fromIndex?: number): number {
337
+ return Array.from(this.$items.values()).indexOf(searchElement, fromIndex);
338
+ }
339
+
340
+ /**
341
+ * Returns the index of the last occurrence of a specified value in an array.
342
+ * @param searchElement The value to locate in the array.
343
+ * @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.
344
+ */
345
+ lastIndexOf(searchElement: V, fromIndex: number = this.length - 1): number {
346
+ return Array.from(this.$items.values()).lastIndexOf(searchElement, fromIndex);
347
+ }
348
+
349
+ /**
350
+ * Determines whether all the members of an array satisfy the specified test.
351
+ * @param callbackfn A function that accepts up to three arguments. The every method calls
352
+ * the callbackfn function for each element in the array until the callbackfn returns a value
353
+ * which is coercible to the Boolean value false, or until the end of the array.
354
+ * @param thisArg An object to which the this keyword can refer in the callbackfn function.
355
+ * If thisArg is omitted, undefined is used as the this value.
356
+ */
357
+ every(callbackfn: (value: V, index: number, array: V[]) => unknown, thisArg?: any): boolean {
358
+ return Array.from(this.$items.values()).every(callbackfn, thisArg);
359
+ }
360
+
361
+ /**
362
+ * Determines whether the specified callback function returns true for any element of an array.
363
+ * @param callbackfn A function that accepts up to three arguments. The some method calls
364
+ * the callbackfn function for each element in the array until the callbackfn returns a value
365
+ * which is coercible to the Boolean value true, or until the end of the array.
366
+ * @param thisArg An object to which the this keyword can refer in the callbackfn function.
367
+ * If thisArg is omitted, undefined is used as the this value.
368
+ */
369
+ some(callbackfn: (value: V, index: number, array: V[]) => unknown, thisArg?: any): boolean {
370
+ return Array.from(this.$items.values()).some(callbackfn, thisArg);
371
+ }
372
+
373
+ /**
374
+ * Performs the specified action for each element in an array.
375
+ * @param callbackfn A function that accepts up to three arguments. forEach calls the callbackfn function one time for each element in the array.
376
+ * @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.
377
+ */
378
+ forEach(callbackfn: (value: V, index: number, array: V[]) => void, thisArg?: any): void {
379
+ Array.from(this.$items.values()).forEach(callbackfn, thisArg);
380
+ }
381
+
382
+ /**
383
+ * Calls a defined callback function on each element of an array, and returns an array that contains the results.
384
+ * @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.
385
+ * @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.
386
+ */
387
+ map<U>(callbackfn: (value: V, index: number, array: V[]) => U, thisArg?: any): U[] {
388
+ return Array.from(this.$items.values()).map(callbackfn, thisArg);
389
+ }
390
+
391
+ /**
392
+ * Returns the elements of an array that meet the condition specified in a callback function.
393
+ * @param callbackfn A function that accepts up to three arguments. The filter method calls the callbackfn function one time for each element in the array.
394
+ * @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.
395
+ */
396
+ filter(callbackfn: (value: V, index: number, array: V[]) => unknown, thisArg?: any)
397
+ filter<S extends V>(callbackfn: (value: V, index: number, array: V[]) => value is S, thisArg?: any): V[] {
398
+ return Array.from(this.$items.values()).filter(callbackfn, thisArg);
399
+ }
400
+
401
+ /**
402
+ * 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.
403
+ * @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.
404
+ * @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.
405
+ */
406
+ reduce<U=V>(callbackfn: (previousValue: U, currentValue: V, currentIndex: number, array: V[]) => U, initialValue?: U): U {
407
+ return Array.prototype.reduce.apply(Array.from(this.$items.values()), arguments);
408
+ }
409
+
410
+ /**
411
+ * 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.
412
+ * @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.
413
+ * @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.
414
+ */
415
+ reduceRight<U=V>(callbackfn: (previousValue: U, currentValue: V, currentIndex: number, array: V[]) => U, initialValue?: U): U {
416
+ return Array.prototype.reduceRight.apply(Array.from(this.$items.values()), arguments);
417
+ }
418
+
419
+ /**
420
+ * Returns the value of the first element in the array where predicate is true, and undefined
421
+ * otherwise.
422
+ * @param predicate find calls predicate once for each element of the array, in ascending
423
+ * order, until it finds one where predicate returns true. If such an element is found, find
424
+ * immediately returns that element value. Otherwise, find returns undefined.
425
+ * @param thisArg If provided, it will be used as the this value for each invocation of
426
+ * predicate. If it is not provided, undefined is used instead.
427
+ */
428
+ find(predicate: (value: V, index: number, obj: V[]) => boolean, thisArg?: any): V | undefined {
429
+ return Array.from(this.$items.values()).find(predicate, thisArg);
430
+ }
431
+
432
+ /**
433
+ * Returns the index of the first element in the array where predicate is true, and -1
434
+ * otherwise.
435
+ * @param predicate find calls predicate once for each element of the array, in ascending
436
+ * order, until it finds one where predicate returns true. If such an element is found,
437
+ * findIndex immediately returns that element index. Otherwise, findIndex returns -1.
438
+ * @param thisArg If provided, it will be used as the this value for each invocation of
439
+ * predicate. If it is not provided, undefined is used instead.
440
+ */
441
+ findIndex(predicate: (value: V, index: number, obj: V[]) => unknown, thisArg?: any): number {
442
+ return Array.from(this.$items.values()).findIndex(predicate, thisArg);
443
+ }
444
+
445
+ /**
446
+ * Returns the this object after filling the section identified by start and end with value
447
+ * @param value value to fill array section with
448
+ * @param start index to start filling the array at. If start is negative, it is treated as
449
+ * length+start where length is the length of the array.
450
+ * @param end index to stop filling the array at. If end is negative, it is treated as
451
+ * length+end.
452
+ */
453
+ fill(value: V, start?: number, end?: number): this {
454
+ //
455
+ // TODO
456
+ //
457
+ throw new Error("ArraySchema#fill() not implemented");
458
+ // this.$items.fill(value, start, end);
459
+
460
+ return this;
461
+ }
462
+
463
+ /**
464
+ * Returns the this object after copying a section of the array identified by start and end
465
+ * to the same array starting at position target
466
+ * @param target If target is negative, it is treated as length+target where length is the
467
+ * length of the array.
468
+ * @param start If start is negative, it is treated as length+start. If end is negative, it
469
+ * is treated as length+end.
470
+ * @param end If not specified, length of the this object is used as its default value.
471
+ */
472
+ copyWithin(target: number, start: number, end?: number): this {
473
+ //
474
+ // TODO
475
+ //
476
+ throw new Error("ArraySchema#copyWithin() not implemented");
477
+ return this;
478
+ }
479
+
480
+ /**
481
+ * Returns a string representation of an array.
482
+ */
483
+ toString(): string { return this.$items.toString(); }
484
+
485
+ /**
486
+ * Returns a string representation of an array. The elements are converted to string using their toLocalString methods.
487
+ */
488
+ toLocaleString(): string { return this.$items.toLocaleString() };
489
+
490
+ /** Iterator */
491
+ [Symbol.iterator](): IterableIterator<V> {
492
+ return Array.from(this.$items.values())[Symbol.iterator]();
493
+ }
494
+
495
+ [Symbol.unscopables]() {
496
+ return this.$items[Symbol.unscopables]();
497
+ }
498
+
499
+ /**
500
+ * Returns an iterable of key, value pairs for every entry in the array
501
+ */
502
+ entries(): IterableIterator<[number, V]> { return this.$items.entries(); }
503
+
504
+ /**
505
+ * Returns an iterable of keys in the array
506
+ */
507
+ keys(): IterableIterator<number> { return this.$items.keys(); }
508
+
509
+ /**
510
+ * Returns an iterable of values in the array
511
+ */
512
+ values(): IterableIterator<V> { return this.$items.values(); }
513
+
514
+ /**
515
+ * Determines whether an array includes a certain element, returning true or false as appropriate.
516
+ * @param searchElement The element to search for.
517
+ * @param fromIndex The position in this array at which to begin searching for searchElement.
518
+ */
519
+ includes(searchElement: V, fromIndex?: number): boolean {
520
+ return Array.from(this.$items.values()).includes(searchElement, fromIndex);
521
+ }
522
+
523
+ /**
524
+ * Calls a defined callback function on each element of an array. Then, flattens the result into
525
+ * a new array.
526
+ * This is identical to a map followed by flat with depth 1.
527
+ *
528
+ * @param callback A function that accepts up to three arguments. The flatMap method calls the
529
+ * callback function one time for each element in the array.
530
+ * @param thisArg An object to which the this keyword can refer in the callback function. If
531
+ * thisArg is omitted, undefined is used as the this value.
532
+ */
533
+ // @ts-ignore
534
+ flatMap<U, This = undefined>(callback: (this: This, value: V, index: number, array: V[]) => U | ReadonlyArray<U>, thisArg?: This): U[] {
535
+ // @ts-ignore
536
+ throw new Error("ArraySchema#flatMap() is not supported.");
537
+ }
538
+
539
+ /**
540
+ * Returns a new array with all sub-array elements concatenated into it recursively up to the
541
+ * specified depth.
542
+ *
543
+ * @param depth The maximum recursion depth
544
+ */
545
+ // @ts-ignore
546
+ flat<A, D extends number = 1>(this: A, depth?: D): any {
547
+ // @ts-ignore
548
+ throw new Error("ArraySchema#flat() is not supported.");
549
+ }
550
+
551
+ // get size () {
552
+ // return this.$items.size;
553
+ // }
554
+
555
+ protected setIndex(index: number, key: number) {
556
+ this.$indexes.set(index, key);
557
+ }
558
+
559
+ protected getIndex(index: number) {
560
+ return this.$indexes.get(index);
561
+ }
562
+
563
+ protected getByIndex(index: number) {
564
+ return this.$items.get(this.$indexes.get(index));
565
+ }
566
+
567
+ protected deleteByIndex(index: number) {
568
+ const key = this.$indexes.get(index);
569
+ this.$items.delete(key);
570
+ this.$indexes.delete(index);
571
+ }
572
+
573
+ toArray() {
574
+ return Array.from(this.$items.values());
575
+ }
576
+
577
+ toJSON() {
578
+ return this.toArray().map((value) => {
579
+ return (typeof (value['toJSON']) === "function")
580
+ ? value['toJSON']()
581
+ : value;
582
+ });
583
+ }
584
+
585
+ //
586
+ // Decoding utilities
587
+ //
588
+ clone(isDecoding?: boolean): ArraySchema<V> {
589
+ let cloned: ArraySchema;
590
+
591
+ if (isDecoding) {
592
+ cloned = new ArraySchema(...Array.from(this.$items.values()));
593
+
594
+ } else {
595
+ cloned = new ArraySchema(...this.map(item => (
596
+ (item['$changes'])
597
+ ? (item as any as Schema).clone()
598
+ : item
599
+ )));
600
+ }
601
+
602
+ return cloned;
603
+ };
604
+
605
+ triggerAll (): void {
606
+ Schema.prototype.triggerAll.apply(this);
607
+ }
608
+ }