refactor: entity -> MutableSignal

This commit is contained in:
2026-04-03 14:19:44 +08:00
parent b1b059de8c
commit 86714e7837
8 changed files with 51 additions and 62 deletions
+3 -3
View File
@@ -16,7 +16,7 @@ import {
PlayerType,
getBoardRegion,
} from '@/samples/boop';
import {Entity} from "@/utils/entity";
import {MutableSignal} from "@/utils/entity";
import {createGameContext} from "@/";
import type { PromptEvent } from '@/utils/command';
@@ -25,7 +25,7 @@ function createTestContext() {
return { registry, ctx };
}
function getState(ctx: ReturnType<typeof createTestContext>['ctx']): Entity<BoopState> {
function getState(ctx: ReturnType<typeof createTestContext>['ctx']): MutableSignal<BoopState> {
return ctx.state;
}
@@ -35,7 +35,7 @@ function waitForPrompt(ctx: ReturnType<typeof createTestContext>['ctx']): Promis
});
}
function getParts(state: Entity<BoopState>) {
function getParts(state: MutableSignal<BoopState>) {
return state.value.pieces;
}
+2 -2
View File
@@ -8,7 +8,7 @@ import {
TicTacToeState,
WinnerType, PlayerType
} from '@/samples/tic-tac-toe';
import {Entity} from "@/utils/entity";
import {MutableSignal} from "@/utils/mutable-signal";
import {createGameContext} from "@/";
import type { PromptEvent } from '@/utils/command';
@@ -17,7 +17,7 @@ function createTestContext() {
return { registry, ctx };
}
function getState(ctx: ReturnType<typeof createTestContext>['ctx']): Entity<TicTacToeState> {
function getState(ctx: ReturnType<typeof createTestContext>['ctx']): MutableSignal<TicTacToeState> {
return ctx.state;
}
@@ -1,5 +1,5 @@
import { describe, it, expect } from 'vitest';
import { createEntityCollection, Entity, entity } from '@/utils/entity';
import { createEntityCollection, MutableSignal, mutableSignal } from '@/utils/mutable-signal';
type TestEntity = {
id: string;
@@ -82,16 +82,6 @@ describe('createEntityCollection', () => {
expect(collection.get('nonexistent')).toBeUndefined();
});
it('should have correct accessor id', () => {
const collection = createEntityCollection<TestEntity>();
const testEntity: TestEntity = { id: 'e1', name: 'Entity 1', value: 10 };
collection.add(testEntity);
const accessor = collection.get('e1');
expect(accessor.id).toBe('e1');
});
it('should handle removing non-existent entity', () => {
const collection = createEntityCollection<TestEntity>();
const testEntity: TestEntity = { id: 'e1', name: 'Entity 1', value: 10 };
@@ -116,20 +106,19 @@ describe('createEntityCollection', () => {
});
});
describe('Entity', () => {
it('should create entity with id and value', () => {
const e = entity('test', { count: 1 });
expect(e.id).toBe('test');
expect(e.value.count).toBe(1);
describe('MutableSignal', () => {
it('should create signal with initial value', () => {
const s = mutableSignal({ count: 1 });
expect(s.value.count).toBe(1);
});
it('should produce immutable updates', () => {
const e = entity('test', { count: 1, items: [1, 2, 3] });
e.produce(draft => {
const s = mutableSignal({ count: 1, items: [1, 2, 3] });
s.produce(draft => {
draft.count = 2;
draft.items.push(4);
});
expect(e.value.count).toBe(2);
expect(e.value.items).toEqual([1, 2, 3, 4]);
expect(s.value.count).toBe(2);
expect(s.value.items).toEqual([1, 2, 3, 4]);
});
});