Skip to content

Commit 2183c8c

Browse files
committed
refactor(builder): deprecate withTag and withRelationTag methods
Make value parameter optional for void components in with() and withRelation() methods, allowing simpler syntax like .with(TagId) instead of .withTag(TagId). The deprecated methods remain for backward compatibility.
1 parent 2343748 commit 2183c8c

File tree

4 files changed

+19
-9
lines changed

4 files changed

+19
-9
lines changed

README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -322,10 +322,8 @@ const restored = new World(readySnapshot);
322322

323323
EntityBuilder 提供流式 API 用于便捷的实体创建:
324324

325-
- `with(componentId, value)`: 添加组件到构建器
326-
- `withTag(componentId)`: 添加标记组件(无值)到构建器
327-
- `withRelation(componentId, targetEntity, value)`: 添加关系组件到构建器
328-
- `withRelationTag(componentId, targetEntity)`: 添加关系标记(无值)到构建器
325+
- `with(componentId, value?)`: 添加组件到构建器(对于 `void` 类型组件,value 参数可省略)
326+
- `withRelation(componentId, targetEntity, value?)`: 添加关系组件到构建器(对于 `void` 类型关系,value 参数可省略)
329327
- `build()`: 创建实体并应用所有组件(需要手动调用 `world.sync()`
330328

331329
### World

src/__tests__/testing.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ describe("testing module", () => {
102102

103103
it("should support tag components", () => {
104104
const world = new World();
105-
const entity = new EntityBuilder(world).withTag(TagId).build();
105+
const entity = new EntityBuilder(world).with(TagId).build();
106106
world.sync();
107107

108108
expect(world.has(entity, TagId)).toBe(true);
@@ -130,7 +130,7 @@ describe("testing module", () => {
130130
const parent = world.new();
131131
world.sync();
132132

133-
const child = new EntityBuilder(world).withRelationTag(ChildOfId, parent).build();
133+
const child = new EntityBuilder(world).withRelation(ChildOfId, parent).build();
134134
world.sync();
135135

136136
const relationId = relation(ChildOfId, parent);

src/core/builder.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,33 @@ export class EntityBuilder {
2121
this.world = world;
2222
}
2323

24-
with<T>(componentId: EntityId<T>, value: T): this {
24+
with<T>(componentId: EntityId<T>, ...args: T extends void ? [] | [void] : [T]): this {
25+
const value = (args.length > 0 ? args[0] : undefined) as T;
2526
this.components.push({ type: "component", id: componentId, value });
2627
return this;
2728
}
2829

30+
/**
31+
* @deprecated Use `with(componentId)` instead for void components
32+
*/
2933
withTag(componentId: EntityId<void>): this {
3034
this.components.push({ type: "component", id: componentId, value: undefined as void });
3135
return this;
3236
}
3337

34-
withRelation<T>(componentId: ComponentId<T>, targetEntity: EntityId<any>, value: T): this {
38+
withRelation<T>(
39+
componentId: ComponentId<T>,
40+
targetEntity: EntityId<any>,
41+
...args: T extends void ? [] | [void] : [T]
42+
): this {
43+
const value = (args.length > 0 ? args[0] : undefined) as T;
3544
this.components.push({ type: "relation", componentId, targetId: targetEntity, value });
3645
return this;
3746
}
3847

48+
/**
49+
* @deprecated Use `withRelation(componentId, targetEntity)` instead for void relations
50+
*/
3951
withRelationTag(componentId: ComponentId<void>, targetEntity: EntityId<any>): this {
4052
this.components.push({ type: "relation", componentId, targetId: targetEntity, value: undefined as void });
4153
return this;

src/testing/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ export class WorldFixture {
216216
*
217217
* // Tag component (void type)
218218
* const tagged = new EntityBuilder(world)
219-
* .withTag(PlayerTagId)
219+
* .with(PlayerTagId)
220220
* .build();
221221
* ```
222222
*/

0 commit comments

Comments
 (0)