summaryrefslogtreecommitdiffstats
path: root/pkg/tasks/tasks.go
diff options
context:
space:
mode:
authorJesse Duffield <jessedduffield@gmail.com>2021-11-02 16:39:15 +1100
committerJesse Duffield <jessedduffield@gmail.com>2021-11-05 07:58:21 +1100
commit802cfb1a0436568c72fc998249f10f8150b352a3 (patch)
tree599f8a8bd52b786312a11f3b3cac2a2d5b7c597e /pkg/tasks/tasks.go
parent2fc1498517523a20a3080816ec50ee9e7fbe533d (diff)
render commit graph
Diffstat (limited to 'pkg/tasks/tasks.go')
-rw-r--r--pkg/tasks/tasks.go29
1 files changed, 25 insertions, 4 deletions
diff --git a/pkg/tasks/tasks.go b/pkg/tasks/tasks.go
index e394f18f7..62237f22f 100644
--- a/pkg/tasks/tasks.go
+++ b/pkg/tasks/tasks.go
@@ -14,6 +14,8 @@ import (
"github.com/sirupsen/logrus"
)
+const THROTTLE_TIME = time.Millisecond * 30
+
type Task struct {
stop chan struct{}
stopped bool
@@ -38,6 +40,12 @@ type ViewBufferManager struct {
beforeStart func()
refreshView func()
onEndOfInput func()
+
+ // if the user flicks through a heap of items, with each one
+ // spawning a process to render something to the main view,
+ // it can slow things down quite a bit. In these situations we
+ // want to throttle the spawning of processes.
+ throttle bool
}
func (m *ViewBufferManager) GetTaskKey() string {
@@ -69,18 +77,31 @@ func (m *ViewBufferManager) ReadLines(n int) {
})
}
-func (m *ViewBufferManager) NewCmdTask(r io.Reader, cmd *exec.Cmd, prefix string, linesToRead int, onDone func()) func(chan struct{}) error {
+func (m *ViewBufferManager) NewCmdTask(start func() (*exec.Cmd, io.Reader), prefix string, linesToRead int, onDone func()) func(chan struct{}) error {
return func(stop chan struct{}) error {
+ if m.throttle {
+ m.Log.Info("throttling task")
+ time.Sleep(THROTTLE_TIME)
+ }
+
+ select {
+ case <-stop:
+ return nil
+ default:
+ }
+
+ startTime := time.Now()
+
+ cmd, r := start()
+
go utils.Safe(func() {
<-stop
+ m.throttle = time.Since(startTime) < THROTTLE_TIME
if err := oscommands.Kill(cmd); err != nil {
if !strings.Contains(err.Error(), "process already finished") {
m.Log.Errorf("error when running cmd task: %v", err)
}
}
- if onDone != nil {
- onDone()
- }
})
loadingMutex := sync.Mutex{}