fix multithreading crashes.

This commit is contained in:
fiatjaf
2025-11-28 23:14:25 -03:00
parent 529d24566c
commit a2cf1a5f29
2 changed files with 18 additions and 7 deletions
+2 -1
View File
@@ -16,6 +16,7 @@ import (
)
var (
app *qt.QApplication
window *qt.QMainWindow
currentSec nostr.SecretKey
@@ -27,7 +28,7 @@ var (
)
func main() {
qt.NewQApplication(os.Args)
app = qt.NewQApplication(os.Args)
window = qt.NewQMainWindow2()
+16 -6
View File
@@ -9,6 +9,7 @@ import (
"fiatjaf.com/nostr"
qt "github.com/mappu/miqt/qt6"
"github.com/mappu/miqt/qt6/mainthread"
)
type reqVars struct {
@@ -357,7 +358,9 @@ func (req reqVars) subscribe() {
go func() {
reason := <-sub.ClosedReason
time.Sleep(time.Second)
statusLabel.SetText(fmt.Sprintf("subscription closed: %s", reason))
mainthread.Wait(func() {
statusLabel.SetText(fmt.Sprintf("subscription closed: %s", reason))
})
}()
} else {
statusLabel.SetText("subscribed to " + strings.Join(niceRelayURLs(relays), ", "))
@@ -378,13 +381,20 @@ func (req reqVars) subscribe() {
// collect events
eosed := false
go func() {
first := true
for event := range eventsChan {
jsonBytes, _ := json.Marshal(event)
if eosed {
req.resultsEdit.SetPlainText(string(jsonBytes) + "\n" + req.resultsEdit.ToPlainText())
} else {
req.resultsEdit.InsertPlainText("\n" + string(jsonBytes))
}
mainthread.Wait(func() {
if eosed {
req.resultsEdit.SetPlainText(string(jsonBytes) + "\n" + req.resultsEdit.ToPlainText())
} else {
if !first {
req.resultsEdit.InsertPlainText("\n")
}
req.resultsEdit.InsertPlainText(string(jsonBytes))
}
first = false
})
}
statusLabel.SetText("subscription ended")
}()