@colyseus/schema 5.0.9 → 5.0.11
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/README.md +87 -51
- package/build/codegen/cli.cjs +13 -5
- package/build/codegen/cli.cjs.map +1 -1
- package/build/encoder/ChangeTree.d.ts +10 -3
- package/build/encoder/StateView.d.ts +0 -6
- package/build/index.cjs +182 -102
- package/build/index.cjs.map +1 -1
- package/build/index.js +182 -102
- package/build/index.mjs +182 -102
- package/build/index.mjs.map +1 -1
- package/build/types/custom/ArraySchema.d.ts +9 -1
- package/build/types/custom/MapSchema.d.ts +18 -1
- package/package.json +9 -3
- package/src/codegen/cli.ts +4 -4
- package/src/codegen/parser.ts +10 -1
- package/src/decoder/DecodeOperation.ts +23 -7
- package/src/encoder/ChangeTree.ts +18 -12
- package/src/encoder/EncodeOperation.ts +14 -1
- package/src/encoder/Encoder.ts +3 -16
- package/src/encoder/StateView.ts +16 -35
- package/src/types/custom/ArraySchema.ts +77 -30
- package/src/types/custom/MapSchema.ts +34 -1
|
@@ -140,7 +140,7 @@ export class MapSchema<V=any, K extends string = string> implements Map<K, V>, C
|
|
|
140
140
|
}
|
|
141
141
|
|
|
142
142
|
/** Iterator */
|
|
143
|
-
[Symbol.iterator]():
|
|
143
|
+
[Symbol.iterator](): ReturnType<Map<K, V>[typeof Symbol.iterator]> { return this.$items[Symbol.iterator](); }
|
|
144
144
|
get [Symbol.toStringTag]() { return this.$items[Symbol.toStringTag] }
|
|
145
145
|
|
|
146
146
|
static get [Symbol.species]() { return MapSchema; }
|
|
@@ -224,6 +224,39 @@ export class MapSchema<V=any, K extends string = string> implements Map<K, V>, C
|
|
|
224
224
|
return this.$items.get(key);
|
|
225
225
|
}
|
|
226
226
|
|
|
227
|
+
/**
|
|
228
|
+
* Returns the value for `key` if present. Otherwise inserts `defaultValue`
|
|
229
|
+
* (tracked as an ADD change, like `set()`) and returns it.
|
|
230
|
+
*
|
|
231
|
+
* Mirrors `Map.prototype.getOrInsert` (TC39 "upsert" proposal, typed in
|
|
232
|
+
* TypeScript 6's standard library).
|
|
233
|
+
*/
|
|
234
|
+
getOrInsert(key: K, defaultValue: V): V {
|
|
235
|
+
if (this.$items.has(key)) {
|
|
236
|
+
return this.$items.get(key);
|
|
237
|
+
}
|
|
238
|
+
this.set(key, defaultValue);
|
|
239
|
+
return defaultValue;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Returns the value for `key` if present. Otherwise computes a value via
|
|
244
|
+
* `callbackfn(key)`, inserts it (tracked as an ADD change, like `set()`)
|
|
245
|
+
* and returns it. The callback is only invoked when the key is missing.
|
|
246
|
+
*
|
|
247
|
+
* Mirrors `Map.prototype.getOrInsertComputed` (TC39 "upsert" proposal,
|
|
248
|
+
* typed in TypeScript 6's standard library).
|
|
249
|
+
*/
|
|
250
|
+
getOrInsertComputed(key: K, callbackfn: (key: K) => V): V {
|
|
251
|
+
if (this.$items.has(key)) {
|
|
252
|
+
return this.$items.get(key);
|
|
253
|
+
}
|
|
254
|
+
const value = callbackfn(key);
|
|
255
|
+
// per spec: overwrites even if callbackfn itself inserted `key`
|
|
256
|
+
this.set(key, value);
|
|
257
|
+
return value;
|
|
258
|
+
}
|
|
259
|
+
|
|
227
260
|
delete(key: K) {
|
|
228
261
|
if (!this.$items.has(key)) {
|
|
229
262
|
return false;
|