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