Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

chore: add mutex on tables #28

Merged
merged 1 commit into from
Aug 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions pkg/display/all_rounds_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package display
import (
"main/pkg/types"
"strconv"
"sync"

"github.com/gdamore/tcell/v2"

Expand All @@ -17,6 +18,7 @@ type AllRoundsTableData struct {
Transpose bool

cells [][]*tview.TableCell
mutex sync.Mutex
}

func NewAllRoundsTableData(disableEmojis bool, transpose bool) *AllRoundsTableData {
Expand All @@ -29,6 +31,9 @@ func NewAllRoundsTableData(disableEmojis bool, transpose bool) *AllRoundsTableDa
}

func (d *AllRoundsTableData) GetCell(row, column int) *tview.TableCell {
d.mutex.Lock()
defer d.mutex.Unlock()

if len(d.cells) <= row {
return nil
}
Expand All @@ -41,10 +46,16 @@ func (d *AllRoundsTableData) GetCell(row, column int) *tview.TableCell {
}

func (d *AllRoundsTableData) GetRowCount() int {
d.mutex.Lock()
defer d.mutex.Unlock()

return len(d.cells)
}

func (d *AllRoundsTableData) GetColumnCount() int {
d.mutex.Lock()
defer d.mutex.Unlock()

if len(d.cells) == 0 {
return 0
}
Expand All @@ -67,6 +78,9 @@ func (d *AllRoundsTableData) SetTranspose(transpose bool) {
}

func (d *AllRoundsTableData) redrawCells() {
d.mutex.Lock()
defer d.mutex.Unlock()

d.cells = make([][]*tview.TableCell, len(d.Validators.Validators)+1)

for row := 0; row < len(d.Validators.Validators)+1; row++ {
Expand Down
14 changes: 14 additions & 0 deletions pkg/display/last_round_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package display

import (
"main/pkg/types"
"sync"

"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
Expand All @@ -16,6 +17,7 @@ type LastRoundTableData struct {
Transpose bool

cells [][]*tview.TableCell
mutex sync.Mutex
}

func NewLastRoundTableData(columnsCount int, disableEmojis bool, transpose bool) *LastRoundTableData {
Expand All @@ -40,6 +42,9 @@ func (d *LastRoundTableData) SetTranspose(transpose bool) {
}

func (d *LastRoundTableData) GetCell(row, column int) *tview.TableCell {
d.mutex.Lock()
defer d.mutex.Unlock()

if len(d.cells) <= row {
return nil
}
Expand All @@ -52,10 +57,16 @@ func (d *LastRoundTableData) GetCell(row, column int) *tview.TableCell {
}

func (d *LastRoundTableData) GetRowCount() int {
d.mutex.Lock()
defer d.mutex.Unlock()

return len(d.cells)
}

func (d *LastRoundTableData) GetColumnCount() int {
d.mutex.Lock()
defer d.mutex.Unlock()

if len(d.cells) == 0 {
return 0
}
Expand All @@ -69,6 +80,9 @@ func (d *LastRoundTableData) SetValidators(validators types.ValidatorsWithInfo)
}

func (d *LastRoundTableData) redrawData() {
d.mutex.Lock()
defer d.mutex.Unlock()

rowsCount := len(d.Validators)/d.ColumnsCount + 1
if len(d.Validators)%d.ColumnsCount == 0 {
rowsCount = len(d.Validators) / d.ColumnsCount
Expand Down
Loading