From d24e4712a45e03d79fa1ccb71d00c5b830d5a305 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Thu, 1 Feb 2018 18:42:03 -0500 Subject: Reduce boilerplate in worker/UI --- worker/worker.go | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) (limited to 'worker/worker.go') diff --git a/worker/worker.go b/worker/worker.go index b6658849..439ab648 100644 --- a/worker/worker.go +++ b/worker/worker.go @@ -9,22 +9,24 @@ import ( "net/url" ) -type Worker interface { - GetMessages() chan types.WorkerMessage - PostAction(types.WorkerMessage) - Run() -} - // Guesses the appropriate worker type based on the given source string -func NewWorker(source string, logger *log.Logger) (Worker, error) { +func NewWorker(source string, logger *log.Logger) (*types.Worker, error) { u, err := url.Parse(source) if err != nil { return nil, err } + worker := &types.Worker{ + Actions: make(chan types.WorkerMessage, 50), + Callbacks: make(map[types.WorkerMessage]func(msg types.WorkerMessage)), + Messages: make(chan types.WorkerMessage, 50), + Logger: logger, + } switch u.Scheme { case "imap": case "imaps": - return imap.NewIMAPWorker(logger), nil + worker.Backend = imap.NewIMAPWorker(worker) + default: + return nil, fmt.Errorf("Unknown backend %s", u.Scheme) } - return nil, fmt.Errorf("Unknown backend %s", u.Scheme) + return worker, nil } -- cgit