pub struct NodeContext<'a, S: WorkflowState = State> { /* private fields */ }Expand description
节点上下文 — Runtime Handle(运行时句柄)。
§泛型参数
S— 类型化状态(默认State= HashMap,向后兼容)
§设计原则
NodeContext 是 Runtime Handle,不是 Runtime State。
- 节点只借用,不拥有。零复制透传给子组件
- 禁止放入:RuntimeEventEmitter、TraceId、SpanId、GraphHandle、ExecutorConfig
§Effects 缓冲(v0.4+)
effects 字段收集节点产生的 Effect(领域事件),
供上层(如 Executor)统一 apply 到 Typed State。
Implementations§
Source§impl<'a, S: WorkflowState> NodeContext<'a, S>
impl<'a, S: WorkflowState> NodeContext<'a, S>
Sourcepub fn new(
state: &'a mut S,
branch: &'a mut BranchState,
stream: Option<&'a dyn StreamSink>,
cancel: CancellationToken,
) -> Self
pub fn new( state: &'a mut S, branch: &'a mut BranchState, stream: Option<&'a dyn StreamSink>, cancel: CancellationToken, ) -> Self
创建新的 NodeContext。
Sourcepub fn branch(&self) -> &BranchState
pub fn branch(&self) -> &BranchState
获取底层 BranchState 引用(用于 fork 等操作)。
Sourcepub fn branch_mut(&mut self) -> &mut BranchState
pub fn branch_mut(&mut self) -> &mut BranchState
获取底层 BranchState 可变引用。
Sourcepub fn emit(&self, chunk: StreamChunk)
pub fn emit(&self, chunk: StreamChunk)
发射数据面事件(无 stream 则静默丢弃)。
Sourcepub fn emit_flow_event(&mut self, event: FlowEvent)
pub fn emit_flow_event(&mut self, event: FlowEvent)
发射控制面 FlowEvent(缓冲到 NodeContext,供 Executor 收集转发)。
Sourcepub fn is_cancelled(&self) -> bool
pub fn is_cancelled(&self) -> bool
检查是否已取消。
Sourcepub fn cancel_token(&self) -> &CancellationToken
pub fn cancel_token(&self) -> &CancellationToken
获取取消令牌引用。
Sourcepub fn set_token_cost(&mut self, cost: f64)
pub fn set_token_cost(&mut self, cost: f64)
设置 token 成本。
Sourcepub fn set_has_side_effects(&mut self)
pub fn set_has_side_effects(&mut self)
标记有副作用。
Sourcepub fn emit_effect(&mut self, effect: S::Effect)
pub fn emit_effect(&mut self, effect: S::Effect)
发射一个 Effect(强类型领域事件)到缓冲。
零序列化开销 — 直接存储 S::Effect。
Sourcepub fn consume_effects(&mut self) -> Vec<S::Effect>
pub fn consume_effects(&mut self) -> Vec<S::Effect>
消费 Effect 缓冲(返回所有收集的 Effect)。
Sourcepub fn effects_len(&self) -> usize
pub fn effects_len(&self) -> usize
获取已收集的 Effect 数量(不消费)。
Sourcepub fn take_control(&mut self) -> (NextAction, Option<ExecutionSignal>)
pub fn take_control(&mut self) -> (NextAction, Option<ExecutionSignal>)
消费控制信号(Executor 调用)。
Sourcepub fn take_metadata(&mut self) -> NodeMetadata
pub fn take_metadata(&mut self) -> NodeMetadata
获取元数据(Executor 调用)。
Sourcepub fn stream(&self) -> Option<&'a dyn StreamSink>
pub fn stream(&self) -> Option<&'a dyn StreamSink>
获取数据面发射器引用。
Sourcepub fn take_flow_events(&mut self) -> Vec<FlowEvent>
pub fn take_flow_events(&mut self) -> Vec<FlowEvent>
消费 FlowEvent 缓冲(Executor 调用)。
Auto Trait Implementations§
impl<'a, S = State> !RefUnwindSafe for NodeContext<'a, S>
impl<'a, S = State> !UnwindSafe for NodeContext<'a, S>
impl<'a, S> Freeze for NodeContext<'a, S>
impl<'a, S> Send for NodeContext<'a, S>
impl<'a, S> Sync for NodeContext<'a, S>
impl<'a, S> Unpin for NodeContext<'a, S>
impl<'a, S> UnsafeUnpin for NodeContext<'a, S>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more