Skip to content

Commit ebaaec1

Browse files
committed
make nonenumerable. fix replacing at .assign() calls.
1 parent 9e4f5b7 commit ebaaec1

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
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": "4.0.11",
3+
"version": "4.0.12",
44
"description": "Binary state serializer with delta encoding for games",
55
"type": "module",
66
"bin": {

src/decoder/ReferenceTracker.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,12 @@ export class ReferenceTracker {
4040
// for decoding
4141
addRef(refId: number, ref: IRef, incrementCount: boolean = true) {
4242
this.refs.set(refId, ref);
43-
ref[$refId] = refId;
43+
44+
Object.defineProperty(ref, $refId, {
45+
value: refId,
46+
enumerable: false,
47+
writable: true
48+
});
4449

4550
if (incrementCount) {
4651
this.refCount[refId] = (this.refCount[refId] || 0) + 1;

src/encoder/Root.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ export class Root {
2828

2929
// Assign unique `refId` to ref if it doesn't have one yet.
3030
if (ref[$refId] === undefined) {
31-
ref[$refId] = this.getNextUniqueId();
31+
Object.defineProperty(ref, $refId, {
32+
value: this.getNextUniqueId(),
33+
enumerable: false,
34+
writable: true
35+
});
3236
}
3337

3438
const refId = ref[$refId];

test/Schema.test.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import * as assert from "assert";
1010
import { State, Player, DeepState, DeepMap, DeepChild, Position, DeepEntity, assertDeepStrictEqualEncodeAll, createInstanceFromReflection, getEncoder } from "./Schema";
11-
import { Schema, ArraySchema, MapSchema, type, Metadata, $changes, Encoder, Decoder, SetSchema, schema, ToJSON } from "../src";
11+
import { Schema, ArraySchema, MapSchema, type, Metadata, $changes, Encoder, Decoder, SetSchema, schema, ToJSON, $refId } from "../src";
1212
import { getNormalizedType } from "../src/Metadata";
1313

1414
describe("Type: Schema", () => {
@@ -649,6 +649,19 @@ describe("Type: Schema", () => {
649649
});
650650

651651
describe("encoding/decoding", () => {
652+
it(".assign() should not re-assign the $refId", () => {
653+
const state = new State();
654+
state.player = new Player();
655+
state.mapOfPlayers = new MapSchema<Player>();
656+
657+
getEncoder(state);
658+
659+
const newPlayer = new Player().assign(state.player);
660+
state.mapOfPlayers.set("one", newPlayer);
661+
662+
assert.ok(newPlayer[$refId] !== state.player[$refId]);
663+
});
664+
652665
it("should encode/decode STRING", () => {
653666
const state = new State();
654667
state.fieldString = "Hello world";

0 commit comments

Comments
 (0)