This commit is contained in:
Lobo 2026-02-02 13:56:16 -03:00
parent 4990c6d26a
commit 9830065f5c
4 changed files with 50 additions and 21 deletions

55
main.go
View file

@ -102,6 +102,41 @@ func (app *App) EnsureConfigDir() error {
return nil
}
func (app *App) Error(err error) {
if app.conn != nil {
host := app.conn.host
port := app.conn.port
app.conn.stop()
app.conn.Close()
if app.conn.ticker != nil {
app.conn.ticker.Stop()
}
app.conn = nil
app.incoming <- SystemMessageEvent(fmt.Sprintf("disconnected from %s:%s: %v", host, port, err))
} else {
app.incoming <- SystemMessageEvent(fmt.Sprintf("error: %v", err))
}
}
func (app *App) Run() {
for {
select {
case ev := <-app.outgoing:
_, dial := ev.(DialEvent)
if dial || app.conn != nil {
if err := ev.HandleOutgoing(app); err != nil {
app.Error(err)
continue
}
} else {
app.incoming <- SystemMessageEvent("not connected to any server")
}
case <-app.ctx.Done():
return
}
}
}
func NewApp() *App {
app := &App{}
app.ctx, app.stop = context.WithCancel(context.Background())
@ -117,25 +152,7 @@ func NewApp() *App {
panic(err)
}
go func() {
for {
select {
case ev := <-app.outgoing:
_, dial := ev.(DialEvent)
if dial || app.conn != nil {
if err := ev.HandleOutgoing(app); err != nil {
app.error <- err
return
}
} else {
app.incoming <- SystemMessageEvent("not connected to any server")
}
case <-app.ctx.Done():
return
}
}
}()
go app.Run()
app.InitUI()
return app