~adnano/astronaut

728e09564e7a84dd8ebc2e0c8841ee3e38c0c24b — Adnan Maolood 1 year, 8 months ago 67db381
tab: Improve duplicate page removal
1 files changed, 11 insertions(+), 15 deletions(-)

M tab.go
M tab.go => tab.go +11 -15
@@ 134,6 134,17 @@ func (t *Tab) setText(text *Text) {
	}
	page.Text = text
	t.view.Invalidate()

	// Remove duplicate pages
	if len(t.pages) < 2 {
		return
	}
	i, j := len(t.pages)-1, len(t.pages)-2
	if t.pages[i].URL == t.pages[j].URL {
		t.pages[j] = t.pages[i]
		t.pages = t.pages[:i]
		t.page--
	}
}

// Clone returns a new tab with the same URL and navigation history.


@@ 417,7 428,6 @@ func (t *Tab) do(ctx context.Context, req *gemini.Request, via []*gemini.Request
		return nil

	case gemini.StatusSuccess:
		t.removeDuplicatePage()
		t.buf.Reset()
		resp.Body = &teeReader{resp.Body, &t.buf}
		return t.handle(req, resp)


@@ 623,20 633,6 @@ func (t *Tab) newPage() {
	t.pages = append(t.pages, Page{})
}

func (t *Tab) removeDuplicatePage() {
	t.mu.Lock()
	defer t.mu.Unlock()
	if len(t.pages) < 2 {
		return
	}
	i, j := len(t.pages)-1, len(t.pages)-2
	if t.pages[i].URL == t.pages[j].URL {
		t.pages[j] = t.pages[i]
		t.pages = t.pages[:i]
		t.page--
	}
}

func (t *Tab) handle(req *gemini.Request, resp *gemini.Response) error {
	defer resp.Body.Close()