Refactors the event emitter system from string-based messages to a type-safe generic implementation using typed events. Consolidates separate tracker packages into a unified plugins architecture. Changes: - Replace Emitter with BaseEmitter[E comparable] using generics - Add Event type with StartEvent, StepEvent, and StopEvent constants - Create Listener[E] interface with BaseListener implementation - Consolidate explanation, performance, and statistics trackers into internal/plugins package - Simplify main CLI by using plugin constructors instead of manual event subscription - Add Items() iterator method to Set for idiomatic range loops
45 lines
845 B
Go
45 lines
845 B
Go
package plugins
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"time"
|
|
|
|
"git.maximhutz.com/max/lambda/internal/engine"
|
|
"git.maximhutz.com/max/lambda/internal/statistics"
|
|
)
|
|
|
|
// An observer, to track reduction performance.
|
|
type Statistics struct {
|
|
start time.Time
|
|
steps uint64
|
|
}
|
|
|
|
// Create a new reduction performance Statistics.
|
|
func NewStatistics(process *engine.Engine) *Statistics {
|
|
plugin := &Statistics{}
|
|
process.On(engine.StartEvent, plugin.Start)
|
|
process.On(engine.StepEvent, plugin.Step)
|
|
process.On(engine.StopEvent, plugin.Step)
|
|
|
|
return plugin
|
|
}
|
|
|
|
func (t *Statistics) Start() {
|
|
t.start = time.Now()
|
|
t.steps = 0
|
|
}
|
|
|
|
func (t *Statistics) Step() {
|
|
t.steps++
|
|
}
|
|
|
|
func (t *Statistics) Stop() {
|
|
results := statistics.Results{
|
|
StepsTaken: t.steps,
|
|
TimeElapsed: uint64(time.Since(t.start).Milliseconds()),
|
|
}
|
|
|
|
fmt.Fprint(os.Stderr, results.String())
|
|
}
|