Skip to content

Commit 375a669

Browse files
committed
plain schema({}) declaration: do not auto-initialize child Schema types
1 parent 9dc4133 commit 375a669

File tree

6 files changed

+25
-3
lines changed

6 files changed

+25
-3
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.70",
3+
"version": "3.0.71",
44
"description": "Binary state serializer with delta encoding for games",
55
"bin": {
66
"schema-codegen": "bin/schema-codegen",

src/annotations.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,6 @@ export function schema<
607607
} else if (typeof (value) === "function") {
608608
if (Schema.is(value)) {
609609
// Direct Schema type: Type → new Type()
610-
defaultValues[fieldName] = new value();
611610
fields[fieldName] = getNormalizedType(value);
612611
} else {
613612
methods[fieldName] = value;

test/BackwardsForwardsCompatibility.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ describe("backwards/forwards compatibility", () => {
5555
x: 10,
5656
y: 10,
5757
name: "Forward",
58+
// @ts-ignore
5859
arrayOfStrings: new ArraySchema("one"),
5960
}));
6061

test/Definition.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,27 @@ describe("Definition Tests", () => {
656656

657657
});
658658

659+
it("should not auto-initialize Schema instances", () => {
660+
const Base = schema({
661+
value: 'string',
662+
initialize(value: { something: string }) {
663+
this.value = value.something;
664+
},
665+
});
666+
667+
const Child = schema({
668+
world: 'string',
669+
random: Base,
670+
initialize(world: string) {
671+
this.world = world;
672+
},
673+
});
674+
675+
assert.doesNotThrow(() => {
676+
new Child('hello');
677+
});
678+
});
679+
659680
});
660681

661682
});

test/Schema.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,7 @@ describe("Type: Schema", () => {
344344

345345
assert.throws(() => {
346346
class Player extends Entity {
347+
// @ts-ignore
347348
@type("string") id: string;
348349
}
349350
}, /Duplicate 'id' definition on 'Player'/);

test/StateView.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2520,7 +2520,7 @@ describe("StateView", () => {
25202520
entity.components.push(new TagComponent());
25212521
assert.strictEqual(entity.components[1][$changes].isFiltered, true);
25222522

2523-
entity.components.push(new ListComponent().assign({list: new ArraySchema("one", "two")}));
2523+
entity.components.push(new ListComponent().assign({ list: new ArraySchema("one", "two") }));
25242524
assert.strictEqual(entity.components[2][$changes].isFiltered, true);
25252525

25262526
entity.component = new Component();

0 commit comments

Comments
 (0)