feat: rng & commands impl & tests

This commit is contained in:
hyper
2026-04-01 17:34:21 +08:00
parent ea337acacb
commit df9698b67b
10 changed files with 835 additions and 221 deletions
+63 -7
View File
@@ -9,24 +9,80 @@ export type RuleContext<T> = Context & {
resolution?: T;
}
function invokeRuleContext<T>(pushContext: (context: Context) => void, type: string, rule: Generator<string, T, Command>){
/**
* 调用规则生成器并管理其上下文
* @param pushContext - 用于推送上下文到上下文栈的函数
* @param type - 规则类型
* @param rule - 规则生成器函数
* @returns 规则执行结果
*/
export function invokeRuleContext<T>(
pushContext: (context: Context) => void,
type: string,
rule: Generator<string, T, Command>
): RuleContext<T> {
const ctx: RuleContext<T> = {
type,
actions: [],
handledActions: 0,
invocations: [],
resolution: undefined,
}
};
// 执行生成器直到完成或需要等待动作
const executeRule = () => {
try {
const result = rule.next();
if (result.done) {
// 规则执行完成,设置结果
ctx.resolution = result.value;
return;
}
// 如果生成器 yield 了一个动作类型,等待处理
// 这里可以扩展为实际的动作处理逻辑
const actionType = result.value;
// 继续执行直到有动作需要处理或规则完成
if (!result.done) {
executeRule();
}
} catch (error) {
// 规则执行出错,抛出错误
throw error;
}
};
// 使用 effect 来跟踪响应式依赖
const dispose = effect(() => {
if(ctx.resolution) {
if (ctx.resolution !== undefined) {
dispose();
return;
}
executeRule();
});
pushContext(rule);
// 将规则上下文推入栈中
pushContext(ctx);
return ctx;
}
function* rule(){
const play: Command = yield 'play';
}
/**
* 创建一个规则生成器辅助函数
* @param type - 规则类型
* @param fn - 规则逻辑函数
*/
export function createRule<T>(
type: string,
fn: (ctx: RuleContext<T>) => Generator<string, T, Command>
): Generator<string, T, Command> {
return fn({
type,
actions: [],
handledActions: 0,
invocations: [],
resolution: undefined,
});
}