import {Context} from "./context"; import {Command} from "../utils/command"; import {effect} from "@preact/signals-core"; export type RuleContext = Context & { actions: Command[]; handledActions: number; invocations: RuleContext[]; resolution?: T; } /** * 调用规则生成器并管理其上下文 * @param pushContext - 用于推送上下文到上下文栈的函数 * @param type - 规则类型 * @param rule - 规则生成器函数 * @returns 规则执行结果 */ export function invokeRuleContext( pushContext: (context: Context) => void, type: string, rule: Generator ): RuleContext { const ctx: RuleContext = { type, actions: [], handledActions: 0, invocations: [], resolution: undefined, }; let disposed = false; const executeRule = () => { if (disposed || ctx.resolution !== undefined) return; try { const result = rule.next(); if (result.done) { ctx.resolution = result.value; return; } const actionType = result.value; if (actionType) { // 暂停于 yield 点,等待外部处理动作 // 当外部更新 actions 后,effect 会重新触发 } } catch (error) { throw error; } }; const dispose = effect(() => { if (ctx.resolution !== undefined) { dispose(); disposed = true; return; } executeRule(); }); pushContext(ctx); return ctx; } /** * 创建一个规则生成器辅助函数 * @param type - 规则类型 * @param fn - 规则逻辑函数 */ export function createRule( type: string, fn: (ctx: RuleContext) => Generator ): (ctx: RuleContext) => Generator { return fn; }