89 lines
2.3 KiB
TypeScript
89 lines
2.3 KiB
TypeScript
import {Context} from "./context";
|
|
import {Command} from "../utils/command";
|
|
import {effect} from "@preact/signals-core";
|
|
|
|
export type RuleContext<T> = Context & {
|
|
actions: Command[];
|
|
handledActions: number;
|
|
invocations: RuleContext<unknown>[];
|
|
resolution?: T;
|
|
}
|
|
|
|
/**
|
|
* 调用规则生成器并管理其上下文
|
|
* @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 !== undefined) {
|
|
dispose();
|
|
return;
|
|
}
|
|
executeRule();
|
|
});
|
|
|
|
// 将规则上下文推入栈中
|
|
pushContext(ctx);
|
|
|
|
return ctx;
|
|
}
|
|
|
|
/**
|
|
* 创建一个规则生成器辅助函数
|
|
* @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,
|
|
});
|
|
}
|