@colyseus/schema 3.0.1 → 3.0.3

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.
@@ -9,11 +9,11 @@ import type { StateView } from "../../encoder/StateView";
9
9
  import type { Schema } from "../../Schema";
10
10
  import { assertInstanceType } from "../../encoding/assert";
11
11
 
12
- export class MapSchema<V=any> implements Map<string, V>, Collection<string, V, [string, V]> {
12
+ export class MapSchema<V=any, K extends string = string> implements Map<K, V>, Collection<K, V, [K, V]> {
13
13
  protected childType: new () => V;
14
14
 
15
- protected $items: Map<string, V> = new Map<string, V>();
16
- protected $indexes: Map<number, string> = new Map<number, string>();
15
+ protected $items: Map<K, V> = new Map<K, V>();
16
+ protected $indexes: Map<number, K> = new Map<number, K>();
17
17
 
18
18
  protected [$changes]: ChangeTree;
19
19
 
@@ -41,7 +41,7 @@ export class MapSchema<V=any> implements Map<string, V>, Collection<string, V, [
41
41
  return type['map'] !== undefined;
42
42
  }
43
43
 
44
- constructor (initialValues?: Map<string, V> | Record<string, V>) {
44
+ constructor (initialValues?: Map<K, V> | Record<K, V>) {
45
45
  this[$changes] = new ChangeTree(this);
46
46
  this[$changes].indexes = {};
47
47
 
@@ -68,12 +68,12 @@ export class MapSchema<V=any> implements Map<string, V>, Collection<string, V, [
68
68
  }
69
69
 
70
70
  /** Iterator */
71
- [Symbol.iterator](): IterableIterator<[string, V]> { return this.$items[Symbol.iterator](); }
71
+ [Symbol.iterator](): IterableIterator<[K, V]> { return this.$items[Symbol.iterator](); }
72
72
  get [Symbol.toStringTag]() { return this.$items[Symbol.toStringTag] }
73
73
 
74
74
  static get [Symbol.species]() { return MapSchema; }
75
75
 
76
- set(key: string, value: V) {
76
+ set(key: K, value: V) {
77
77
  if (value === undefined || value === null) {
78
78
  throw new Error(`MapSchema#set('${key}', ${value}): trying to set ${value} value on '${key}'.`);
79
79
 
@@ -83,7 +83,7 @@ export class MapSchema<V=any> implements Map<string, V>, Collection<string, V, [
83
83
 
84
84
  // Force "key" as string
85
85
  // See: https://github.com/colyseus/colyseus/issues/561#issuecomment-1646733468
86
- key = key.toString() as string;
86
+ key = key.toString() as K;
87
87
 
88
88
  const changeTree = this[$changes];
89
89
 
@@ -138,11 +138,11 @@ export class MapSchema<V=any> implements Map<string, V>, Collection<string, V, [
138
138
  return this;
139
139
  }
140
140
 
141
- get(key: string): V | undefined {
141
+ get(key: K): V | undefined {
142
142
  return this.$items.get(key);
143
143
  }
144
144
 
145
- delete(key: string) {
145
+ delete(key: K) {
146
146
  const index = this[$changes].indexes[key];
147
147
 
148
148
  this[$changes].delete(index);
@@ -166,11 +166,11 @@ export class MapSchema<V=any> implements Map<string, V>, Collection<string, V, [
166
166
  changeTree.operation(OPERATION.CLEAR);
167
167
  }
168
168
 
169
- has (key: string) {
169
+ has (key: K) {
170
170
  return this.$items.has(key);
171
171
  }
172
172
 
173
- forEach(callbackfn: (value: V, key: string, map: Map<string, V>) => void) {
173
+ forEach(callbackfn: (value: V, key: K, map: Map<K, V>) => void) {
174
174
  this.$items.forEach(callbackfn);
175
175
  }
176
176
 
@@ -190,7 +190,7 @@ export class MapSchema<V=any> implements Map<string, V>, Collection<string, V, [
190
190
  return this.$items.size;
191
191
  }
192
192
 
193
- protected setIndex(index: number, key: string) {
193
+ protected setIndex(index: number, key: K) {
194
194
  this.$indexes.set(index, key);
195
195
  }
196
196