Skip to content

Commit 2fc3726

Browse files
implemented cancelation and contexts... very minor change XD
1 parent 4cb248c commit 2fc3726

18 files changed

Lines changed: 359 additions & 176 deletions

File tree

client/mock/client.go

Lines changed: 134 additions & 35 deletions
Large diffs are not rendered by default.

client/testui/client.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,16 +351,34 @@ func (c *Client) DeployAccounts(ctx context.Context, accountIds ...client.Accoun
351351
}),
352352
}
353353

354+
checkContextCanceled := func(accountId client.AccountId, deployProgress client.DeployProgressAccounts) bool {
355+
if ctx.Err() != nil {
356+
deployProgress.Accounts[accountId].Status = "error"
357+
deployProgress.Accounts[accountId].Progress = 1
358+
deployProgress.Accounts[accountId].Err = ctx.Err()
359+
return true
360+
}
361+
return false
362+
}
363+
354364
go func() {
355365
defer close(deployProgressChan)
356366

367+
AccountLoop:
357368
for i, account := range accounts {
369+
if checkContextCanceled(account.Id, deployProgress) {
370+
continue AccountLoop
371+
}
372+
358373
deployProgress.Accounts[account.Id].Status = "deploying"
359374
deployProgressChan <- deployProgress
360375

361376
// simulate deplay
362377
for _i := range 5 {
363378
time.Sleep(time.Millisecond * 100)
379+
if checkContextCanceled(account.Id, deployProgress) {
380+
continue AccountLoop
381+
}
364382
deployProgress.Accounts[account.Id].Progress = float64(_i+1) / 10
365383
deployProgressChan <- deployProgress
366384
}
@@ -377,6 +395,9 @@ func (c *Client) DeployAccounts(ctx context.Context, accountIds ...client.Accoun
377395
// simulate deplay
378396
for _i := range 5 {
379397
time.Sleep(time.Millisecond * 100)
398+
if checkContextCanceled(account.Id, deployProgress) {
399+
continue AccountLoop
400+
}
380401
deployProgress.Accounts[account.Id].Progress = float64(_i+6) / 10
381402
deployProgressChan <- deployProgress
382403
}
@@ -386,6 +407,7 @@ func (c *Client) DeployAccounts(ctx context.Context, accountIds ...client.Accoun
386407
deployProgress.Accounts[account.Id].Progress = 1
387408
deployProgressChan <- deployProgress
388409
}
410+
deployProgressChan <- deployProgress
389411
}()
390412

391413
return deployProgressChan, nil

ui/tui/models/helpers/crud/create_model.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
package crud
55

66
import (
7+
"context"
8+
79
"github.com/charmbracelet/bubbles/help"
810
tea "github.com/charmbracelet/bubbletea"
911
"github.com/toeirei/keymaster/client"
@@ -72,8 +74,8 @@ func (m *CreateModel[TRecord, TRecordCreate, TRecordUpdate, TRecordId, TFilter])
7274
return popupviews.OpenProgress(
7375
popupviews.ProgressSpinner,
7476
"Creating "+m.crud.Texts.EntityNameSingular,
75-
func(_ popupviews.ProgressChan) tea.Cmd {
76-
record, err := m.crud.createRecord(result)
77+
func(ctx context.Context, _ popupviews.ProgressChan) tea.Cmd {
78+
record, err := m.crud.createRecord(ctx, result)
7779
return util.TeaMsgToCmd(createMsgCreateResult[TRecord]{record, err})
7880
},
7981
), true

ui/tui/models/helpers/crud/crud.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
package crud
55

66
import (
7+
"context"
8+
79
"github.com/charmbracelet/bubbles/table"
810
tea "github.com/charmbracelet/bubbletea"
911
"github.com/toeirei/keymaster/ui/tui/models/components/router"
@@ -26,11 +28,11 @@ type Crud[
2628
Texts Texts
2729

2830
getRecordId func(record TRecord) TRecordId
29-
getRecords func(filter TFilter) ([]TRecord, error)
30-
getRecord func(id TRecordId) (TRecord, error)
31-
createRecord func(recordCreate TRecordCreate) (TRecord, error)
32-
updateRecord func(id TRecordId, recordUpdate TRecordUpdate) (TRecord, error)
33-
deleteRecord func(id TRecordId) error
31+
getRecords func(ctx context.Context, filter TFilter) ([]TRecord, error)
32+
getRecord func(ctx context.Context, id TRecordId) (TRecord, error)
33+
createRecord func(ctx context.Context, recordCreate TRecordCreate) (TRecord, error)
34+
updateRecord func(ctx context.Context, id TRecordId, recordUpdate TRecordUpdate) (TRecord, error)
35+
deleteRecord func(ctx context.Context, id TRecordId) error
3436

3537
buildListTable func(records []TRecord, width int) ([]table.Column, []table.Row)
3638
recordToRecordUpdate func(record TRecord) TRecordUpdate

ui/tui/models/helpers/crud/list_model.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
package crud
55

66
import (
7+
"context"
8+
79
"github.com/bobg/go-generics/v4/slices"
810
"github.com/charmbracelet/bubbles/help"
911
"github.com/charmbracelet/bubbles/key"
@@ -153,11 +155,11 @@ func (m *ListModel[TRecord, TRecordCreate, TRecordUpdate, TRecordId, TFilter]) U
153155
{Name: "Delete", Cmd: popupviews.OpenProgress(
154156
popupviews.ProgressSpinner,
155157
"Deleting "+m.crud.Texts.EntityNameSingular,
156-
func(_ popupviews.ProgressChan) tea.Cmd {
158+
func(ctx context.Context, _ popupviews.ProgressChan) tea.Cmd {
157159
return func() tea.Msg {
158160
return listMsgDeleteResult[TRecord]{
159161
record: *selectedRecord,
160-
err: m.crud.deleteRecord(m.crud.getRecordId(*selectedRecord)),
162+
err: m.crud.deleteRecord(ctx, m.crud.getRecordId(*selectedRecord)),
161163
}
162164
}
163165
},
@@ -219,8 +221,8 @@ var _ util.Model = (*ListModel[any, any, any, any, any])(nil)
219221
func (m *ListModel[TRecord, TRecordCreate, TRecordUpdate, TRecordId, TFilter]) reload() tea.Cmd {
220222
return popupviews.OpenProgress(
221223
popupviews.ProgressSpinner,
222-
"Loading "+m.crud.Texts.EntityNameMultiple, func(pc popupviews.ProgressChan) tea.Cmd {
223-
records, err := m.crud.getRecords(util.NewZero[TFilter]())
224+
"Loading "+m.crud.Texts.EntityNameMultiple, func(ctx context.Context, pc popupviews.ProgressChan) tea.Cmd {
225+
records, err := m.crud.getRecords(ctx, util.NewZero[TFilter]())
224226
return util.TeaMsgToCmd(listMsgReloaded[TRecord]{records, err})
225227
},
226228
)

ui/tui/models/helpers/crud/new.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
package crud
55

66
import (
7+
"context"
8+
79
"github.com/charmbracelet/bubbles/key"
810
"github.com/charmbracelet/bubbles/table"
911
tea "github.com/charmbracelet/bubbletea"
@@ -32,11 +34,11 @@ func New[
3234
texts Texts,
3335

3436
getRecordId func(record TRecord) TRecordId,
35-
getRecords func(filter TFilter) ([]TRecord, error),
36-
getRecord func(id TRecordId) (TRecord, error),
37-
createRecord func(recordCreate TRecordCreate) (TRecord, error),
38-
updateRecord func(id TRecordId, recordUpdate TRecordUpdate) (TRecord, error),
39-
deleteRecord func(id TRecordId) error,
37+
getRecords func(ctx context.Context, filter TFilter) ([]TRecord, error),
38+
getRecord func(ctx context.Context, id TRecordId) (TRecord, error),
39+
createRecord func(ctx context.Context, recordCreate TRecordCreate) (TRecord, error),
40+
updateRecord func(ctx context.Context, id TRecordId, recordUpdate TRecordUpdate) (TRecord, error),
41+
deleteRecord func(ctx context.Context, id TRecordId) error,
4042

4143
buildListTable func(records []TRecord, width int) ([]table.Column, []table.Row),
4244
recordToRecordUpdate func(record TRecord) TRecordUpdate,

ui/tui/models/helpers/crud/update_model.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
package crud
55

66
import (
7+
"context"
8+
79
"github.com/charmbracelet/bubbles/help"
810
tea "github.com/charmbracelet/bubbletea"
911
"github.com/toeirei/keymaster/ui/tui/models/helpers/form"
@@ -69,8 +71,8 @@ func (m *UpdateModel[TRecord, TRecordCreate, TRecordUpdate, TRecordId, TFilter])
6971
return popupviews.OpenProgress(
7072
popupviews.ProgressSpinner,
7173
"Updating "+m.crud.Texts.EntityNameSingular,
72-
func(_ popupviews.ProgressChan) tea.Cmd {
73-
record, err := m.crud.updateRecord(m.crud.getRecordId(m.record), result)
74+
func(ctx context.Context, _ popupviews.ProgressChan) tea.Cmd {
75+
record, err := m.crud.updateRecord(ctx, m.crud.getRecordId(m.record), result)
7476
return util.TeaMsgToCmd(updateMsgUpdateResult[TRecord]{record, err})
7577
},
7678
), true

ui/tui/models/helpers/deploy/deploy.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,15 @@ func DeployMany(ctx context.Context, c client.Client, accounts ...client.Account
4545
accountNamesWidth := slicest.Reduce(slicest.MapValues(accountNamesMap), func(accountName string, width int) int { return max(width, len(accountName)) })
4646
accountNameRenderer := lipgloss.NewStyle().Width(accountNamesWidth)
4747

48-
dpc, err := c.DeployAccounts(ctx, ids...)
49-
if err != nil {
50-
return popupviews.OpenMessage(popupviews.MessageError, err.Error(), nil)
51-
}
52-
5348
return popupviews.OpenProgress(
5449
popupviews.ProgressBar,
5550
"Deploying Accounts",
56-
func(pc popupviews.ProgressChan) tea.Cmd {
51+
func(ctx context.Context, pc popupviews.ProgressChan) tea.Cmd {
52+
dpc, err := c.DeployAccounts(ctx, ids...)
53+
if err != nil {
54+
return popupviews.OpenMessage(popupviews.MessageError, err.Error(), nil)
55+
}
56+
5757
var dp client.DeployProgressAccounts
5858

5959
// map [client.DeployProgressAccounts] chan to [popupviews.Progress] chan
@@ -88,5 +88,7 @@ func DeployMany(ctx context.Context, c client.Client, accounts ...client.Account
8888
nil,
8989
)
9090
},
91+
popupviews.WithContext(ctx),
92+
popupviews.WithCancel(),
9193
)
9294
}

ui/tui/models/helpers/deploy/verify.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func VerifyMany(ctx context.Context, c client.Client, accounts ...client.Account
5353
return popupviews.OpenProgress(
5454
popupviews.ProgressBar,
5555
"Verifying Accounts",
56-
func(pc popupviews.ProgressChan) tea.Cmd {
56+
func(_ context.Context, pc popupviews.ProgressChan) tea.Cmd {
5757
var dp client.VerifyProgressAccounts
5858

5959
// map [client.VerifyProgressAccounts] chan to [popupviews.Progress] chan

ui/tui/models/helpers/form/form.go

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,17 +212,33 @@ var _ util.Model = (*Form[any])(nil)
212212

213213
func (f *Form[T]) Focus(parentKeyMap help.KeyMap) tea.Cmd {
214214
f.focused, f.parentKeyMap = true, parentKeyMap
215-
return f.items[f.activeIndex].Element.Focus(f.keymap())
215+
return f.activeElementFocus(f.keymap())
216216
}
217217

218218
func (f *Form[T]) Blur() {
219219
f.focused, f.parentKeyMap = false, nil
220-
f.items[f.activeIndex].Element.Blur()
220+
f.activeElementBlur()
221221
}
222222

223223
// *[Model] implements [util.Focusable]
224224
var _ util.Focusable = (*Form[any])(nil)
225225

226+
func (f *Form[T]) hasActiveItem() bool { return f.activeIndex < len(f.items) }
227+
228+
func (f *Form[T]) activeElementFocus(parentKeyMap help.KeyMap) tea.Cmd {
229+
if !f.hasActiveItem() {
230+
return util.AnnounceKeyMapCmd(parentKeyMap)
231+
}
232+
return f.items[f.activeIndex].Element.Focus(parentKeyMap)
233+
}
234+
235+
func (f *Form[T]) activeElementBlur() {
236+
if !f.hasActiveItem() {
237+
return
238+
}
239+
f.items[f.activeIndex].Element.Blur()
240+
}
241+
226242
func (f *Form[T]) viewRow(rowIndex int, availableWidth int, eager bool) []string {
227243
return slicest.MapI(f.rows[rowIndex].items, func(i int, itemIndex int) string {
228244
width := availableWidth / (len(f.rows[rowIndex].items) - i)
@@ -310,6 +326,10 @@ func (f *Form[T]) guardUnsavedChanges(action Action) tea.Cmd {
310326
func (f *Form[T]) updateElement(index int, msg tea.Msg) tea.Cmd {
311327
var actionCmd tea.Cmd
312328

329+
if index >= len(f.items) {
330+
return nil
331+
}
332+
313333
updateCmd, action := f.items[index].Element.Update(msg)
314334

315335
switch action {
@@ -354,10 +374,10 @@ func (f *Form[T]) changeActiveIndex(index_delta int) tea.Cmd {
354374
return nil
355375
}
356376

357-
f.items[f.activeIndex].Element.Blur()
377+
f.activeElementBlur()
358378
f.activeIndex = newActiveIndex
359379

360-
return f.items[f.activeIndex].Element.Focus(f.keymap())
380+
return f.activeElementFocus(f.keymap())
361381
}
362382

363383
func (f *Form[T]) Get() (T, error) {

0 commit comments

Comments
 (0)