Skip to content

Commit 29ac980

Browse files
committed
plain schema(): fix auto-initializing child schemas if they don't require initialize() arguments
1 parent ac89e09 commit 29ac980

File tree

3 files changed

+40
-8
lines changed

3 files changed

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

src/annotations.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -597,8 +597,12 @@ export function schema<
597597

598598
} else if (value['type'] !== undefined && Schema.is(value['type'])) {
599599
// Direct Schema type: Type → new Type()
600-
// TODO: should we auto-initialize Schema instances, in case their initialize() method is not defined?
601-
// defaultValues[fieldName] = new value['type']();
600+
if (!value['type'].prototype.initialize || value['type'].prototype.initialize.length === 0) {
601+
// only auto-initialize Schema instances if:
602+
// - they don't have an initialize method
603+
// - or initialize method doesn't accept any parameters
604+
defaultValues[fieldName] = new value['type']();
605+
}
602606
}
603607
} else {
604608
defaultValues[fieldName] = value['default'];
@@ -608,8 +612,12 @@ export function schema<
608612
} else if (typeof (value) === "function") {
609613
if (Schema.is(value)) {
610614
// Direct Schema type: Type → new Type()
611-
// TODO: should we auto-initialize Schema instances, in case their initialize() method is not defined?
612-
// defaultValues[fieldName] = new value();
615+
if (!value.prototype.initialize || value.prototype.initialize.length === 0) {
616+
// only auto-initialize Schema instances if:
617+
// - they don't have an initialize method
618+
// - or initialize method doesn't accept any parameters
619+
defaultValues[fieldName] = new value();
620+
}
613621
fields[fieldName] = getNormalizedType(value);
614622
} else {
615623
methods[fieldName] = value;

test/Definition.test.ts

Lines changed: 27 additions & 3 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 === undefined);
311-
assert.ok(state.entity2 === undefined);
310+
assert.ok(state.entity1 instanceof Entity);
311+
assert.ok(state.entity2 instanceof Entity);
312312

313313
assert.ok(state.map instanceof MapSchema);
314314
assert.strictEqual(state.map.size, 0);
@@ -320,6 +320,8 @@ 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);
323325
assert.ok(state.map !== state2.map);
324326
assert.ok(state.array1 !== state2.array1);
325327
assert.ok(state.array2 !== state2.array2);
@@ -675,7 +677,7 @@ describe("Definition Tests", () => {
675677
});
676678
});
677679

678-
it("should exclude parent props from initialize method", () => {
680+
it("should exclude parent props from initialize method (1)", () => {
679681
const StatSchema = schema({
680682
value: 'number',
681683
initialize(value: number) {
@@ -710,6 +712,28 @@ describe("Definition Tests", () => {
710712
assert.strictEqual(entity.stats.get('hp')?.value, 500);
711713
});
712714

715+
it("should exclude parent props from encode method (2)", () => {
716+
const AnotherRandomSchema = schema({
717+
value: { type: 'string', default: 'world', },
718+
});
719+
720+
const RandomSchema = schema({
721+
value: { type: 'string', default: 'hello', },
722+
anotherRandom: AnotherRandomSchema,
723+
});
724+
725+
const StatSchema = schema({
726+
value: 'number',
727+
random: RandomSchema,
728+
initialize(value: number) {
729+
this.value = value;
730+
},
731+
});
732+
733+
const entity = new StatSchema(5);
734+
console.log(entity.random.value, entity.random.anotherRandom.value);
735+
})
736+
713737
});
714738

715739
});

0 commit comments

Comments
 (0)