refactor: fix reactivity

This commit is contained in:
hypercross 2026-04-01 22:55:59 +08:00
parent 4761806a02
commit dbd2a25185
4 changed files with 102 additions and 39 deletions

View File

@ -2,7 +2,7 @@ import {createModel, Signal, signal} from '@preact/signals-core';
import {createEntityCollection} from "../utils/entity"; import {createEntityCollection} from "../utils/entity";
import {Part} from "./part"; import {Part} from "./part";
import {Region} from "./region"; import {Region} from "./region";
import {RuleRegistry, RuleContext, dispatchCommand as dispatchRuleCommand} from "./rule"; import {RuleDef, RuleRegistry, RuleContext, dispatchCommand as dispatchRuleCommand} from "./rule";
export type Context = { export type Context = {
type: string; type: string;
@ -37,11 +37,33 @@ export const GameContext = createModel((root: Context) => {
return undefined; return undefined;
} }
function registerRule(name: string, rule: RuleDef<unknown>) {
const newRules = new Map(rules.value);
newRules.set(name, rule);
rules.value = newRules;
}
function unregisterRule(name: string) {
const newRules = new Map(rules.value);
newRules.delete(name);
rules.value = newRules;
}
function addRuleContext(ctx: RuleContext<unknown>) {
ruleContexts.value = [...ruleContexts.value, ctx];
}
function removeRuleContext(ctx: RuleContext<unknown>) {
ruleContexts.value = ruleContexts.value.filter(c => c !== ctx);
}
function dispatchCommand(input: string) { function dispatchCommand(input: string) {
return dispatchRuleCommand({ return dispatchRuleCommand({
rules: rules.value, rules: rules.value,
ruleContexts: ruleContexts.value, ruleContexts: ruleContexts.value,
contexts, contexts,
addRuleContext,
removeRuleContext,
}, input); }, input);
} }
@ -54,6 +76,8 @@ export const GameContext = createModel((root: Context) => {
pushContext, pushContext,
popContext, popContext,
latestContext, latestContext,
registerRule,
unregisterRule,
dispatchCommand, dispatchCommand,
} }
}) })

View File

@ -38,13 +38,12 @@ function parseYieldedSchema(value: string | CommandSchema): CommandSchema {
function pushContextToGame(game: GameContextLike, ctx: RuleContext<unknown>) { function pushContextToGame(game: GameContextLike, ctx: RuleContext<unknown>) {
game.contexts.value = [...game.contexts.value, { value: ctx } as any]; game.contexts.value = [...game.contexts.value, { value: ctx } as any];
game.ruleContexts.push(ctx); game.addRuleContext(ctx);
} }
function discardChildren(game: GameContextLike, parent: RuleContext<unknown>) { function discardChildren(game: GameContextLike, parent: RuleContext<unknown>) {
for (const child of parent.children) { for (const child of parent.children) {
const idx = game.ruleContexts.indexOf(child); game.removeRuleContext(child);
if (idx !== -1) game.ruleContexts.splice(idx, 1);
const ctxIdx = game.contexts.value.findIndex((c: any) => c.value === child); const ctxIdx = game.contexts.value.findIndex((c: any) => c.value === child);
if (ctxIdx !== -1) { if (ctxIdx !== -1) {
@ -161,4 +160,6 @@ type GameContextLike = {
rules: RuleRegistry; rules: RuleRegistry;
ruleContexts: RuleContext<unknown>[]; ruleContexts: RuleContext<unknown>[];
contexts: { value: any[] }; contexts: { value: any[] };
addRuleContext: (ctx: RuleContext<unknown>) => void;
removeRuleContext: (ctx: RuleContext<unknown>) => void;
}; };

View File

@ -9,6 +9,43 @@ export type EntityAccessor<T extends Entity> = {
value: T; value: T;
} }
function createReactiveProxy<T extends Entity>(entitySignal: Signal<T>): T {
return new Proxy({} as T, {
get(_target, prop) {
const current = entitySignal.value;
const value = current[prop as keyof T];
if (typeof value === 'function') {
return value.bind(current);
}
return value;
},
set(_target, prop, value) {
const current = entitySignal.value;
entitySignal.value = { ...current, [prop]: value };
return true;
},
ownKeys(_target) {
return Reflect.ownKeys(entitySignal.value);
},
getOwnPropertyDescriptor(_target, prop) {
return Reflect.getOwnPropertyDescriptor(entitySignal.value, prop);
},
});
}
function createReactiveAccessor<T extends Entity>(id: string, entitySignal: Signal<T>): EntityAccessor<T> {
const proxy = createReactiveProxy(entitySignal);
return {
id,
get value() {
return proxy;
},
set value(value: T) {
entitySignal.value = value;
}
} as EntityAccessor<T>;
}
export function createEntityCollection<T extends Entity>() { export function createEntityCollection<T extends Entity>() {
const collection = signal({} as Record<string, Signal<T>>); const collection = signal({} as Record<string, Signal<T>>);
const remove = (...ids: string[]) => { const remove = (...ids: string[]) => {
@ -24,17 +61,18 @@ export function createEntityCollection<T extends Entity>() {
}; };
}; };
const get = (id: string) => { const get = (id: string): EntityAccessor<T> => {
const entitySignal = collection.value[id];
if (!entitySignal) {
return { return {
id, id,
get value(){ get value() {
return collection.value[id]?.value; return undefined as unknown as T;
}, },
set value(value: T){ set value(_value: T) {}
const signal = collection.value[id]; } as EntityAccessor<T>;
if(signal)signal.value = value;
}
} }
return createReactiveAccessor(id, entitySignal);
} }
return { return {

View File

@ -1,5 +1,5 @@
import { describe, it, expect } from 'vitest'; import { describe, it, expect } from 'vitest';
import { createRule, dispatchCommand, type RuleContext, type RuleRegistry } from '../../src/core/rule'; import { createRule, type RuleContext } from '../../src/core/rule';
import { createGameContext } from '../../src/core/context'; import { createGameContext } from '../../src/core/context';
describe('Rule System', () => { describe('Rule System', () => {
@ -39,7 +39,7 @@ describe('Rule System', () => {
it('should invoke a registered rule and yield schema', () => { it('should invoke a registered rule and yield schema', () => {
const game = createTestGame(); const game = createTestGame();
game.rules.value.set('move', createRule('<from> <to>', function*(cmd) { game.registerRule('move', createRule('<from> <to>', function*(cmd) {
yield { name: '', params: [], options: [], flags: [] }; yield { name: '', params: [], options: [], flags: [] };
return { moved: cmd.params[0] }; return { moved: cmd.params[0] };
})); }));
@ -55,7 +55,7 @@ describe('Rule System', () => {
it('should complete a rule when final command matches yielded schema', () => { it('should complete a rule when final command matches yielded schema', () => {
const game = createTestGame(); const game = createTestGame();
game.rules.value.set('move', createRule('<from> <to>', function*(cmd) { game.registerRule('move', createRule('<from> <to>', function*(cmd) {
const confirm = yield { name: '', params: [], options: [], flags: [] }; const confirm = yield { name: '', params: [], options: [], flags: [] };
return { moved: cmd.params[0], confirmed: confirm.name === 'confirm' }; return { moved: cmd.params[0], confirmed: confirm.name === 'confirm' };
})); }));
@ -79,7 +79,7 @@ describe('Rule System', () => {
it('should pass the initial command to the generator', () => { it('should pass the initial command to the generator', () => {
const game = createTestGame(); const game = createTestGame();
game.rules.value.set('attack', createRule('<target> [--power: number]', function*(cmd) { game.registerRule('attack', createRule('<target> [--power: number]', function*(cmd) {
return { target: cmd.params[0], power: cmd.options.power || '1' }; return { target: cmd.params[0], power: cmd.options.power || '1' };
})); }));
@ -92,7 +92,7 @@ describe('Rule System', () => {
it('should complete immediately if generator does not yield', () => { it('should complete immediately if generator does not yield', () => {
const game = createTestGame(); const game = createTestGame();
game.rules.value.set('look', createRule('[--at]', function*() { game.registerRule('look', createRule('[--at]', function*() {
return 'looked'; return 'looked';
})); }));
@ -107,12 +107,12 @@ describe('Rule System', () => {
it('should prioritize new rule invocation over feeding yielded context', () => { it('should prioritize new rule invocation over feeding yielded context', () => {
const game = createTestGame(); const game = createTestGame();
game.rules.value.set('move', createRule('<from> <to>', function*(cmd) { game.registerRule('move', createRule('<from> <to>', function*(cmd) {
yield { name: '', params: [], options: [], flags: [] }; yield { name: '', params: [], options: [], flags: [] };
return { moved: cmd.params[0] }; return { moved: cmd.params[0] };
})); }));
game.rules.value.set('confirm', createRule('', function*() { game.registerRule('confirm', createRule('', function*() {
return 'new confirm rule'; return 'new confirm rule';
})); }));
@ -130,7 +130,7 @@ describe('Rule System', () => {
it('should feed a yielded context when command does not match any rule', () => { it('should feed a yielded context when command does not match any rule', () => {
const game = createTestGame(); const game = createTestGame();
game.rules.value.set('move', createRule('<from> <to>', function*(cmd) { game.registerRule('move', createRule('<from> <to>', function*(cmd) {
const response = yield { name: '', params: [], options: [], flags: [] }; const response = yield { name: '', params: [], options: [], flags: [] };
return { moved: cmd.params[0], response: response.name }; return { moved: cmd.params[0], response: response.name };
})); }));
@ -145,7 +145,7 @@ describe('Rule System', () => {
it('should skip non-matching commands for yielded context', () => { it('should skip non-matching commands for yielded context', () => {
const game = createTestGame(); const game = createTestGame();
game.rules.value.set('move', createRule('<from> <to>', function*(cmd) { game.registerRule('move', createRule('<from> <to>', function*(cmd) {
const response = yield '<item>'; const response = yield '<item>';
return { response: response.params[0] }; return { response: response.params[0] };
})); }));
@ -160,7 +160,7 @@ describe('Rule System', () => {
it('should validate command against yielded schema', () => { it('should validate command against yielded schema', () => {
const game = createTestGame(); const game = createTestGame();
game.rules.value.set('trade', createRule('<from> <to>', function*(cmd) { game.registerRule('trade', createRule('<from> <to>', function*(cmd) {
const response = yield '<item> [amount: number]'; const response = yield '<item> [amount: number]';
return { traded: response.params[0] }; return { traded: response.params[0] };
})); }));
@ -177,12 +177,12 @@ describe('Rule System', () => {
it('should feed the deepest yielded context', () => { it('should feed the deepest yielded context', () => {
const game = createTestGame(); const game = createTestGame();
game.rules.value.set('parent', createRule('<action>', function*() { game.registerRule('parent', createRule('<action>', function*() {
yield { name: '', params: [], options: [], flags: [] }; yield { name: '', params: [], options: [], flags: [] };
return 'parent done'; return 'parent done';
})); }));
game.rules.value.set('child', createRule('<target>', function*() { game.registerRule('child', createRule('<target>', function*() {
yield { name: '', params: [], options: [], flags: [] }; yield { name: '', params: [], options: [], flags: [] };
return 'child done'; return 'child done';
})); }));
@ -201,12 +201,12 @@ describe('Rule System', () => {
it('should link child to parent', () => { it('should link child to parent', () => {
const game = createTestGame(); const game = createTestGame();
game.rules.value.set('parent', createRule('<action>', function*() { game.registerRule('parent', createRule('<action>', function*() {
yield 'child_cmd'; yield 'child_cmd';
return 'parent done'; return 'parent done';
})); }));
game.rules.value.set('child_cmd', createRule('<target>', function*() { game.registerRule('child_cmd', createRule('<target>', function*() {
return 'child done'; return 'child done';
})); }));
@ -225,16 +225,16 @@ describe('Rule System', () => {
it('should discard previous children when a new child is invoked', () => { it('should discard previous children when a new child is invoked', () => {
const game = createTestGame(); const game = createTestGame();
game.rules.value.set('parent', createRule('<action>', function*() { game.registerRule('parent', createRule('<action>', function*() {
yield 'child_a | child_b'; yield 'child_a | child_b';
return 'parent done'; return 'parent done';
})); }));
game.rules.value.set('child_a', createRule('<target>', function*() { game.registerRule('child_a', createRule('<target>', function*() {
return 'child_a done'; return 'child_a done';
})); }));
game.rules.value.set('child_b', createRule('<target>', function*() { game.registerRule('child_b', createRule('<target>', function*() {
return 'child_b done'; return 'child_b done';
})); }));
@ -259,7 +259,7 @@ describe('Rule System', () => {
it('should track rule contexts in ruleContexts signal', () => { it('should track rule contexts in ruleContexts signal', () => {
const game = createTestGame(); const game = createTestGame();
game.rules.value.set('test', createRule('<arg>', function*() { game.registerRule('test', createRule('<arg>', function*() {
yield { name: '', params: [], options: [], flags: [] }; yield { name: '', params: [], options: [], flags: [] };
return 'done'; return 'done';
})); }));
@ -275,7 +275,7 @@ describe('Rule System', () => {
it('should add context to the context stack', () => { it('should add context to the context stack', () => {
const game = createTestGame(); const game = createTestGame();
game.rules.value.set('test', createRule('<arg>', function*() { game.registerRule('test', createRule('<arg>', function*() {
yield { name: '', params: [], options: [], flags: [] }; yield { name: '', params: [], options: [], flags: [] };
return 'done'; return 'done';
})); }));
@ -292,7 +292,7 @@ describe('Rule System', () => {
it('should leave context in place when generator throws', () => { it('should leave context in place when generator throws', () => {
const game = createTestGame(); const game = createTestGame();
game.rules.value.set('failing', createRule('<arg>', function*() { game.registerRule('failing', createRule('<arg>', function*() {
throw new Error('rule error'); throw new Error('rule error');
})); }));
@ -304,12 +304,12 @@ describe('Rule System', () => {
it('should leave children in place when child generator throws', () => { it('should leave children in place when child generator throws', () => {
const game = createTestGame(); const game = createTestGame();
game.rules.value.set('parent', createRule('<action>', function*() { game.registerRule('parent', createRule('<action>', function*() {
yield 'child'; yield 'child';
return 'parent done'; return 'parent done';
})); }));
game.rules.value.set('child', createRule('<target>', function*() { game.registerRule('child', createRule('<target>', function*() {
throw new Error('child error'); throw new Error('child error');
})); }));
@ -331,7 +331,7 @@ describe('Rule System', () => {
flags: [], flags: [],
}; };
game.rules.value.set('test', createRule('<arg>', function*() { game.registerRule('test', createRule('<arg>', function*() {
const cmd = yield customSchema; const cmd = yield customSchema;
return { received: cmd.params[0] }; return { received: cmd.params[0] };
})); }));
@ -346,7 +346,7 @@ describe('Rule System', () => {
it('should parse string schema on each yield', () => { it('should parse string schema on each yield', () => {
const game = createTestGame(); const game = createTestGame();
game.rules.value.set('multi', createRule('<start>', function*() { game.registerRule('multi', createRule('<start>', function*() {
const a = yield '<value>'; const a = yield '<value>';
const b = yield '<value>'; const b = yield '<value>';
return { a: a.params[0], b: b.params[0] }; return { a: a.params[0], b: b.params[0] };
@ -365,7 +365,7 @@ describe('Rule System', () => {
it('should handle a multi-step game flow', () => { it('should handle a multi-step game flow', () => {
const game = createTestGame(); const game = createTestGame();
game.rules.value.set('start', createRule('<player>', function*(cmd) { game.registerRule('start', createRule('<player>', function*(cmd) {
const player = cmd.params[0]; const player = cmd.params[0];
const action = yield { name: '', params: [], options: [], flags: [] }; const action = yield { name: '', params: [], options: [], flags: [] };