refactor event systems

This commit is contained in:
adman
2026-03-08 11:52:25 +01:00
parent 73d4e07b06
commit 2eee7bde24
12 changed files with 100 additions and 33 deletions
+51
View File
@@ -0,0 +1,51 @@
package event
import (
"bufio"
"context"
"os"
)
type Stdin struct {
Val string
}
type StdinHandler struct {
*Loop
stdInChan chan Stdin
ctx context.Context
cancel context.CancelFunc
}
func (s *StdinHandler) OnTick(_ Tick) (err error) {
select {
case stdin := <-s.stdInChan:
err = s.Fire(Stdin{stdin.Val})
default:
}
return
}
func (s *StdinHandler) ScanJob() {
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
select {
case <-s.ctx.Done():
return
default:
s.stdInChan <- Stdin{scanner.Text()}
}
}
}
func (s *StdinHandler) OnStart(_ Start) (err error) {
s.stdInChan = make(chan Stdin)
s.ctx, s.cancel = context.WithCancel(context.Background())
go s.ScanJob()
return
}
func (s *StdinHandler) OnClose(_ Close) (err error) {
s.cancel()
return
}