Skip to content

Commit

Permalink
Don't use a goroutine for waiting on receive code
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacalz committed Nov 1, 2023
1 parent c630a12 commit a430639
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 14 deletions.
17 changes: 7 additions & 10 deletions internal/transport/bridge/recv.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ func (r *RecvItem) failed() {
r.refresh(r.index)
}

func (r *RecvItem) setPath(path string) {
r.URI = storage.NewFileURI(path)
r.Name = r.URI.Name()
r.refresh(r.index)
}

// RecvData is a list of progress bars that track send progress.
type RecvData struct {
Client *transport.Client
Expand Down Expand Up @@ -141,17 +147,8 @@ func (d *RecvData) NewReceive(code string) {
item := d.NewRecv(code)
d.list.Refresh()

path := make(chan string)

go func() {
item.URI = storage.NewFileURI(<-path)
item.Name = item.URI.Name()
close(path)
d.refresh(item.index)
}()

go func(code string) {
if err := d.Client.NewReceive(code, path, item.update); err != nil {
if err := d.Client.NewReceive(code, item.setPath, item.update); err != nil {
d.Client.ShowNotification("Receive failed", "An error occurred when receiving the data.")
item.failed()
dialog.ShowError(err, d.Window)
Expand Down
7 changes: 3 additions & 4 deletions internal/transport/receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,17 @@ func bail(msg *wormhole.IncomingMessage, err error) error {
}

// NewReceive runs a receive using wormhole-william and handles types accordingly.
func (c *Client) NewReceive(code string, pathname chan string, progress func(int64, int64)) (err error) {
func (c *Client) NewReceive(code string, setPath func(string), progress func(int64, int64)) (err error) {
msg, err := c.Receive(context.Background(), code)
if err != nil {
pathname <- "" // We want to always send a URI, even on fail, in order to not block goroutines.
fyne.LogError("Error on receiving data", err)
return bail(msg, err)
}

contents := util.NewProgressReader(msg, progress, msg.TransferBytes64)

if msg.Type == wormhole.TransferText {
pathname <- "Text Snippet"
setPath("Text Snippet")

text := make([]byte, int(msg.TransferBytes64))
_, err := io.ReadFull(contents, text)
Expand All @@ -52,7 +51,7 @@ func (c *Client) NewReceive(code string, pathname chan string, progress func(int
}

path := filepath.Join(c.DownloadPath, msg.Name)
pathname <- path
setPath(path)

if !c.OverwriteExisting {
if _, err := os.Stat(path); err == nil || os.IsExist(err) {
Expand Down

0 comments on commit a430639

Please sign in to comment.