Handler

Trait Handler 

Source
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));
            }
            _ => {}
        }
    }
}

Required Associated Types§

Source

type Scope: Default

Handler固有のスコープ型

ブロック開始時にDefault::default()で生成され、 ブロック終了時に破棄されます。

Required Methods§

Source

fn on_event(&mut self, scope: &mut Self::Scope, event: &K::Event)

イベントを処理する

Implementors§