notmuch: add internal event loop
This commit is contained in:
parent
bb09694f07
commit
8d216ab10e
3 changed files with 38 additions and 12 deletions
9
worker/notmuch/eventhandlers.go
Normal file
9
worker/notmuch/eventhandlers.go
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
package notmuch
|
||||||
|
|
||||||
|
func (w *worker) handleNotmuchEvent(et eventType) error {
|
||||||
|
switch ev := et.(type) {
|
||||||
|
default:
|
||||||
|
_ = ev
|
||||||
|
return errUnsupported
|
||||||
|
}
|
||||||
|
}
|
5
worker/notmuch/events.go
Normal file
5
worker/notmuch/events.go
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
package notmuch
|
||||||
|
|
||||||
|
type eventType interface{}
|
||||||
|
|
||||||
|
type event struct{}
|
|
@ -28,6 +28,7 @@ var errUnsupported = fmt.Errorf("unsupported command")
|
||||||
|
|
||||||
type worker struct {
|
type worker struct {
|
||||||
w *types.Worker
|
w *types.Worker
|
||||||
|
nmEvents chan eventType
|
||||||
query string
|
query string
|
||||||
uidStore *uidstore.Store
|
uidStore *uidstore.Store
|
||||||
nameQueryMap map[string]string
|
nameQueryMap map[string]string
|
||||||
|
@ -38,23 +39,34 @@ type worker struct {
|
||||||
|
|
||||||
// NewWorker creates a new maildir worker with the provided worker.
|
// NewWorker creates a new maildir worker with the provided worker.
|
||||||
func NewWorker(w *types.Worker) (types.Backend, error) {
|
func NewWorker(w *types.Worker) (types.Backend, error) {
|
||||||
return &worker{w: w}, nil
|
events := make(chan eventType, 20)
|
||||||
|
return &worker{w: w,
|
||||||
|
nmEvents: events}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run starts the worker's message handling loop.
|
// Run starts the worker's message handling loop.
|
||||||
func (w *worker) Run() {
|
func (w *worker) Run() {
|
||||||
for {
|
for {
|
||||||
action := <-w.w.Actions
|
select {
|
||||||
|
case action := <-w.w.Actions:
|
||||||
msg := w.w.ProcessAction(action)
|
msg := w.w.ProcessAction(action)
|
||||||
if err := w.handleMessage(msg); err == errUnsupported {
|
if err := w.handleMessage(msg); err == errUnsupported {
|
||||||
w.w.PostMessage(&types.Unsupported{
|
w.w.PostMessage(&types.Unsupported{
|
||||||
Message: types.RespondTo(msg),
|
Message: types.RespondTo(msg),
|
||||||
}, nil)
|
}, nil)
|
||||||
|
w.w.Logger.Printf("ProcessAction(%T) unsupported: %v", msg, err)
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
w.w.PostMessage(&types.Error{
|
w.w.PostMessage(&types.Error{
|
||||||
Message: types.RespondTo(msg),
|
Message: types.RespondTo(msg),
|
||||||
Error: err,
|
Error: err,
|
||||||
}, nil)
|
}, nil)
|
||||||
|
w.w.Logger.Printf("ProcessAction(%T) failure: %v", msg, err)
|
||||||
|
}
|
||||||
|
case nmEvent := <-w.nmEvents:
|
||||||
|
err := w.handleNotmuchEvent(nmEvent)
|
||||||
|
if err != nil {
|
||||||
|
w.w.Logger.Printf("notmuch event failure: %v", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue