Skip to content

Commit 3dd71cb

Browse files
committed
plain schema() declaration: do not call initialize() automatically on parent classes
1 parent 29ac980 commit 3dd71cb

File tree

3 files changed

+80
-5
lines changed

3 files changed

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

src/annotations.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,13 @@ export function schema<
662662
// call initialize method
663663
if (methods.initialize && typeof methods.initialize === 'function') {
664664
super(Object.assign({}, getDefaultValues(), getParentProps(args[0] || {})));
665-
methods.initialize.apply(this, args);
665+
/**
666+
* only call initialize() in the current class, not the parent ones.
667+
* see "should not call initialize automatically when creating an instance of inherited Schema"
668+
*/
669+
if (new.target === klass) {
670+
methods.initialize.apply(this, args);
671+
}
666672

667673
} else {
668674
super(Object.assign({}, getDefaultValues(), args[0] || {}));

test/Definition.test.ts

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,7 @@ describe("Definition Tests", () => {
712712
assert.strictEqual(entity.stats.get('hp')?.value, 500);
713713
});
714714

715-
it("should exclude parent props from encode method (2)", () => {
715+
it("should auto-initialize Schema instances with default values", () => {
716716
const AnotherRandomSchema = schema({
717717
value: { type: 'string', default: 'world', },
718718
});
@@ -730,10 +730,79 @@ describe("Definition Tests", () => {
730730
},
731731
});
732732

733-
const entity = new StatSchema(5);
734-
console.log(entity.random.value, entity.random.anotherRandom.value);
733+
assert.doesNotThrow(() => {
734+
const entity = new StatSchema(5);
735+
assert.strictEqual(entity.value, 5);
736+
assert.strictEqual(entity.random.value, 'hello');
737+
assert.strictEqual(entity.random.anotherRandom.value, 'world');
738+
739+
})
735740
})
736741

742+
describe("extends", () => {
743+
it("should not call initialize automatically when creating an instance of inherited Schema (1)", () => {
744+
let childSchemaInitializeCallCount = 0;
745+
746+
const ChildSchema = schema({
747+
name: 'string',
748+
initialize(props: { name: string }) {
749+
this.name = props.name;
750+
childSchemaInitializeCallCount++;
751+
},
752+
});
753+
754+
const ParentSchema = ChildSchema.extends({
755+
id: 'string',
756+
initialize(props: { id: string }) {
757+
ChildSchema.prototype.initialize.call(this, {
758+
name: 'Jim',
759+
});
760+
this.id = props.id;
761+
},
762+
});
763+
764+
assert.doesNotThrow(() => {
765+
const parent = new ParentSchema({ id: 'parent' });
766+
assert.strictEqual(parent.id, 'parent');
767+
assert.strictEqual(parent.name, 'Jim');
768+
});
769+
770+
assert.strictEqual(childSchemaInitializeCallCount, 1);
771+
})
772+
773+
it("should not call initialize automatically when creating an instance of inherited Schema (2)", () => {
774+
const EntitySchema = schema({
775+
id: 'string',
776+
name: 'string',
777+
initialize(props: { id: string; name: string }) {
778+
this.id = props.id;
779+
this.name = props.name;
780+
},
781+
});
782+
783+
const PlayerSchema = EntitySchema.extends({
784+
initialize(props: any) {
785+
EntitySchema.prototype.initialize.call(this, {
786+
id: props.public_id,
787+
name: props.username,
788+
});
789+
},
790+
});
791+
792+
const player = new EntitySchema({ id: '1', name: 'test' });
793+
assert.strictEqual(player.id, '1');
794+
assert.strictEqual(player.name, 'test');
795+
796+
assert.doesNotThrow(() => {
797+
const player = new PlayerSchema({ id: 1, public_id: '1', username: 'test' });
798+
assert.strictEqual(player.id, '1');
799+
assert.strictEqual(player.name, 'test');
800+
});
801+
});
802+
803+
});
804+
805+
737806
});
738807

739808
});

0 commit comments

Comments
 (0)