Skip to content
This repository has been archived by the owner on May 31, 2024. It is now read-only.

Commit

Permalink
fix: update page before checking prefetch condition
Browse files Browse the repository at this point in the history
Signed-off-by: zychen5186 <[email protected]>
  • Loading branch information
zychen5186 committed Apr 30, 2024
1 parent 3fea44f commit 3489e71
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
34 changes: 22 additions & 12 deletions pkg/bubbletea/bubbletea_pagination.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,29 +53,38 @@ func (m pageModel) Init() tea.Cmd {

func (m pageModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd

switch msg := msg.(type) {
case spinner.TickMsg:
m.spinner, cmd = m.spinner.Update(msg)
return m, cmd
case tea.KeyMsg:
switch {
case key.Matches(msg, m.paginator.KeyMap.PrevPage):
// If current page will be out of the range of the first batch, don't update
if m.paginator.Page == firstBatchIndex*pagePerBatch {
return m, cmd
}
}
}

m.paginator, _ = m.paginator.Update(msg)

switch msg := msg.(type) {

case tea.KeyMsg:
switch msg.String() {
case "q", "esc", "ctrl+c":
return m, tea.Quit
}
switch {
case key.Matches(msg, m.paginator.KeyMap.NextPage):
// If no more data, don't fetch again (won't show spinner)
value, ok := batchLen[lastBatchIndex+1]
if !ok || value != 0 {
if (m.paginator.Page >= (lastBatchIndex+1)*pagePerBatch-prefetchThreshold) && !fetchingForward {
if (m.paginator.Page >= (lastBatchIndex+1)*pagePerBatch-prefetchThreshold) && !fetchingForward {
// If no more data, don't fetch again (won't show spinner)
value, ok := batchLen[lastBatchIndex+1]
if !ok || value != 0 {
fetchingForward = true
cmd = fetchDataCmd(lastBatchIndex+1, forward)
}
}
case key.Matches(msg, m.paginator.KeyMap.PrevPage):
if m.paginator.Page-firstBatchIndex*pagePerBatch == 0 {
return m, cmd
}
if (m.paginator.Page <= firstBatchIndex*pagePerBatch+prefetchThreshold) && (firstBatchIndex > 0) && !fetchingBackward {
fetchingBackward = true
cmd = fetchDataCmd(firstBatchIndex-1, backward)
Expand Down Expand Up @@ -109,10 +118,11 @@ func (m pageModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}
m.paginator.SetTotalPages(countTotalPages())
return m, nil
case spinner.TickMsg:
m.spinner, cmd = m.spinner.Update(msg)
return m, cmd
}

m.paginator, _ = m.paginator.Update(msg)

return m, cmd
}

Expand Down
13 changes: 8 additions & 5 deletions pkg/bubbletea/bubbletea_pagination_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"strings"
"sync"
"time"

tea "github.com/charmbracelet/bubbletea"
"github.com/flyteorg/flytectl/pkg/filters"
Expand All @@ -24,7 +25,7 @@ const (
msgPerPage = 10
pagePerBatch = msgPerBatch / msgPerPage
prefetchThreshold = pagePerBatch - 1
localBatchLimit = 10 // Please set localBatchLimit at least 2
localBatchLimit = 2 // Please set localBatchLimit at least 2
)

var (
Expand Down Expand Up @@ -99,7 +100,7 @@ func getMessageList(batchIndex int) []proto.Message {
spin = false
mutex.Unlock()
}()

time.Sleep(2 * time.Second)
msg := callback(filters.Filters{
Limit: msgPerBatch,
Page: int32(batchIndex + 1),
Expand All @@ -112,18 +113,20 @@ func getMessageList(batchIndex int) []proto.Message {
return msg
}

type direction int

const (
forward int = iota
forward direction = iota
backward
)

type newDataMsg struct {
newItems []proto.Message
batchIndex int
fetchDirection int
fetchDirection direction
}

func fetchDataCmd(batchIndex int, fetchDirection int) tea.Cmd {
func fetchDataCmd(batchIndex int, fetchDirection direction) tea.Cmd {
return func() tea.Msg {
msg := newDataMsg{
newItems: getMessageList(batchIndex),
Expand Down

0 comments on commit 3489e71

Please sign in to comment.