@colyseus/schema 4.0.28 → 4.0.30
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/index.cjs +31 -0
- package/build/index.cjs.map +1 -1
- package/build/index.js +31 -0
- package/build/index.mjs +31 -0
- package/build/index.mjs.map +1 -1
- package/build/types/custom/MapSchema.d.ts +18 -1
- package/package.json +3 -2
- package/src/types/custom/MapSchema.ts +34 -1
package/build/index.cjs
CHANGED
|
@@ -2779,6 +2779,37 @@ class MapSchema {
|
|
|
2779
2779
|
get(key) {
|
|
2780
2780
|
return this.$items.get(key);
|
|
2781
2781
|
}
|
|
2782
|
+
/**
|
|
2783
|
+
* Returns the value for `key` if present. Otherwise inserts `defaultValue`
|
|
2784
|
+
* (tracked as an ADD change, like `set()`) and returns it.
|
|
2785
|
+
*
|
|
2786
|
+
* Mirrors `Map.prototype.getOrInsert` (TC39 "upsert" proposal, typed in
|
|
2787
|
+
* TypeScript 6's standard library).
|
|
2788
|
+
*/
|
|
2789
|
+
getOrInsert(key, defaultValue) {
|
|
2790
|
+
if (this.$items.has(key)) {
|
|
2791
|
+
return this.$items.get(key);
|
|
2792
|
+
}
|
|
2793
|
+
this.set(key, defaultValue);
|
|
2794
|
+
return defaultValue;
|
|
2795
|
+
}
|
|
2796
|
+
/**
|
|
2797
|
+
* Returns the value for `key` if present. Otherwise computes a value via
|
|
2798
|
+
* `callbackfn(key)`, inserts it (tracked as an ADD change, like `set()`)
|
|
2799
|
+
* and returns it. The callback is only invoked when the key is missing.
|
|
2800
|
+
*
|
|
2801
|
+
* Mirrors `Map.prototype.getOrInsertComputed` (TC39 "upsert" proposal,
|
|
2802
|
+
* typed in TypeScript 6's standard library).
|
|
2803
|
+
*/
|
|
2804
|
+
getOrInsertComputed(key, callbackfn) {
|
|
2805
|
+
if (this.$items.has(key)) {
|
|
2806
|
+
return this.$items.get(key);
|
|
2807
|
+
}
|
|
2808
|
+
const value = callbackfn(key);
|
|
2809
|
+
// per spec: overwrites even if callbackfn itself inserted `key`
|
|
2810
|
+
this.set(key, value);
|
|
2811
|
+
return value;
|
|
2812
|
+
}
|
|
2782
2813
|
delete(key) {
|
|
2783
2814
|
if (!this.$items.has(key)) {
|
|
2784
2815
|
return false;
|