@colyseus/schema 4.0.1 → 4.0.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.
Files changed (239) hide show
  1. package/build/cjs/{index.js → index.cjs} +1 -1
  2. package/build/cjs/index.cjs.map +1 -0
  3. package/build/esm/index.mjs.map +1 -1
  4. package/lib/Metadata.js +54 -58
  5. package/lib/Metadata.js.map +1 -1
  6. package/lib/Reflection.js +29 -32
  7. package/lib/Reflection.js.map +1 -1
  8. package/lib/Schema.js +41 -45
  9. package/lib/Schema.js.map +1 -1
  10. package/lib/annotations.d.ts +1 -1
  11. package/lib/annotations.js +58 -69
  12. package/lib/annotations.js.map +1 -1
  13. package/lib/bench_encode.js +24 -59
  14. package/lib/bench_encode.js.map +1 -1
  15. package/lib/benchmark.d.ts +1 -0
  16. package/lib/benchmark.js +218 -0
  17. package/lib/benchmark.js.map +1 -0
  18. package/lib/codegen/api.js +10 -41
  19. package/lib/codegen/api.js.map +1 -1
  20. package/lib/codegen/argv.js +1 -3
  21. package/lib/codegen/argv.js.map +1 -1
  22. package/lib/codegen/cli.js +4 -9
  23. package/lib/codegen/cli.js.map +1 -1
  24. package/lib/codegen/languages/cpp.js +5 -8
  25. package/lib/codegen/languages/cpp.js.map +1 -1
  26. package/lib/codegen/languages/csharp.js +5 -8
  27. package/lib/codegen/languages/csharp.js.map +1 -1
  28. package/lib/codegen/languages/haxe.js +3 -6
  29. package/lib/codegen/languages/haxe.js.map +1 -1
  30. package/lib/codegen/languages/java.js +3 -6
  31. package/lib/codegen/languages/java.js.map +1 -1
  32. package/lib/codegen/languages/js.js +4 -7
  33. package/lib/codegen/languages/js.js.map +1 -1
  34. package/lib/codegen/languages/lua.js +4 -7
  35. package/lib/codegen/languages/lua.js.map +1 -1
  36. package/lib/codegen/languages/ts.js +5 -8
  37. package/lib/codegen/languages/ts.js.map +1 -1
  38. package/lib/codegen/parser.js +22 -59
  39. package/lib/codegen/parser.js.map +1 -1
  40. package/lib/codegen/types.js +12 -51
  41. package/lib/codegen/types.js.map +1 -1
  42. package/lib/decoder/DecodeOperation.js +44 -51
  43. package/lib/decoder/DecodeOperation.js.map +1 -1
  44. package/lib/decoder/Decoder.js +24 -28
  45. package/lib/decoder/Decoder.js.map +1 -1
  46. package/lib/decoder/ReferenceTracker.js +10 -14
  47. package/lib/decoder/ReferenceTracker.js.map +1 -1
  48. package/lib/decoder/strategy/Callbacks.js +39 -43
  49. package/lib/decoder/strategy/Callbacks.js.map +1 -1
  50. package/lib/decoder/strategy/RawChanges.js +1 -4
  51. package/lib/decoder/strategy/RawChanges.js.map +1 -1
  52. package/lib/decoder/strategy/getDecoderStateCallbacks.js +24 -27
  53. package/lib/decoder/strategy/getDecoderStateCallbacks.js.map +1 -1
  54. package/lib/encoder/ChangeTree.js +36 -44
  55. package/lib/encoder/ChangeTree.js.map +1 -1
  56. package/lib/encoder/EncodeOperation.js +25 -32
  57. package/lib/encoder/EncodeOperation.js.map +1 -1
  58. package/lib/encoder/Encoder.js +25 -29
  59. package/lib/encoder/Encoder.js.map +1 -1
  60. package/lib/encoder/Root.js +17 -21
  61. package/lib/encoder/Root.js.map +1 -1
  62. package/lib/encoder/StateView.js +40 -45
  63. package/lib/encoder/StateView.js.map +1 -1
  64. package/lib/encoding/assert.js +3 -9
  65. package/lib/encoding/assert.js.map +1 -1
  66. package/lib/encoding/decode.js +2 -6
  67. package/lib/encoding/decode.js.map +1 -1
  68. package/lib/encoding/encode.js +1 -4
  69. package/lib/encoding/encode.js.map +1 -1
  70. package/lib/encoding/spec.js +4 -7
  71. package/lib/encoding/spec.js.map +1 -1
  72. package/lib/index.d.ts +2 -2
  73. package/lib/index.js +33 -75
  74. package/lib/index.js.map +1 -1
  75. package/lib/src/Metadata.d.ts +49 -0
  76. package/lib/src/Metadata.js +256 -0
  77. package/lib/src/Metadata.js.map +1 -0
  78. package/lib/src/Reflection.d.ts +71 -0
  79. package/lib/src/Reflection.js +179 -0
  80. package/lib/src/Reflection.js.map +1 -0
  81. package/lib/src/Schema.d.ts +86 -0
  82. package/lib/src/Schema.js +352 -0
  83. package/lib/src/Schema.js.map +1 -0
  84. package/lib/src/annotations.d.ts +109 -0
  85. package/lib/src/annotations.js +473 -0
  86. package/lib/src/annotations.js.map +1 -0
  87. package/lib/src/bench_encode.d.ts +1 -0
  88. package/lib/src/bench_encode.js +91 -0
  89. package/lib/src/bench_encode.js.map +1 -0
  90. package/lib/src/codegen/api.d.ts +7 -0
  91. package/lib/src/codegen/api.js +56 -0
  92. package/lib/src/codegen/api.js.map +1 -0
  93. package/lib/src/codegen/argv.d.ts +6 -0
  94. package/lib/src/codegen/argv.js +39 -0
  95. package/lib/src/codegen/argv.js.map +1 -0
  96. package/lib/src/codegen/cli.d.ts +1 -0
  97. package/lib/src/codegen/cli.js +58 -0
  98. package/lib/src/codegen/cli.js.map +1 -0
  99. package/lib/src/codegen/languages/cpp.d.ts +3 -0
  100. package/lib/src/codegen/languages/cpp.js +258 -0
  101. package/lib/src/codegen/languages/cpp.js.map +1 -0
  102. package/lib/src/codegen/languages/csharp.d.ts +4 -0
  103. package/lib/src/codegen/languages/csharp.js +154 -0
  104. package/lib/src/codegen/languages/csharp.js.map +1 -0
  105. package/lib/src/codegen/languages/haxe.d.ts +3 -0
  106. package/lib/src/codegen/languages/haxe.js +100 -0
  107. package/lib/src/codegen/languages/haxe.js.map +1 -0
  108. package/lib/src/codegen/languages/java.d.ts +6 -0
  109. package/lib/src/codegen/languages/java.js +100 -0
  110. package/lib/src/codegen/languages/java.js.map +1 -0
  111. package/lib/src/codegen/languages/js.d.ts +3 -0
  112. package/lib/src/codegen/languages/js.js +101 -0
  113. package/lib/src/codegen/languages/js.js.map +1 -0
  114. package/lib/src/codegen/languages/lua.d.ts +3 -0
  115. package/lib/src/codegen/languages/lua.js +103 -0
  116. package/lib/src/codegen/languages/lua.js.map +1 -0
  117. package/lib/src/codegen/languages/ts.d.ts +3 -0
  118. package/lib/src/codegen/languages/ts.js +116 -0
  119. package/lib/src/codegen/languages/ts.js.map +1 -0
  120. package/lib/src/codegen/parser.d.ts +13 -0
  121. package/lib/src/codegen/parser.js +327 -0
  122. package/lib/src/codegen/parser.js.map +1 -0
  123. package/lib/src/codegen/types.d.ts +52 -0
  124. package/lib/src/codegen/types.js +142 -0
  125. package/lib/src/codegen/types.js.map +1 -0
  126. package/lib/src/decoder/DecodeOperation.d.ts +23 -0
  127. package/lib/src/decoder/DecodeOperation.js +248 -0
  128. package/lib/src/decoder/DecodeOperation.js.map +1 -0
  129. package/lib/src/decoder/Decoder.d.ts +21 -0
  130. package/lib/src/decoder/Decoder.js +114 -0
  131. package/lib/src/decoder/Decoder.js.map +1 -0
  132. package/lib/src/decoder/ReferenceTracker.d.ts +25 -0
  133. package/lib/src/decoder/ReferenceTracker.js +135 -0
  134. package/lib/src/decoder/ReferenceTracker.js.map +1 -0
  135. package/lib/src/decoder/strategy/Callbacks.d.ts +154 -0
  136. package/lib/src/decoder/strategy/Callbacks.js +336 -0
  137. package/lib/src/decoder/strategy/Callbacks.js.map +1 -0
  138. package/lib/src/decoder/strategy/RawChanges.d.ts +3 -0
  139. package/lib/src/decoder/strategy/RawChanges.js +4 -0
  140. package/lib/src/decoder/strategy/RawChanges.js.map +1 -0
  141. package/lib/src/decoder/strategy/getDecoderStateCallbacks.d.ts +76 -0
  142. package/lib/src/decoder/strategy/getDecoderStateCallbacks.js +274 -0
  143. package/lib/src/decoder/strategy/getDecoderStateCallbacks.js.map +1 -0
  144. package/lib/src/encoder/ChangeTree.d.ts +135 -0
  145. package/lib/src/encoder/ChangeTree.js +534 -0
  146. package/lib/src/encoder/ChangeTree.js.map +1 -0
  147. package/lib/src/encoder/EncodeOperation.d.ts +22 -0
  148. package/lib/src/encoder/EncodeOperation.js +132 -0
  149. package/lib/src/encoder/EncodeOperation.js.map +1 -0
  150. package/lib/src/encoder/Encoder.d.ts +22 -0
  151. package/lib/src/encoder/Encoder.js +204 -0
  152. package/lib/src/encoder/Encoder.js.map +1 -0
  153. package/lib/src/encoder/Root.d.ts +28 -0
  154. package/lib/src/encoder/Root.js +229 -0
  155. package/lib/src/encoder/Root.js.map +1 -0
  156. package/lib/src/encoder/StateView.d.ts +34 -0
  157. package/lib/src/encoder/StateView.js +279 -0
  158. package/lib/src/encoder/StateView.js.map +1 -0
  159. package/lib/src/encoding/assert.d.ts +10 -0
  160. package/lib/src/encoding/assert.js +49 -0
  161. package/lib/src/encoding/assert.js.map +1 -0
  162. package/lib/src/encoding/decode.d.ts +67 -0
  163. package/lib/src/encoding/decode.js +217 -0
  164. package/lib/src/encoding/decode.js.map +1 -0
  165. package/lib/src/encoding/encode.d.ts +40 -0
  166. package/lib/src/encoding/encode.js +279 -0
  167. package/lib/src/encoding/encode.js.map +1 -0
  168. package/lib/src/encoding/spec.d.ts +24 -0
  169. package/lib/src/encoding/spec.js +26 -0
  170. package/lib/src/encoding/spec.js.map +1 -0
  171. package/lib/src/index.d.ts +32 -0
  172. package/lib/src/index.js +39 -0
  173. package/lib/src/index.js.map +1 -0
  174. package/lib/src/symbol.shim.d.ts +6 -0
  175. package/lib/src/symbol.shim.js +3 -0
  176. package/lib/src/symbol.shim.js.map +1 -0
  177. package/lib/src/types/HelperTypes.d.ts +77 -0
  178. package/lib/src/types/HelperTypes.js +2 -0
  179. package/lib/src/types/HelperTypes.js.map +1 -0
  180. package/lib/src/types/TypeContext.d.ts +31 -0
  181. package/lib/src/types/TypeContext.js +143 -0
  182. package/lib/src/types/TypeContext.js.map +1 -0
  183. package/lib/src/types/custom/ArraySchema.d.ts +270 -0
  184. package/lib/src/types/custom/ArraySchema.js +733 -0
  185. package/lib/src/types/custom/ArraySchema.js.map +1 -0
  186. package/lib/src/types/custom/CollectionSchema.d.ts +51 -0
  187. package/lib/src/types/custom/CollectionSchema.js +170 -0
  188. package/lib/src/types/custom/CollectionSchema.js.map +1 -0
  189. package/lib/src/types/custom/MapSchema.d.ts +51 -0
  190. package/lib/src/types/custom/MapSchema.js +222 -0
  191. package/lib/src/types/custom/MapSchema.js.map +1 -0
  192. package/lib/src/types/custom/SetSchema.d.ts +48 -0
  193. package/lib/src/types/custom/SetSchema.js +178 -0
  194. package/lib/src/types/custom/SetSchema.js.map +1 -0
  195. package/lib/src/types/registry.d.ts +16 -0
  196. package/lib/src/types/registry.js +30 -0
  197. package/lib/src/types/registry.js.map +1 -0
  198. package/lib/src/types/symbols.d.ts +33 -0
  199. package/lib/src/types/symbols.js +34 -0
  200. package/lib/src/types/symbols.js.map +1 -0
  201. package/lib/src/types/utils.d.ts +1 -0
  202. package/lib/src/types/utils.js +13 -0
  203. package/lib/src/types/utils.js.map +1 -0
  204. package/lib/src/utils.d.ts +13 -0
  205. package/lib/src/utils.js +49 -0
  206. package/lib/src/utils.js.map +1 -0
  207. package/lib/src/v3_bench.d.ts +1 -0
  208. package/lib/src/v3_bench.js +128 -0
  209. package/lib/src/v3_bench.js.map +1 -0
  210. package/lib/symbol.shim.js +1 -2
  211. package/lib/symbol.shim.js.map +1 -1
  212. package/lib/types/HelperTypes.js +1 -2
  213. package/lib/types/TypeContext.js +8 -12
  214. package/lib/types/TypeContext.js.map +1 -1
  215. package/lib/types/custom/ArraySchema.js +63 -67
  216. package/lib/types/custom/ArraySchema.js.map +1 -1
  217. package/lib/types/custom/CollectionSchema.js +27 -31
  218. package/lib/types/custom/CollectionSchema.js.map +1 -1
  219. package/lib/types/custom/MapSchema.js +37 -41
  220. package/lib/types/custom/MapSchema.js.map +1 -1
  221. package/lib/types/custom/SetSchema.js +28 -32
  222. package/lib/types/custom/SetSchema.js.map +1 -1
  223. package/lib/types/registry.js +13 -20
  224. package/lib/types/registry.js.map +1 -1
  225. package/lib/types/symbols.js +16 -19
  226. package/lib/types/symbols.js.map +1 -1
  227. package/lib/types/utils.js +1 -4
  228. package/lib/types/utils.js.map +1 -1
  229. package/lib/utils.js +9 -14
  230. package/lib/utils.js.map +1 -1
  231. package/lib/v3_bench.js +17 -19
  232. package/lib/v3_bench.js.map +1 -1
  233. package/package.json +9 -7
  234. package/src/codegen/api.ts +7 -0
  235. package/src/codegen/parser.ts +2 -2
  236. package/src/codegen/types.ts +5 -0
  237. package/src/index.ts +2 -2
  238. package/build/cjs/index.js.map +0 -1
  239. package/src/bench_encode.ts +0 -64
@@ -0,0 +1,733 @@
1
+ var _a, _b;
2
+ import { $changes, $childType, $decoder, $deleteByIndex, $onEncodeEnd, $encoder, $filter, $getByIndex, $onDecodeEnd, $refId } from "../symbols";
3
+ import { ChangeTree, setOperationAtIndex } from "../../encoder/ChangeTree";
4
+ import { OPERATION } from "../../encoding/spec";
5
+ import { registerType } from "../registry";
6
+ import { encodeArray } from "../../encoder/EncodeOperation";
7
+ import { decodeArray } from "../../decoder/DecodeOperation";
8
+ import { assertInstanceType } from "../../encoding/assert";
9
+ const DEFAULT_SORT = (a, b) => {
10
+ const A = a.toString();
11
+ const B = b.toString();
12
+ if (A < B)
13
+ return -1;
14
+ else if (A > B)
15
+ return 1;
16
+ else
17
+ return 0;
18
+ };
19
+ export class ArraySchema {
20
+ static { this[_a] = encodeArray; }
21
+ static { this[_b] = decodeArray; }
22
+ /**
23
+ * Determine if a property must be filtered.
24
+ * - If returns false, the property is NOT going to be encoded.
25
+ * - If returns true, the property is going to be encoded.
26
+ *
27
+ * Encoding with "filters" happens in two steps:
28
+ * - First, the encoder iterates over all "not owned" properties and encodes them.
29
+ * - Then, the encoder iterates over all "owned" properties per instance and encodes them.
30
+ */
31
+ static [(_a = $encoder, _b = $decoder, $filter)](ref, index, view) {
32
+ return (!view ||
33
+ typeof (ref[$childType]) === "string" ||
34
+ view.isChangeTreeVisible(ref['tmpItems'][index]?.[$changes]));
35
+ }
36
+ static is(type) {
37
+ return (
38
+ // type format: ["string"]
39
+ Array.isArray(type) ||
40
+ // type format: { array: "string" }
41
+ (type['array'] !== undefined));
42
+ }
43
+ static from(iterable) {
44
+ return new ArraySchema(...Array.from(iterable));
45
+ }
46
+ constructor(...items) {
47
+ this.items = [];
48
+ this.tmpItems = [];
49
+ this.deletedIndexes = {};
50
+ this.isMovingItems = false;
51
+ Object.defineProperty(this, $childType, {
52
+ value: undefined,
53
+ enumerable: false,
54
+ writable: true,
55
+ configurable: true,
56
+ });
57
+ const proxy = new Proxy(this, {
58
+ get: (obj, prop) => {
59
+ if (typeof (prop) !== "symbol" &&
60
+ // FIXME: d8 accuses this as low performance
61
+ !isNaN(prop) // https://stackoverflow.com/a/175787/892698
62
+ ) {
63
+ return this.items[prop];
64
+ }
65
+ else {
66
+ return Reflect.get(obj, prop);
67
+ }
68
+ },
69
+ set: (obj, key, setValue) => {
70
+ if (typeof (key) !== "symbol" && !isNaN(key)) {
71
+ if (setValue === undefined || setValue === null) {
72
+ obj.$deleteAt(key);
73
+ }
74
+ else {
75
+ if (setValue[$changes]) {
76
+ assertInstanceType(setValue, obj[$childType], obj, key);
77
+ const previousValue = obj.items[key];
78
+ if (!obj.isMovingItems) {
79
+ obj.$changeAt(Number(key), setValue);
80
+ }
81
+ else {
82
+ if (previousValue !== undefined) {
83
+ if (setValue[$changes].isNew) {
84
+ obj[$changes].indexedOperation(Number(key), OPERATION.MOVE_AND_ADD);
85
+ }
86
+ else {
87
+ if ((obj[$changes].getChange(Number(key)) & OPERATION.DELETE) === OPERATION.DELETE) {
88
+ obj[$changes].indexedOperation(Number(key), OPERATION.DELETE_AND_MOVE);
89
+ }
90
+ else {
91
+ obj[$changes].indexedOperation(Number(key), OPERATION.MOVE);
92
+ }
93
+ }
94
+ }
95
+ else if (setValue[$changes].isNew) {
96
+ obj[$changes].indexedOperation(Number(key), OPERATION.ADD);
97
+ }
98
+ setValue[$changes].setParent(this, obj[$changes].root, key);
99
+ }
100
+ if (previousValue !== undefined) {
101
+ // remove root reference from previous value
102
+ previousValue[$changes].root?.remove(previousValue[$changes]);
103
+ }
104
+ }
105
+ else {
106
+ obj.$changeAt(Number(key), setValue);
107
+ }
108
+ obj.items[key] = setValue;
109
+ obj.tmpItems[key] = setValue;
110
+ }
111
+ return true;
112
+ }
113
+ else {
114
+ return Reflect.set(obj, key, setValue);
115
+ }
116
+ },
117
+ deleteProperty: (obj, prop) => {
118
+ if (typeof (prop) === "number") {
119
+ obj.$deleteAt(prop);
120
+ }
121
+ else {
122
+ delete obj[prop];
123
+ }
124
+ return true;
125
+ },
126
+ has: (obj, key) => {
127
+ if (typeof (key) !== "symbol" && !isNaN(Number(key))) {
128
+ return Reflect.has(this.items, key);
129
+ }
130
+ return Reflect.has(obj, key);
131
+ }
132
+ });
133
+ Object.defineProperty(this, $changes, {
134
+ value: new ChangeTree(proxy),
135
+ enumerable: false,
136
+ writable: true,
137
+ });
138
+ if (items.length > 0) {
139
+ this.push(...items);
140
+ }
141
+ return proxy;
142
+ }
143
+ set length(newLength) {
144
+ if (newLength === 0) {
145
+ this.clear();
146
+ }
147
+ else if (newLength < this.items.length) {
148
+ this.splice(newLength, this.length - newLength);
149
+ }
150
+ else {
151
+ console.warn("ArraySchema: can't set .length to a higher value than its length.");
152
+ }
153
+ }
154
+ get length() {
155
+ return this.items.length;
156
+ }
157
+ push(...values) {
158
+ let length = this.tmpItems.length;
159
+ const changeTree = this[$changes];
160
+ for (let i = 0, l = values.length; i < l; i++, length++) {
161
+ const value = values[i];
162
+ if (value === undefined || value === null) {
163
+ // skip null values
164
+ return;
165
+ }
166
+ else if (typeof (value) === "object" && this[$childType]) {
167
+ assertInstanceType(value, this[$childType], this, i);
168
+ // TODO: move value[$changes]?.setParent() to this block.
169
+ }
170
+ changeTree.indexedOperation(length, OPERATION.ADD, this.items.length);
171
+ this.items.push(value);
172
+ this.tmpItems.push(value);
173
+ //
174
+ // set value's parent after the value is set
175
+ // (to avoid encoding "refId" operations before parent's "ADD" operation)
176
+ //
177
+ value[$changes]?.setParent(this, changeTree.root, length);
178
+ }
179
+ return length;
180
+ }
181
+ /**
182
+ * Removes the last element from an array and returns it.
183
+ */
184
+ pop() {
185
+ let index = -1;
186
+ // find last non-undefined index
187
+ for (let i = this.tmpItems.length - 1; i >= 0; i--) {
188
+ // if (this.tmpItems[i] !== undefined) {
189
+ if (this.deletedIndexes[i] !== true) {
190
+ index = i;
191
+ break;
192
+ }
193
+ }
194
+ if (index < 0) {
195
+ return undefined;
196
+ }
197
+ this[$changes].delete(index, undefined, this.items.length - 1);
198
+ this.deletedIndexes[index] = true;
199
+ return this.items.pop();
200
+ }
201
+ at(index) {
202
+ // Allow negative indexing from the end
203
+ if (index < 0)
204
+ index += this.length;
205
+ return this.items[index];
206
+ }
207
+ // encoding only
208
+ $changeAt(index, value) {
209
+ if (value === undefined || value === null) {
210
+ console.error("ArraySchema items cannot be null nor undefined; Use `deleteAt(index)` instead.");
211
+ return;
212
+ }
213
+ // skip if the value is the same as cached.
214
+ if (this.items[index] === value) {
215
+ return;
216
+ }
217
+ const operation = (this.items[index] !== undefined)
218
+ ? typeof (value) === "object"
219
+ ? OPERATION.DELETE_AND_ADD // schema child
220
+ : OPERATION.REPLACE // primitive
221
+ : OPERATION.ADD;
222
+ const changeTree = this[$changes];
223
+ changeTree.change(index, operation);
224
+ //
225
+ // set value's parent after the value is set
226
+ // (to avoid encoding "refId" operations before parent's "ADD" operation)
227
+ //
228
+ value[$changes]?.setParent(this, changeTree.root, index);
229
+ }
230
+ // encoding only
231
+ $deleteAt(index, operation) {
232
+ this[$changes].delete(index, operation);
233
+ }
234
+ // decoding only
235
+ $setAt(index, value, operation) {
236
+ if (index === 0 &&
237
+ operation === OPERATION.ADD &&
238
+ this.items[index] !== undefined) {
239
+ // handle decoding unshift
240
+ this.items.unshift(value);
241
+ }
242
+ else if (operation === OPERATION.DELETE_AND_MOVE) {
243
+ this.items.splice(index, 1);
244
+ this.items[index] = value;
245
+ }
246
+ else {
247
+ this.items[index] = value;
248
+ }
249
+ }
250
+ clear() {
251
+ // skip if already clear
252
+ if (this.items.length === 0) {
253
+ return;
254
+ }
255
+ // discard previous operations.
256
+ const changeTree = this[$changes];
257
+ // remove children references
258
+ changeTree.forEachChild((childChangeTree, _) => {
259
+ changeTree.root?.remove(childChangeTree);
260
+ });
261
+ changeTree.discard(true);
262
+ changeTree.operation(OPERATION.CLEAR);
263
+ this.items.length = 0;
264
+ this.tmpItems.length = 0;
265
+ }
266
+ /**
267
+ * Combines two or more arrays.
268
+ * @param items Additional items to add to the end of array1.
269
+ */
270
+ // @ts-ignore
271
+ concat(...items) {
272
+ return new ArraySchema(...this.items.concat(...items));
273
+ }
274
+ /**
275
+ * Adds all the elements of an array separated by the specified separator string.
276
+ * @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.
277
+ */
278
+ join(separator) {
279
+ return this.items.join(separator);
280
+ }
281
+ /**
282
+ * Reverses the elements in an Array.
283
+ */
284
+ // @ts-ignore
285
+ reverse() {
286
+ this[$changes].operation(OPERATION.REVERSE);
287
+ this.items.reverse();
288
+ this.tmpItems.reverse();
289
+ return this;
290
+ }
291
+ /**
292
+ * Removes the first element from an array and returns it.
293
+ */
294
+ shift() {
295
+ if (this.items.length === 0) {
296
+ return undefined;
297
+ }
298
+ const changeTree = this[$changes];
299
+ const index = this.tmpItems.findIndex(item => item === this.items[0]);
300
+ const allChangesIndex = this.items.findIndex(item => item === this.items[0]);
301
+ changeTree.delete(index, OPERATION.DELETE, allChangesIndex);
302
+ changeTree.shiftAllChangeIndexes(-1, allChangesIndex);
303
+ this.deletedIndexes[index] = true;
304
+ return this.items.shift();
305
+ }
306
+ /**
307
+ * Returns a section of an array.
308
+ * @param start The beginning of the specified portion of the array.
309
+ * @param end The end of the specified portion of the array. This is exclusive of the element at the index 'end'.
310
+ */
311
+ slice(start, end) {
312
+ const sliced = new ArraySchema();
313
+ sliced.push(...this.items.slice(start, end));
314
+ return sliced;
315
+ }
316
+ /**
317
+ * Sorts an array.
318
+ * @param compareFn Function used to determine the order of the elements. It is expected to return
319
+ * a negative value if first argument is less than second argument, zero if they're equal and a positive
320
+ * value otherwise. If omitted, the elements are sorted in ascending, ASCII character order.
321
+ * ```ts
322
+ * [11,2,22,1].sort((a, b) => a - b)
323
+ * ```
324
+ */
325
+ sort(compareFn = DEFAULT_SORT) {
326
+ this.isMovingItems = true;
327
+ const changeTree = this[$changes];
328
+ const sortedItems = this.items.sort(compareFn);
329
+ // wouldn't OPERATION.MOVE make more sense here?
330
+ sortedItems.forEach((_, i) => changeTree.change(i, OPERATION.REPLACE));
331
+ this.tmpItems.sort(compareFn);
332
+ this.isMovingItems = false;
333
+ return this;
334
+ }
335
+ /**
336
+ * Removes elements from an array and, if necessary, inserts new elements in their place, returning the deleted elements.
337
+ * @param start The zero-based location in the array from which to start removing elements.
338
+ * @param deleteCount The number of elements to remove.
339
+ * @param insertItems Elements to insert into the array in place of the deleted elements.
340
+ */
341
+ splice(start, deleteCount, ...insertItems) {
342
+ const changeTree = this[$changes];
343
+ const itemsLength = this.items.length;
344
+ const tmpItemsLength = this.tmpItems.length;
345
+ const insertCount = insertItems.length;
346
+ // build up-to-date list of indexes, excluding removed values.
347
+ const indexes = [];
348
+ for (let i = 0; i < tmpItemsLength; i++) {
349
+ if (this.deletedIndexes[i] !== true) {
350
+ indexes.push(i);
351
+ }
352
+ }
353
+ if (itemsLength > start) {
354
+ // if deleteCount is not provided, delete all items from start to end
355
+ if (deleteCount === undefined) {
356
+ deleteCount = itemsLength - start;
357
+ }
358
+ //
359
+ // delete operations at correct index
360
+ //
361
+ for (let i = start; i < start + deleteCount; i++) {
362
+ const index = indexes[i];
363
+ changeTree.delete(index, OPERATION.DELETE);
364
+ this.deletedIndexes[index] = true;
365
+ }
366
+ }
367
+ else {
368
+ // not enough items to delete
369
+ deleteCount = 0;
370
+ }
371
+ // insert operations
372
+ if (insertCount > 0) {
373
+ if (insertCount > deleteCount) {
374
+ console.error("Inserting more elements than deleting during ArraySchema#splice()");
375
+ throw new Error("ArraySchema#splice(): insertCount must be equal or lower than deleteCount.");
376
+ }
377
+ for (let i = 0; i < insertCount; i++) {
378
+ const addIndex = (indexes[start] ?? itemsLength) + i;
379
+ changeTree.indexedOperation(addIndex, (this.deletedIndexes[addIndex])
380
+ ? OPERATION.DELETE_AND_ADD
381
+ : OPERATION.ADD);
382
+ // set value's parent/root
383
+ insertItems[i][$changes]?.setParent(this, changeTree.root, addIndex);
384
+ }
385
+ }
386
+ //
387
+ // delete exceeding indexes from "allChanges"
388
+ // (prevent .encodeAll() from encoding non-existing items)
389
+ //
390
+ if (deleteCount > insertCount) {
391
+ changeTree.shiftAllChangeIndexes(-(deleteCount - insertCount), indexes[start + insertCount]);
392
+ // debugChangeSet("AFTER SHIFT indexes", changeTree.allChanges);
393
+ }
394
+ //
395
+ // FIXME: this code block is duplicated on ChangeTree
396
+ //
397
+ if (changeTree.filteredChanges !== undefined) {
398
+ changeTree.root?.enqueueChangeTree(changeTree, 'filteredChanges');
399
+ }
400
+ else {
401
+ changeTree.root?.enqueueChangeTree(changeTree, 'changes');
402
+ }
403
+ return this.items.splice(start, deleteCount, ...insertItems);
404
+ }
405
+ /**
406
+ * Inserts new elements at the start of an array.
407
+ * @param items Elements to insert at the start of the Array.
408
+ */
409
+ unshift(...items) {
410
+ const changeTree = this[$changes];
411
+ // shift indexes
412
+ changeTree.shiftChangeIndexes(items.length);
413
+ // new index
414
+ if (changeTree.isFiltered) {
415
+ setOperationAtIndex(changeTree.filteredChanges, this.items.length);
416
+ // changeTree.filteredChanges[this.items.length] = OPERATION.ADD;
417
+ }
418
+ else {
419
+ setOperationAtIndex(changeTree.allChanges, this.items.length);
420
+ // changeTree.allChanges[this.items.length] = OPERATION.ADD;
421
+ }
422
+ // FIXME: should we use OPERATION.MOVE here instead?
423
+ items.forEach((_, index) => {
424
+ changeTree.change(index, OPERATION.ADD);
425
+ });
426
+ this.tmpItems.unshift(...items);
427
+ return this.items.unshift(...items);
428
+ }
429
+ /**
430
+ * Returns the index of the first occurrence of a value in an array.
431
+ * @param searchElement The value to locate in the array.
432
+ * @param fromIndex The array index at which to begin the search. If fromIndex is omitted, the search starts at index 0.
433
+ */
434
+ indexOf(searchElement, fromIndex) {
435
+ return this.items.indexOf(searchElement, fromIndex);
436
+ }
437
+ /**
438
+ * Returns the index of the last occurrence of a specified value in an array.
439
+ * @param searchElement The value to locate in the array.
440
+ * @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.
441
+ */
442
+ lastIndexOf(searchElement, fromIndex = this.length - 1) {
443
+ return this.items.lastIndexOf(searchElement, fromIndex);
444
+ }
445
+ every(callbackfn, thisArg) {
446
+ return this.items.every(callbackfn, thisArg);
447
+ }
448
+ /**
449
+ * Determines whether the specified callback function returns true for any element of an array.
450
+ * @param callbackfn A function that accepts up to three arguments. The some method calls
451
+ * the callbackfn function for each element in the array until the callbackfn returns a value
452
+ * which is coercible to the Boolean value true, or until the end of the array.
453
+ * @param thisArg An object to which the this keyword can refer in the callbackfn function.
454
+ * If thisArg is omitted, undefined is used as the this value.
455
+ */
456
+ some(callbackfn, thisArg) {
457
+ return this.items.some(callbackfn, thisArg);
458
+ }
459
+ /**
460
+ * Performs the specified action for each element in an array.
461
+ * @param callbackfn A function that accepts up to three arguments. forEach calls the callbackfn function one time for each element in the array.
462
+ * @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.
463
+ */
464
+ forEach(callbackfn, thisArg) {
465
+ return this.items.forEach(callbackfn, thisArg);
466
+ }
467
+ /**
468
+ * Calls a defined callback function on each element of an array, and returns an array that contains the results.
469
+ * @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.
470
+ * @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.
471
+ */
472
+ map(callbackfn, thisArg) {
473
+ return this.items.map(callbackfn, thisArg);
474
+ }
475
+ filter(callbackfn, thisArg) {
476
+ return this.items.filter(callbackfn, thisArg);
477
+ }
478
+ /**
479
+ * 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.
480
+ * @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.
481
+ * @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.
482
+ */
483
+ reduce(callbackfn, initialValue) {
484
+ return this.items.reduce(callbackfn, initialValue);
485
+ }
486
+ /**
487
+ * 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.
488
+ * @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.
489
+ * @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.
490
+ */
491
+ reduceRight(callbackfn, initialValue) {
492
+ return this.items.reduceRight(callbackfn, initialValue);
493
+ }
494
+ /**
495
+ * Returns the value of the first element in the array where predicate is true, and undefined
496
+ * otherwise.
497
+ * @param predicate find calls predicate once for each element of the array, in ascending
498
+ * order, until it finds one where predicate returns true. If such an element is found, find
499
+ * immediately returns that element value. Otherwise, find returns undefined.
500
+ * @param thisArg If provided, it will be used as the this value for each invocation of
501
+ * predicate. If it is not provided, undefined is used instead.
502
+ */
503
+ find(predicate, thisArg) {
504
+ return this.items.find(predicate, thisArg);
505
+ }
506
+ /**
507
+ * Returns the index of the first element in the array where predicate is true, and -1
508
+ * otherwise.
509
+ * @param predicate find calls predicate once for each element of the array, in ascending
510
+ * order, until it finds one where predicate returns true. If such an element is found,
511
+ * findIndex immediately returns that element index. Otherwise, findIndex returns -1.
512
+ * @param thisArg If provided, it will be used as the this value for each invocation of
513
+ * predicate. If it is not provided, undefined is used instead.
514
+ */
515
+ findIndex(predicate, thisArg) {
516
+ return this.items.findIndex(predicate, thisArg);
517
+ }
518
+ /**
519
+ * Returns the this object after filling the section identified by start and end with value
520
+ * @param value value to fill array section with
521
+ * @param start index to start filling the array at. If start is negative, it is treated as
522
+ * length+start where length is the length of the array.
523
+ * @param end index to stop filling the array at. If end is negative, it is treated as
524
+ * length+end.
525
+ */
526
+ fill(value, start, end) {
527
+ //
528
+ // TODO
529
+ //
530
+ throw new Error("ArraySchema#fill() not implemented");
531
+ // this.$items.fill(value, start, end);
532
+ return this;
533
+ }
534
+ /**
535
+ * Returns the this object after copying a section of the array identified by start and end
536
+ * to the same array starting at position target
537
+ * @param target If target is negative, it is treated as length+target where length is the
538
+ * length of the array.
539
+ * @param start If start is negative, it is treated as length+start. If end is negative, it
540
+ * is treated as length+end.
541
+ * @param end If not specified, length of the this object is used as its default value.
542
+ */
543
+ copyWithin(target, start, end) {
544
+ //
545
+ // TODO
546
+ //
547
+ throw new Error("ArraySchema#copyWithin() not implemented");
548
+ return this;
549
+ }
550
+ /**
551
+ * Returns a string representation of an array.
552
+ */
553
+ toString() {
554
+ return this.items.toString();
555
+ }
556
+ /**
557
+ * Returns a string representation of an array. The elements are converted to string using their toLocalString methods.
558
+ */
559
+ toLocaleString() {
560
+ return this.items.toLocaleString();
561
+ }
562
+ ;
563
+ /** Iterator */
564
+ [Symbol.iterator]() {
565
+ return this.items[Symbol.iterator]();
566
+ }
567
+ static get [Symbol.species]() {
568
+ return ArraySchema;
569
+ }
570
+ /**
571
+ * Returns an iterable of key, value pairs for every entry in the array
572
+ */
573
+ entries() { return this.items.entries(); }
574
+ /**
575
+ * Returns an iterable of keys in the array
576
+ */
577
+ keys() { return this.items.keys(); }
578
+ /**
579
+ * Returns an iterable of values in the array
580
+ */
581
+ values() { return this.items.values(); }
582
+ /**
583
+ * Determines whether an array includes a certain element, returning true or false as appropriate.
584
+ * @param searchElement The element to search for.
585
+ * @param fromIndex The position in this array at which to begin searching for searchElement.
586
+ */
587
+ includes(searchElement, fromIndex) {
588
+ return this.items.includes(searchElement, fromIndex);
589
+ }
590
+ //
591
+ // ES2022
592
+ //
593
+ /**
594
+ * Calls a defined callback function on each element of an array. Then, flattens the result into
595
+ * a new array.
596
+ * This is identical to a map followed by flat with depth 1.
597
+ *
598
+ * @param callback A function that accepts up to three arguments. The flatMap method calls the
599
+ * callback function one time for each element in the array.
600
+ * @param thisArg An object to which the this keyword can refer in the callback function. If
601
+ * thisArg is omitted, undefined is used as the this value.
602
+ */
603
+ // @ts-ignore
604
+ flatMap(callback, thisArg) {
605
+ // @ts-ignore
606
+ throw new Error("ArraySchema#flatMap() is not supported.");
607
+ }
608
+ /**
609
+ * Returns a new array with all sub-array elements concatenated into it recursively up to the
610
+ * specified depth.
611
+ *
612
+ * @param depth The maximum recursion depth
613
+ */
614
+ // @ts-ignore
615
+ flat(depth) {
616
+ throw new Error("ArraySchema#flat() is not supported.");
617
+ }
618
+ findLast() {
619
+ // @ts-ignore
620
+ return this.items.findLast.apply(this.items, arguments);
621
+ }
622
+ findLastIndex(...args) {
623
+ // @ts-ignore
624
+ return this.items.findLastIndex.apply(this.items, arguments);
625
+ }
626
+ //
627
+ // ES2023
628
+ //
629
+ with(index, value) {
630
+ const copy = this.items.slice();
631
+ // Allow negative indexing from the end
632
+ if (index < 0)
633
+ index += this.length;
634
+ copy[index] = value;
635
+ return new ArraySchema(...copy);
636
+ }
637
+ toReversed() {
638
+ return this.items.slice().reverse();
639
+ }
640
+ toSorted(compareFn) {
641
+ return this.items.slice().sort(compareFn);
642
+ }
643
+ // @ts-ignore
644
+ toSpliced(start, deleteCount, ...items) {
645
+ // @ts-ignore
646
+ return this.items.toSpliced.apply(copy, arguments);
647
+ }
648
+ shuffle() {
649
+ return this.move((_) => {
650
+ let currentIndex = this.items.length;
651
+ while (currentIndex != 0) {
652
+ let randomIndex = Math.floor(Math.random() * currentIndex);
653
+ currentIndex--;
654
+ [this[currentIndex], this[randomIndex]] = [this[randomIndex], this[currentIndex]];
655
+ }
656
+ });
657
+ }
658
+ /**
659
+ * Allows to move items around in the array.
660
+ *
661
+ * Example:
662
+ * state.cards.move((cards) => {
663
+ * [cards[4], cards[3]] = [cards[3], cards[4]];
664
+ * [cards[3], cards[2]] = [cards[2], cards[3]];
665
+ * [cards[2], cards[0]] = [cards[0], cards[2]];
666
+ * [cards[1], cards[1]] = [cards[1], cards[1]];
667
+ * [cards[0], cards[0]] = [cards[0], cards[0]];
668
+ * })
669
+ *
670
+ * @param cb
671
+ * @returns
672
+ */
673
+ move(cb) {
674
+ this.isMovingItems = true;
675
+ cb(this);
676
+ this.isMovingItems = false;
677
+ return this;
678
+ }
679
+ [(Symbol.unscopables, $getByIndex)](index, isEncodeAll = false) {
680
+ //
681
+ // TODO: avoid unecessary `this.tmpItems` check during decoding.
682
+ //
683
+ // ENCODING uses `this.tmpItems` (or `this.items` if `isEncodeAll` is true)
684
+ // DECODING uses `this.items`
685
+ //
686
+ return (isEncodeAll)
687
+ ? this.items[index]
688
+ : this.deletedIndexes[index]
689
+ ? this.items[index]
690
+ : this.tmpItems[index] || this.items[index];
691
+ }
692
+ [$deleteByIndex](index) {
693
+ this.items[index] = undefined;
694
+ this.tmpItems[index] = undefined; // TODO: do not try to get "tmpItems" at decoding time.
695
+ }
696
+ [$onEncodeEnd]() {
697
+ this.tmpItems = this.items.slice();
698
+ this.deletedIndexes = {};
699
+ }
700
+ [$onDecodeEnd]() {
701
+ this.items = this.items.filter((item) => item !== undefined);
702
+ this.tmpItems = this.items.slice(); // TODO: do no use "tmpItems" at decoding time.
703
+ }
704
+ toArray() {
705
+ return this.items.slice(0);
706
+ }
707
+ toJSON() {
708
+ return this.toArray().map((value) => {
709
+ return (typeof (value['toJSON']) === "function")
710
+ ? value['toJSON']()
711
+ : value;
712
+ });
713
+ }
714
+ //
715
+ // Decoding utilities
716
+ //
717
+ clone(isDecoding) {
718
+ let cloned;
719
+ if (isDecoding) {
720
+ cloned = new ArraySchema();
721
+ cloned.push(...this.items);
722
+ }
723
+ else {
724
+ cloned = new ArraySchema(...this.map(item => ((item[$changes])
725
+ ? item.clone()
726
+ : item)));
727
+ }
728
+ return cloned;
729
+ }
730
+ ;
731
+ }
732
+ registerType("array", { constructor: ArraySchema });
733
+ //# sourceMappingURL=ArraySchema.js.map