Skip to content

Commit 3151dff

Browse files
committed
MapSchema: dissallow delete if key doesn't exist
1 parent 3af1099 commit 3151dff

File tree

7 files changed

+30
-6
lines changed

7 files changed

+30
-6
lines changed

.github/workflows/test-suit.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111

1212
strategy:
1313
matrix:
14-
node-version: [16]
14+
node-version: [20]
1515

1616
steps:
1717
- uses: actions/checkout@v2

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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.61",
3+
"version": "3.0.62",
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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,10 @@ export class MapSchema<V=any, K extends string = string> implements Map<K, V>, C
151151
}
152152

153153
delete(key: K) {
154+
if (!this.$items.has(key)) {
155+
return false;
156+
}
157+
154158
const index = this[$changes].indexes[key];
155159

156160
this.deletedItems[index] = this[$changes].delete(index);

test/ArraySchema.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as assert from "assert";
22

33
import { State, Player, getCallbacks, getEncoder, createInstanceFromReflection, getDecoder, assertDeepStrictEqualEncodeAll, assertRefIdCounts } from "./Schema";
4-
import { ArraySchema, Schema, type, Reflection, $changes, MapSchema, ChangeTree, schema } from "../src";
4+
import { ArraySchema, Schema, type, $changes, MapSchema, ChangeTree, schema } from "../src";
55

66
describe("ArraySchema Tests", () => {
77

test/MapSchema.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,25 @@ describe("Type: MapSchema", () => {
661661
assertDeepStrictEqualEncodeAll(state);
662662
});
663663

664+
it("should allow removing the same key twice", () => {
665+
class Item extends Schema {
666+
@type("number") damage: number;
667+
}
668+
class State extends Schema {
669+
@type({ map: Item }) items = new MapSchema<Item>();
670+
}
671+
672+
const state = new State();
673+
state.items.set("one", new Item().assign({ damage: 10 }));
674+
state.items.set("two", new Item().assign({ damage: 20 }));
675+
676+
assertDeepStrictEqualEncodeAll(state);
677+
678+
state.items.delete("one");
679+
state.items.delete("one");
680+
assertDeepStrictEqualEncodeAll(state);
681+
});
682+
664683
it("should allow to move a key from one map to another", () => {
665684
class Entity extends Schema {
666685
@type("number") id: number;

test/Schema.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
import * as assert from "assert";
2+
13
import { Schema, type, ArraySchema, MapSchema, Reflection, Iterator, StateView } from "../src";
24
import { Decoder } from "../src/decoder/Decoder";
35
import { Encoder } from "../src/encoder/Encoder";
46
import { CallbackProxy, getDecoderStateCallbacks, SchemaCallbackProxy } from "../src/decoder/strategy/StateCallbacks";
5-
import assert = require("assert");
67

78
// augment Schema to add encode/decode methods
89
// (workaround to keep tests working while we don't migrate the tests to the new API)

0 commit comments

Comments
 (0)