termtap/internal/app/session.go
Hayden Hargreaves e5be0dd17b feat: process are actually dying right now :)
I did not need to spawn them in a go routine.
2026-04-14 22:31:10 -07:00

54 lines
860 B
Go

package app
import (
"sync"
"syscall"
"termtap.dev/internal/model"
"termtap.dev/internal/proxy"
)
type Session struct {
Messages <-chan model.Message
msgCh chan model.Message
proxy *model.ProxyServer
proc *model.Process
stopOnce sync.Once
}
func StartSession(cmd model.Command, addr string) (*Session, error) {
msgs := make(chan model.Message, 256)
ps, err := proxy.NewProxyServer(addr, msgs)
if err != nil {
return nil, err
}
go StartProxy(ps, msgs)
proc, err := StartProcess(cmd, addr, msgs)
if err != nil {
proxy.Destory(ps, msgs)
return nil, err
}
return &Session{
Messages: msgs,
msgCh: msgs,
proxy: ps,
proc: proc,
}, nil
}
func (s *Session) Stop() {
if s == nil {
return
}
s.stopOnce.Do(func() {
StopProcess(s.proc, s.msgCh, syscall.SIGTERM)
proxy.Destory(s.proxy, s.msgCh)
})
}