@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.
- package/README.md +8 -7
- package/build/cjs/index.js +21 -2
- package/build/cjs/index.js.map +1 -1
- package/build/esm/index.mjs +21 -2
- package/build/esm/index.mjs.map +1 -1
- package/build/umd/index.js +21 -2
- package/lib/decoder/DecodeOperation.js +1 -1
- package/lib/decoder/DecodeOperation.js.map +1 -1
- package/lib/decoder/strategy/StateCallbacks.d.ts +9 -0
- package/lib/decoder/strategy/StateCallbacks.js +20 -1
- package/lib/decoder/strategy/StateCallbacks.js.map +1 -1
- package/lib/types/custom/MapSchema.d.ts +14 -14
- package/lib/types/custom/MapSchema.js.map +1 -1
- package/package.json +1 -1
- package/src/decoder/DecodeOperation.ts +1 -1
- package/src/decoder/strategy/StateCallbacks.ts +35 -8
- package/src/types/custom/MapSchema.ts +12 -12
|
@@ -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<
|
|
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<
|
|
16
|
-
protected $indexes: Map<number,
|
|
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<
|
|
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<[
|
|
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:
|
|
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
|
|
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:
|
|
141
|
+
get(key: K): V | undefined {
|
|
142
142
|
return this.$items.get(key);
|
|
143
143
|
}
|
|
144
144
|
|
|
145
|
-
delete(key:
|
|
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:
|
|
169
|
+
has (key: K) {
|
|
170
170
|
return this.$items.has(key);
|
|
171
171
|
}
|
|
172
172
|
|
|
173
|
-
forEach(callbackfn: (value: V, key:
|
|
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:
|
|
193
|
+
protected setIndex(index: number, key: K) {
|
|
194
194
|
this.$indexes.set(index, key);
|
|
195
195
|
}
|
|
196
196
|
|