Skip to content

Commit 1c31a8c

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

File tree

6 files changed

+32
-8
lines changed

6 files changed

+32
-8
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: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,8 @@ export function schema<
597597

598598
} else if (value['type'] !== undefined && Schema.is(value['type'])) {
599599
// Direct Schema type: Type → new Type()
600-
defaultValues[fieldName] = new value['type']();
600+
// TODO: should we auto-initialize Schema instances, in case their initialize() method is not defined?
601+
// defaultValues[fieldName] = new value['type']();
601602
}
602603
} else {
603604
defaultValues[fieldName] = value['default'];
@@ -607,7 +608,8 @@ export function schema<
607608
} else if (typeof (value) === "function") {
608609
if (Schema.is(value)) {
609610
// Direct Schema type: Type → new Type()
610-
defaultValues[fieldName] = new value();
611+
// TODO: should we auto-initialize Schema instances, in case their initialize() method is not defined?
612+
// defaultValues[fieldName] = new value();
611613
fields[fieldName] = getNormalizedType(value);
612614
} else {
613615
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: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,8 @@ describe("Definition Tests", () => {
307307
});
308308

309309
const state = new State();
310-
assert.ok(state.entity1 instanceof Entity);
311-
assert.ok(state.entity2 instanceof Entity);
310+
assert.ok(state.entity1 === undefined);
311+
assert.ok(state.entity2 === undefined);
312312

313313
assert.ok(state.map instanceof MapSchema);
314314
assert.strictEqual(state.map.size, 0);
@@ -320,8 +320,6 @@ describe("Definition Tests", () => {
320320
assert.strictEqual(state.array2.length, 0);
321321

322322
const state2 = new State();
323-
assert.ok(state.entity1 !== state2.entity1);
324-
assert.ok(state.entity2 !== state2.entity2);
325323
assert.ok(state.map !== state2.map);
326324
assert.ok(state.array1 !== state2.array1);
327325
assert.ok(state.array2 !== state2.array2);
@@ -656,6 +654,27 @@ describe("Definition Tests", () => {
656654

657655
});
658656

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

661680
});

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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2520,7 +2520,8 @@ 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+
// @ts-ignore
2524+
entity.components.push(new ListComponent().assign({ list: new ArraySchema("one", "two") }));
25242525
assert.strictEqual(entity.components[2][$changes].isFiltered, true);
25252526

25262527
entity.component = new Component();

0 commit comments

Comments
 (0)