Skip to content

Commit e29b4d2

Browse files
committed
MapSchema: force keys to be of string type. colyseus/colyseus#561
1 parent 8562af6 commit e29b4d2

File tree

5 files changed

+20
-16
lines changed

5 files changed

+20
-16
lines changed

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

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

src/types/MapSchema.ts

+4
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ export class MapSchema<V=any, K extends string = string> implements Map<K, V>, S
101101
throw new Error(`MapSchema#set('${key}', ${value}): trying to set ${value} value on '${key}'.`);
102102
}
103103

104+
// Force "key" as string
105+
// See: https://github.com/colyseus/colyseus/issues/561#issuecomment-1646733468
106+
key = key.toString() as K;
107+
104108
// get "index" for this value.
105109
const hasIndex = typeof(this.$changes.indexes[key]) !== "undefined";
106110
const index = (hasIndex)

test/ChangeTreeTest.ts

-13
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,6 @@ import { ChangeTree } from "../src/changes/ChangeTree";
44
import { Schema, type, MapSchema, ArraySchema } from "../src";
55

66
describe("ChangeTree", () => {
7-
xit("should not error when updating a detached structure", async () => {
8-
class Player extends Schema {
9-
@type("number") pos: number = 0;
10-
}
11-
class State extends Schema {
12-
@type({ map: Player }) players = new MapSchema<Player>();
13-
}
14-
const state = new State();
15-
// @ts-ignore
16-
state.players.set(3, new Player());
17-
assert.doesNotThrow(() => state.encodeAll());
18-
});
19-
207
it("instances should share parent/root references", () => {
218
class Skill extends Schema {
229
@type("number") damage: number;

test/MapSchemaTest.ts

+13
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,19 @@ describe("MapSchema Tests", () => {
1919
assert.deepEqual(Array.from(decodedState.mapOfPlayers.keys()), ['jake', 'katarina']);
2020
});
2121

22+
it("using number as key should not throw error", async () => {
23+
class Player extends Schema {
24+
@type("number") pos: number = 0;
25+
}
26+
class State extends Schema {
27+
@type({ map: Player }) players = new MapSchema<Player>();
28+
}
29+
const state = new State();
30+
// @ts-ignore
31+
state.players.set(3, new Player());
32+
assert.doesNotThrow(() => state.encodeAll());
33+
});
34+
2235
it("forEach()", () => {
2336
const map = new MapSchema<number>();
2437
map.set('one', 1);

0 commit comments

Comments
 (0)