Skip to content

Commit c5bd060

Browse files
committed
revert 4dc7e29
1 parent 6bc9522 commit c5bd060

File tree

3 files changed

+13
-40
lines changed

3 files changed

+13
-40
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@colyseus/schema",
3-
"version": "3.0.1",
3+
"version": "3.0.2",
44
"description": "Binary state serializer with delta encoding for games",
55
"bin": {
66
"schema-codegen": "./bin/schema-codegen",

src/types/custom/MapSchema.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ import type { StateView } from "../../encoder/StateView";
99
import type { Schema } from "../../Schema";
1010
import { assertInstanceType } from "../../encoding/assert";
1111

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]> {
1313
protected childType: new () => V;
1414

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>();
1717

1818
protected [$changes]: ChangeTree;
1919

@@ -41,7 +41,7 @@ export class MapSchema<V=any> implements Map<string, V>, Collection<string, V, [
4141
return type['map'] !== undefined;
4242
}
4343

44-
constructor (initialValues?: Map<string, V> | Record<string, V>) {
44+
constructor (initialValues?: Map<K, V> | Record<K, V>) {
4545
this[$changes] = new ChangeTree(this);
4646
this[$changes].indexes = {};
4747

@@ -68,12 +68,12 @@ export class MapSchema<V=any> implements Map<string, V>, Collection<string, V, [
6868
}
6969

7070
/** Iterator */
71-
[Symbol.iterator](): IterableIterator<[string, V]> { return this.$items[Symbol.iterator](); }
71+
[Symbol.iterator](): IterableIterator<[K, V]> { return this.$items[Symbol.iterator](); }
7272
get [Symbol.toStringTag]() { return this.$items[Symbol.toStringTag] }
7373

7474
static get [Symbol.species]() { return MapSchema; }
7575

76-
set(key: string, value: V) {
76+
set(key: K, value: V) {
7777
if (value === undefined || value === null) {
7878
throw new Error(`MapSchema#set('${key}', ${value}): trying to set ${value} value on '${key}'.`);
7979

@@ -83,7 +83,7 @@ export class MapSchema<V=any> implements Map<string, V>, Collection<string, V, [
8383

8484
// Force "key" as string
8585
// See: https://github.com/colyseus/colyseus/issues/561#issuecomment-1646733468
86-
key = key.toString() as string;
86+
key = key.toString() as K;
8787

8888
const changeTree = this[$changes];
8989

@@ -138,11 +138,11 @@ export class MapSchema<V=any> implements Map<string, V>, Collection<string, V, [
138138
return this;
139139
}
140140

141-
get(key: string): V | undefined {
141+
get(key: K): V | undefined {
142142
return this.$items.get(key);
143143
}
144144

145-
delete(key: string) {
145+
delete(key: K) {
146146
const index = this[$changes].indexes[key];
147147

148148
this[$changes].delete(index);
@@ -166,11 +166,11 @@ export class MapSchema<V=any> implements Map<string, V>, Collection<string, V, [
166166
changeTree.operation(OPERATION.CLEAR);
167167
}
168168

169-
has (key: string) {
169+
has (key: K) {
170170
return this.$items.has(key);
171171
}
172172

173-
forEach(callbackfn: (value: V, key: string, map: Map<string, V>) => void) {
173+
forEach(callbackfn: (value: V, key: K, map: Map<K, V>) => void) {
174174
this.$items.forEach(callbackfn);
175175
}
176176

@@ -190,7 +190,7 @@ export class MapSchema<V=any> implements Map<string, V>, Collection<string, V, [
190190
return this.$items.size;
191191
}
192192

193-
protected setIndex(index: number, key: string) {
193+
protected setIndex(index: number, key: K) {
194194
this.$indexes.set(index, key);
195195
}
196196

test/Schema.test.ts

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,33 +1009,6 @@ describe("Type: Schema", () => {
10091009
decoded.decode(state.encode());
10101010
assert.deepEqual({one: "hello"}, decoded.map.toJSON());
10111011
});
1012-
1013-
it("should support high number of refId", () => {
1014-
class Entity extends Schema {
1015-
@type("number") x: number;
1016-
@type("number") y: number;
1017-
}
1018-
1019-
class MyState extends Schema {
1020-
@type({map: Entity}) entities = new MapSchema<Entity>();
1021-
}
1022-
1023-
const state = new MyState();
1024-
1025-
const encoder = getEncoder(state);
1026-
encoder.root['nextUniqueId'] = Number.MAX_SAFE_INTEGER - 1;
1027-
1028-
state.entities.set('one', new Entity().assign({ x: 1, y: 1 }));
1029-
state.entities.set('two', new Entity().assign({ x: 2, y: 2 }));
1030-
state.entities.set('three', new Entity().assign({ x: 3, y: 3 }));
1031-
state.entities.set('four', new Entity().assign({ x: 4, y: 4 }));
1032-
state.entities.set('five', new Entity().assign({ x: 5, y: 5 }));
1033-
1034-
const decodedState = new MyState();
1035-
decodedState.decode(state.encode());
1036-
1037-
console.log(decodedState.toJSON());
1038-
});
10391012
})
10401013

10411014
describe("encoder: encodeAll", () => {

0 commit comments

Comments
 (0)