Skip to content

Commit 2b69071

Browse files
authored
Merge pull request #136 from pmndrs/fix--world-set-types
fix: world set types
2 parents fa588e7 + 29189db commit 2b69071

File tree

5 files changed

+65
-46
lines changed

5 files changed

+65
-46
lines changed

packages/core/src/entity/types.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { Relation } from '../relation/types';
22
import type {
33
ConfigurableTrait,
44
ExtractSchema,
5+
SetTraitCallback,
56
Trait,
67
TraitInstance,
78
TraitValue,
@@ -15,9 +16,7 @@ export type Entity = number & {
1516
changed: (trait: Trait) => void;
1617
set: <T extends Trait>(
1718
trait: T,
18-
value:
19-
| TraitValue<ExtractSchema<T>>
20-
| ((prev: TraitInstance<ExtractSchema<T>>) => TraitValue<ExtractSchema<T>>),
19+
value: TraitValue<ExtractSchema<T>> | SetTraitCallback<T>,
2120
flagChanged?: boolean
2221
) => void;
2322
get: <T extends Trait | Relation<Trait>>(trait: T) => TraitInstance<ExtractSchema<T>> | undefined;

packages/core/src/trait/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ type TraitInstanceFromTrait<T extends Trait> = T['schema'] extends AoSFactory
5353
: T['schema'][P];
5454
};
5555

56+
export type SetTraitCallback<T extends Trait> = (
57+
prev: TraitInstance<ExtractSchema<T>>
58+
) => TraitValue<ExtractSchema<T>>;
59+
5660
type TraitInstanceFromSchema<T extends Schema> = T extends AoSFactory
5761
? ReturnType<T>
5862
: {

packages/core/src/world/world.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import type { TraitData } from '../trait/trait-data';
1313
import type {
1414
ConfigurableTrait,
1515
ExtractSchema,
16+
SetTraitCallback,
1617
Trait,
1718
TraitInstance,
1819
TraitValue,
@@ -111,7 +112,7 @@ export class World {
111112
return getTrait(this, this[$internal].worldEntity, trait);
112113
}
113114

114-
set<T extends Trait>(trait: T, value: TraitValue<ExtractSchema<T>>) {
115+
set<T extends Trait>(trait: T, value: TraitValue<ExtractSchema<T>> | SetTraitCallback<T>) {
115116
setTrait(this, this[$internal].worldEntity, trait, value);
116117
}
117118

packages/core/tests/world.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,24 @@ describe('World', () => {
9292
expect(world.has(Time)).toBe(false);
9393
});
9494

95+
it('should set singletons', () => {
96+
const Test = trait({ last: 0, delta: 0 });
97+
const world = createWorld(Test);
98+
99+
world.set(Test, { last: 1, delta: 1 });
100+
101+
expect(world.get(Test)!.last).toBe(1);
102+
expect(world.get(Test)!.delta).toBe(1);
103+
104+
// Use callbacks to set.
105+
world.set(Test, (prev) => {
106+
return { last: prev.last + 1, delta: prev.delta + 1 };
107+
});
108+
109+
expect(world.get(Test)!.last).toBe(2);
110+
expect(world.get(Test)!.delta).toBe(2);
111+
});
112+
95113
it('should observe traits', () => {
96114
const TimeOfDay = trait({ hour: 0 });
97115
const world = createWorld(TimeOfDay);

pnpm-lock.yaml

Lines changed: 39 additions & 42 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)