pub trait Handler<K: Kind> {
type Scope: Default;
// Required method
fn on_event(&mut self, scope: &mut Self::Scope, event: &K::Event);
}Expand description
イベントを処理するハンドラトレイト
特定のKindに対するイベント処理を定義します。
Scopeはブロックのライフサイクル中に保持される状態です。
§Examples
ⓘ
use llm_worker::timeline::{Handler, TextBlockEvent, TextBlockKind};
struct TextCollector {
texts: Vec<String>,
}
impl Handler<TextBlockKind> for TextCollector {
type Scope = String; // ブロックごとのバッファ
fn on_event(&mut self, buffer: &mut String, event: &TextBlockEvent) {
match event {
TextBlockEvent::Delta(text) => buffer.push_str(text),
TextBlockEvent::Stop(_) => {
self.texts.push(std::mem::take(buffer));
}
_ => {}
}
}
}