@colyseus/schema 4.0.29 → 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 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;