@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.
@@ -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](): IterableIterator<[K, V]> { return this.$items[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;