From 115fb9322ce4065bb7212e8979464d0f9c1db12d Mon Sep 17 00:00:00 2001 From: Tim Culverhouse Date: Tue, 20 Jun 2023 11:07:07 -0500 Subject: worker: add context to cancellable requests Add a Context field to requests which we may want to cancel when changing directories. Add a Cancelled meta-message to inform the UI that a request was cancelled (as opposed to Done or Error). Delete callbacks when a request is Cancelled. Signed-off-by: Tim Culverhouse Tested-by: Bence Ferdinandy Acked-by: Robin Jarry --- lib/msgstore.go | 30 +++++++++++++++++++----------- widgets/dirlist.go | 5 ++++- worker/types/messages.go | 17 ++++++++++++++--- worker/types/worker.go | 3 ++- 4 files changed, 39 insertions(+), 16 deletions(-) diff --git a/lib/msgstore.go b/lib/msgstore.go index f7ffaa51..77aeb4c1 100644 --- a/lib/msgstore.go +++ b/lib/msgstore.go @@ -139,16 +139,20 @@ func (store *MessageStore) FetchHeaders(uids []uint32, } } if len(toFetch) > 0 { - store.worker.PostAction(&types.FetchMessageHeaders{Uids: toFetch}, func(msg types.WorkerMessage) { - if _, ok := msg.(*types.Error); ok { - for _, uid := range toFetch { - delete(store.pendingHeaders, uid) + store.worker.PostAction(&types.FetchMessageHeaders{ + Context: store.ctx, + Uids: toFetch, + }, + func(msg types.WorkerMessage) { + if _, ok := msg.(*types.Error); ok { + for _, uid := range toFetch { + delete(store.pendingHeaders, uid) + } } - } - if cb != nil { - cb(msg) - } - }) + if cb != nil { + cb(msg) + } + }) } } @@ -664,7 +668,8 @@ func (store *MessageStore) Prev() { func (store *MessageStore) Search(args []string, cb func([]uint32)) { store.worker.PostAction(&types.SearchDirectory{ - Argv: args, + Context: store.ctx, + Argv: args, }, func(msg types.WorkerMessage) { if msg, ok := msg.(*types.SearchResults); ok { allowedUids := store.Uids() @@ -776,11 +781,13 @@ func (store *MessageStore) Sort(criteria []*types.SortCriterion, cb func(types.W if store.threadedView && !store.buildThreads { store.worker.PostAction(&types.FetchDirectoryThreaded{ + Context: store.ctx, SortCriteria: criteria, FilterCriteria: store.filter, }, handle_return) } else { store.worker.PostAction(&types.FetchDirectoryContents{ + Context: store.ctx, SortCriteria: criteria, FilterCriteria: store.filter, }, handle_return) @@ -830,7 +837,8 @@ func (store *MessageStore) fetchFlags() { store.fetchFlagsDebounce = time.AfterFunc(store.fetchFlagsDelay, func() { store.Lock() store.worker.PostAction(&types.FetchMessageFlags{ - Uids: store.needsFlags, + Context: store.ctx, + Uids: store.needsFlags, }, nil) store.needsFlags = []uint32{} store.Unlock() diff --git a/widgets/dirlist.go b/widgets/dirlist.go index 3b5c2d91..788e197f 100644 --- a/widgets/dirlist.go +++ b/widgets/dirlist.go @@ -161,7 +161,10 @@ func (dirlist *DirectoryList) Select(name string) { select { case <-time.After(delay): - dirlist.worker.PostAction(&types.OpenDirectory{Directory: name}, + dirlist.worker.PostAction(&types.OpenDirectory{ + Context: dirlist.ctx, + Directory: name, + }, func(msg types.WorkerMessage) { switch msg.(type) { case *types.Error: diff --git a/worker/types/messages.go b/worker/types/messages.go index 0745de5b..7bad31f9 100644 --- a/worker/types/messages.go +++ b/worker/types/messages.go @@ -1,6 +1,7 @@ package types import ( + "context" "io" "time" @@ -59,6 +60,10 @@ type Error struct { Error error } +type Cancelled struct { + Message +} + type ConnError struct { Message Error error @@ -93,24 +98,28 @@ type ListDirectories struct { type OpenDirectory struct { Message + Context context.Context Directory string } type FetchDirectoryContents struct { Message + Context context.Context SortCriteria []*SortCriterion FilterCriteria []string } type FetchDirectoryThreaded struct { Message + Context context.Context SortCriteria []*SortCriterion FilterCriteria []string } type SearchDirectory struct { Message - Argv []string + Context context.Context + Argv []string } type DirectoryThreaded struct { @@ -132,7 +141,8 @@ type RemoveDirectory struct { type FetchMessageHeaders struct { Message - Uids []uint32 + Context context.Context + Uids []uint32 } type FetchFullMessages struct { @@ -148,7 +158,8 @@ type FetchMessageBodyPart struct { type FetchMessageFlags struct { Message - Uids []uint32 + Context context.Context + Uids []uint32 } type DeleteMessages struct { diff --git a/worker/types/worker.go b/worker/types/worker.go index cc398da4..e263679a 100644 --- a/worker/types/worker.go +++ b/worker/types/worker.go @@ -122,7 +122,8 @@ func (worker *Worker) ProcessMessage(msg WorkerMessage) WorkerMessage { worker.Unlock() if ok { f(msg) - if _, ok := msg.(*Done); ok { + switch msg.(type) { + case *Cancelled, *Done: worker.Lock() delete(worker.actionCallbacks, inResponseTo.getId()) worker.Unlock() -- cgit