summaryrefslogtreecommitdiff
path: root/app.go
diff options
context:
space:
mode:
authorHubert Hirtz <hubert@hirtz.pm>2021-11-16 09:20:21 +0100
committerHubert Hirtz <hubert@hirtz.pm>2021-11-16 22:22:10 +0100
commitd40d8dc36a608a1349f8353c50c2c71649e6fa75 (patch)
treed43e01ac710680c5e765e4726ff8928ec2431499 /app.go
parentDon't merge message bounds from multiple networks (diff)
Allow App.Close() and App.Run() to be run concurrently
Diffstat (limited to 'app.go')
-rw-r--r--app.go34
1 files changed, 21 insertions, 13 deletions
diff --git a/app.go b/app.go
index acbe32b..237d606 100644
--- a/app.go
+++ b/app.go
@@ -137,7 +137,11 @@ func NewApp(cfg Config) (app *App, err error) {
}
func (app *App) Close() {
- app.win.Close()
+ app.win.Exit() // tell all instances of app.ircLoop to stop when possible
+ app.events <- event{ // tell app.eventLoop to stop
+ src: "*",
+ content: nil,
+ }
for _, session := range app.sessions {
session.Close()
}
@@ -166,6 +170,8 @@ func (app *App) CurrentBuffer() (netID, buffer string) {
// eventLoop retrieves events (in batches) from the event channel and handle
// them, then draws the interface after each batch is handled.
func (app *App) eventLoop() {
+ defer app.win.Close()
+
evs := make([]event, 0, eventChanSize)
for !app.win.ShouldExit() {
ev := <-app.events
@@ -181,7 +187,17 @@ func (app *App) eventLoop() {
}
}
- app.handleEvents(evs)
+ for _, ev := range evs {
+ if ev.src == "*" {
+ if ev.content == nil {
+ return
+ }
+ app.handleUIEvent(ev.content)
+ } else {
+ app.handleIRCEvent(ev.src, ev.content)
+ }
+ }
+
if !app.pasting {
app.setStatus()
app.updatePrompt()
@@ -255,6 +271,9 @@ func (app *App) ircLoop(netID string) {
HeadColor: tcell.ColorRed,
Body: ui.PlainString("Connection lost"),
})
+ if app.win.ShouldExit() {
+ break
+ }
time.Sleep(10 * time.Second)
}
}
@@ -340,17 +359,6 @@ func (app *App) uiLoop() {
}
}
-// handleEvents handles a batch of events.
-func (app *App) handleEvents(evs []event) {
- for _, ev := range evs {
- if ev.src == "*" {
- app.handleUIEvent(ev.content)
- } else {
- app.handleIRCEvent(ev.src, ev.content)
- }
- }
-}
-
func (app *App) handleUIEvent(ev interface{}) {
switch ev := ev.(type) {
case *tcell.EventResize: