@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.
@@ -28,11 +28,28 @@ export declare class MapSchema<V = any, K extends string = string> implements Ma
28
28
  static is(type: any): boolean;
29
29
  constructor(initialValues?: Map<K, V> | Record<K, V>);
30
30
  /** Iterator */
31
- [Symbol.iterator](): IterableIterator<[K, V]>;
31
+ [Symbol.iterator](): ReturnType<Map<K, V>[typeof Symbol.iterator]>;
32
32
  get [Symbol.toStringTag](): string;
33
33
  static get [Symbol.species](): typeof MapSchema;
34
34
  set(key: K, value: V): this;
35
35
  get(key: K): V | undefined;
36
+ /**
37
+ * Returns the value for `key` if present. Otherwise inserts `defaultValue`
38
+ * (tracked as an ADD change, like `set()`) and returns it.
39
+ *
40
+ * Mirrors `Map.prototype.getOrInsert` (TC39 "upsert" proposal, typed in
41
+ * TypeScript 6's standard library).
42
+ */
43
+ getOrInsert(key: K, defaultValue: V): V;
44
+ /**
45
+ * Returns the value for `key` if present. Otherwise computes a value via
46
+ * `callbackfn(key)`, inserts it (tracked as an ADD change, like `set()`)
47
+ * and returns it. The callback is only invoked when the key is missing.
48
+ *
49
+ * Mirrors `Map.prototype.getOrInsertComputed` (TC39 "upsert" proposal,
50
+ * typed in TypeScript 6's standard library).
51
+ */
52
+ getOrInsertComputed(key: K, callbackfn: (key: K) => V): V;
36
53
  delete(key: K): boolean;
37
54
  clear(): void;
38
55
  has(key: K): boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@colyseus/schema",
3
- "version": "4.0.28",
3
+ "version": "4.0.30",
4
4
  "description": "Binary state serializer with delta encoding for games",
5
5
  "type": "module",
6
6
  "bin": {
@@ -10,7 +10,8 @@
10
10
  "scripts": {
11
11
  "build": "tsc -p tsconfig.build.json && rollup -c rollup.config.mjs",
12
12
  "watch": "tsc -p tsconfig.build.json -w",
13
- "test": "tsx --tsconfig tsconfig.test.json ./node_modules/.bin/mocha test/*.test.ts test/**/*.test.ts",
13
+ "test": "npm run test:types && tsx --tsconfig tsconfig.test.json ./node_modules/.bin/mocha test/*.test.ts test/**/*.test.ts",
14
+ "test:types": "tsc -p tsconfig.build.json && tsc -p tsconfig.types.json",
14
15
  "typecheck": "tsc -p tsconfig.test.json",
15
16
  "coverage": "c8 npm run test",
16
17
  "generate-test-1": "bin/schema-codegen test-external/PrimitiveTypes.ts --namespace SchemaTest.PrimitiveTypes --output ../colyseus-unity-sdk/Assets/Colyseus/Tests/Editor/ColyseusTests/Schema/PrimitiveTypes",
@@ -77,7 +77,7 @@ export class MapSchema<V=any, K extends string = string> implements Map<K, V>, C
77
77
  }
78
78
 
79
79
  /** Iterator */
80
- [Symbol.iterator](): IterableIterator<[K, V]> { return this.$items[Symbol.iterator](); }
80
+ [Symbol.iterator](): ReturnType<Map<K, V>[typeof Symbol.iterator]> { return this.$items[Symbol.iterator](); }
81
81
  get [Symbol.toStringTag]() { return this.$items[Symbol.toStringTag] }
82
82
 
83
83
  static get [Symbol.species]() { return MapSchema; }
@@ -152,6 +152,39 @@ export class MapSchema<V=any, K extends string = string> implements Map<K, V>, C
152
152
  return this.$items.get(key);
153
153
  }
154
154
 
155
+ /**
156
+ * Returns the value for `key` if present. Otherwise inserts `defaultValue`
157
+ * (tracked as an ADD change, like `set()`) and returns it.
158
+ *
159
+ * Mirrors `Map.prototype.getOrInsert` (TC39 "upsert" proposal, typed in
160
+ * TypeScript 6's standard library).
161
+ */
162
+ getOrInsert(key: K, defaultValue: V): V {
163
+ if (this.$items.has(key)) {
164
+ return this.$items.get(key);
165
+ }
166
+ this.set(key, defaultValue);
167
+ return defaultValue;
168
+ }
169
+
170
+ /**
171
+ * Returns the value for `key` if present. Otherwise computes a value via
172
+ * `callbackfn(key)`, inserts it (tracked as an ADD change, like `set()`)
173
+ * and returns it. The callback is only invoked when the key is missing.
174
+ *
175
+ * Mirrors `Map.prototype.getOrInsertComputed` (TC39 "upsert" proposal,
176
+ * typed in TypeScript 6's standard library).
177
+ */
178
+ getOrInsertComputed(key: K, callbackfn: (key: K) => V): V {
179
+ if (this.$items.has(key)) {
180
+ return this.$items.get(key);
181
+ }
182
+ const value = callbackfn(key);
183
+ // per spec: overwrites even if callbackfn itself inserted `key`
184
+ this.set(key, value);
185
+ return value;
186
+ }
187
+
155
188
  delete(key: K) {
156
189
  if (!this.$items.has(key)) {
157
190
  return false;