refactor: fix reactivity

This commit is contained in:
2026-04-01 22:57:33 +08:00
parent 4761806a02
commit dbd2a25185
4 changed files with 102 additions and 39 deletions
+25 -1
View File
@@ -2,7 +2,7 @@ import {createModel, Signal, signal} from '@preact/signals-core';
import {createEntityCollection} from "../utils/entity";
import {Part} from "./part";
import {Region} from "./region";
import {RuleRegistry, RuleContext, dispatchCommand as dispatchRuleCommand} from "./rule";
import {RuleDef, RuleRegistry, RuleContext, dispatchCommand as dispatchRuleCommand} from "./rule";
export type Context = {
type: string;
@@ -37,11 +37,33 @@ export const GameContext = createModel((root: Context) => {
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) {
return dispatchRuleCommand({
rules: rules.value,
ruleContexts: ruleContexts.value,
contexts,
addRuleContext,
removeRuleContext,
}, input);
}
@@ -54,6 +76,8 @@ export const GameContext = createModel((root: Context) => {
pushContext,
popContext,
latestContext,
registerRule,
unregisterRule,
dispatchCommand,
}
})
+4 -3
View File
@@ -38,13 +38,12 @@ function parseYieldedSchema(value: string | CommandSchema): CommandSchema {
function pushContextToGame(game: GameContextLike, ctx: RuleContext<unknown>) {
game.contexts.value = [...game.contexts.value, { value: ctx } as any];
game.ruleContexts.push(ctx);
game.addRuleContext(ctx);
}
function discardChildren(game: GameContextLike, parent: RuleContext<unknown>) {
for (const child of parent.children) {
const idx = game.ruleContexts.indexOf(child);
if (idx !== -1) game.ruleContexts.splice(idx, 1);
game.removeRuleContext(child);
const ctxIdx = game.contexts.value.findIndex((c: any) => c.value === child);
if (ctxIdx !== -1) {
@@ -161,4 +160,6 @@ type GameContextLike = {
rules: RuleRegistry;
ruleContexts: RuleContext<unknown>[];
contexts: { value: any[] };
addRuleContext: (ctx: RuleContext<unknown>) => void;
removeRuleContext: (ctx: RuleContext<unknown>) => void;
};
+48 -10
View File
@@ -9,6 +9,43 @@ export type EntityAccessor<T extends Entity> = {
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>() {
const collection = signal({} as Record<string, Signal<T>>);
const remove = (...ids: string[]) => {
@@ -24,17 +61,18 @@ export function createEntityCollection<T extends Entity>() {
};
};
const get = (id: string) => {
return {
id,
get value(){
return collection.value[id]?.value;
},
set value(value: T){
const signal = collection.value[id];
if(signal)signal.value = value;
}
const get = (id: string): EntityAccessor<T> => {
const entitySignal = collection.value[id];
if (!entitySignal) {
return {
id,
get value() {
return undefined as unknown as T;
},
set value(_value: T) {}
} as EntityAccessor<T>;
}
return createReactiveAccessor(id, entitySignal);
}
return {