Skip to content

Commit c0674b9

Browse files
committed
make getContent non blocking
1 parent 8b4477e commit c0674b9

3 files changed

Lines changed: 76 additions & 42 deletions

File tree

list.go

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func (f file) Title() string { return f.title }
112112
func (f file) Description() string { return f.desc }
113113
func (f file) FilterValue() string { return f.title }
114114

115-
func (f file) getContent() (string, error) {
115+
func (f file) getContent(q map[file]string) (string, error) {
116116
if f.draft {
117117
return f.content, nil
118118
}
@@ -140,30 +140,32 @@ func (f file) getContent() (string, error) {
140140
}
141141

142142
if shouldFetch {
143-
client := &http.Client{Timeout: 5 * time.Second}
144-
resp, err := client.Get(f.rawUrl)
145-
if err != nil {
146-
log.Errorf("Could not fetch file with raw url: %s", f.rawUrl)
147-
return "", err
148-
}
149-
defer resp.Body.Close()
150-
151-
contentBytes, err := io.ReadAll(resp.Body)
152-
if err != nil {
153-
log.Errorf(err.Error())
154-
return "", err
155-
}
156-
157-
content := string(contentBytes)
158-
159-
existing.Set("content", content)
160-
161-
if err := storage.db.Save(string(collectionGistContent), existing); err != nil {
162-
log.Errorf(err.Error())
163-
return "", err
164-
}
165-
166-
return content, nil
143+
go func() {
144+
client := &http.Client{Timeout: 5 * time.Second}
145+
resp, err := client.Get(f.rawUrl)
146+
if err != nil {
147+
log.Errorf("Could not fetch file with raw url: %s", f.rawUrl)
148+
return
149+
}
150+
defer resp.Body.Close()
151+
152+
contentBytes, err := io.ReadAll(resp.Body)
153+
if err != nil {
154+
log.Errorf(err.Error())
155+
return
156+
}
157+
158+
content := string(contentBytes)
159+
160+
existing.Set("content", content)
161+
162+
if err := storage.db.Save(string(collectionGistContent), existing); err != nil {
163+
log.Errorf(err.Error())
164+
return
165+
}
166+
q[f] = content
167+
}()
168+
return "Loading...", nil
167169
} else {
168170
if cachedContent, ok := existing.Get("content").(string); ok {
169171
return cachedContent, nil
@@ -173,7 +175,8 @@ func (f file) getContent() (string, error) {
173175
}
174176

175177
type filesDelegate struct {
176-
styles FilesBaseStyle
178+
styles FilesBaseStyle
179+
contentQueue map[file]string
177180
}
178181

179182
func (d filesDelegate) Height() int {
@@ -193,7 +196,7 @@ func (d filesDelegate) Update(msg tea.Msg, m *list.Model) tea.Cmd {
193196
}
194197

195198
f, _ := m.SelectedItem().(file)
196-
content, err := f.getContent()
199+
content, err := f.getContent(d.contentQueue)
197200
if err != nil {
198201
return func() tea.Msg {
199202
return showInfo(err.Error(), info_error)
@@ -243,14 +246,20 @@ func (d filesDelegate) Render(w io.Writer, m list.Model, index int, item list.It
243246
fmt.Fprintln(w, " "+title)
244247
}
245248

246-
func newFileList(items []list.Item, styles FilesBaseStyle) list.Model {
247-
l := list.New(items, filesDelegate{styles: styles}, 25, 0)
249+
func newFileList(items []list.Item, styles FilesBaseStyle) (list.Model, *filesDelegate) {
250+
delegate := filesDelegate{
251+
styles: styles,
252+
contentQueue: map[file]string{},
253+
}
254+
255+
l := list.New(items, delegate, 25, 0)
248256
l.Title = "Files"
249257
l.SetShowStatusBar(false)
250258
l.SetShowHelp(false)
251259
l.Styles.Title = styles.Title
252260
l.Styles.TitleBar = styles.TitleBar
253261
l.Styles.NoItems = styles.NoItems
254262
l.InfiniteScrolling = true
255-
return l
263+
264+
return l, &delegate
256265
}

main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ func main() {
178178
UsageText: " [FILE NAME]",
179179
},
180180
},
181-
Action: delete,
181+
Action: deleteG,
182182
},
183183
{
184184
Name: "drop",
@@ -381,7 +381,7 @@ func create(ctx context.Context, c *cli.Command) error {
381381
return nil
382382
}
383383

384-
func delete(ctx context.Context, c *cli.Command) error {
384+
func deleteG(ctx context.Context, c *cli.Command) error {
385385
if !cfg.hasAccessToken() {
386386
return err_unauthorized
387387
}

main_model.go

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,23 @@ type mainModel struct {
4545
styles Styles
4646
filesStyle FilesBaseStyle
4747
gistsStyle GistsBaseStyle
48+
49+
filesDelegate *filesDelegate
4850
}
4951

52+
type tickMsg time.Time
53+
5054
func newMainModel() mainModel {
5155
defaultStyle := DefaultStyles(cfg)
5256
m := mainModel{
53-
keymap: DefaultKeymap,
54-
help: help.New(),
55-
currentPane: PANE_GISTS,
56-
infoMsg: nil,
57-
styles: defaultStyle,
58-
gistsStyle: defaultStyle.Gists.Focused,
59-
filesStyle: defaultStyle.Files.Blurred,
57+
keymap: DefaultKeymap,
58+
help: help.New(),
59+
currentPane: PANE_GISTS,
60+
infoMsg: nil,
61+
styles: defaultStyle,
62+
gistsStyle: defaultStyle.Gists.Focused,
63+
filesStyle: defaultStyle.Files.Blurred,
64+
filesDelegate: nil,
6065
}
6166

6267
// populate gist list
@@ -77,7 +82,7 @@ func newMainModel() mainModel {
7782
}
7883

7984
m.gistList = newGistList(gistList, m.gistsStyle)
80-
m.fileList = newFileList(gists[firstgist], m.filesStyle)
85+
m.fileList, m.filesDelegate = newFileList(gists[firstgist], m.filesStyle)
8186

8287
// dont care about the width and height because we set it inside the tea.WindowSizeMsg
8388
textEditor := editor.New(0, 0)
@@ -228,9 +233,15 @@ type updateEditorContent struct {
228233
language string
229234
}
230235

236+
func tick() tea.Cmd {
237+
return tea.Tick(time.Second, func(t time.Time) tea.Msg {
238+
return tickMsg(t)
239+
})
240+
}
241+
231242
func (m mainModel) Init() tea.Cmd {
232243
_, initFileList := m.fileList.Update(nil)
233-
return tea.Batch(initFileList, m.editor.CursorBlink())
244+
return tea.Batch(tick(), initFileList, m.editor.CursorBlink())
234245
}
235246

236247
func (m *mainModel) resetListHeight() {
@@ -248,7 +259,21 @@ func (m mainModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
248259
var cmd tea.Cmd
249260

250261
switch msg := msg.(type) {
251-
262+
case tickMsg:
263+
// deque requested gist file content and update
264+
// the fileList that triggers update to the editor with the content from request
265+
q := m.filesDelegate.contentQueue
266+
if len(q) > 0 {
267+
item, _ := m.fileList.SelectedItem().(file)
268+
_, ok := q[item]
269+
if !ok {
270+
return m, tick()
271+
}
272+
delete(q, item)
273+
_, cmd := m.fileList.Update(msg) // <-- calls f.getContent() again to reload the editor content
274+
return m, tea.Batch(tick(), cmd)
275+
}
276+
return m, tick()
252277
case editor.SaveMsg:
253278
if m.currentPane == PANE_EDITOR {
254279
m.editor.Blur()

0 commit comments

Comments
 (0)