summaryrefslogtreecommitdiff
path: root/worker/types/worker.go
blob: 8f179df6a31d941786ab93d36df77a9ed583cf28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package types

import (
	"sync/atomic"

	"git.sr.ht/~rjarry/aerc/logging"
)

var lastId int64 = 1 // access via atomic

type Backend interface {
	Run()
}

type Worker struct {
	Backend  Backend
	Actions  chan WorkerMessage
	Messages chan WorkerMessage

	actionCallbacks  map[int64]func(msg WorkerMessage)
	messageCallbacks map[int64]func(msg WorkerMessage)
}

func NewWorker() *Worker {
	return &Worker{
		Actions:          make(chan WorkerMessage, 50),
		Messages:         make(chan WorkerMessage, 50),
		actionCallbacks:  make(map[int64]func(msg WorkerMessage)),
		messageCallbacks: make(map[int64]func(msg WorkerMessage)),
	}
}

func (worker *Worker) setId(msg WorkerMessage) {
	id := atomic.AddInt64(&lastId, 1)
	msg.setId(id)
}

func (worker *Worker) PostAction(msg WorkerMessage, cb func(msg WorkerMessage)) {

	worker.setId(msg)

	if resp := msg.InResponseTo(); resp != nil {
		logging.Debugf("PostAction %T:%T", msg, resp)
	} else {
		logging.Debugf("PostAction %T", msg)
	}
	worker.Actions <- msg

	if cb != nil {
		worker.actionCallbacks[msg.getId()] = cb
	}
}

func (worker *Worker) PostMessage(msg WorkerMessage,
	cb func(msg WorkerMessage)) {

	worker.setId(msg)

	if resp := msg.InResponseTo(); resp != nil {
		logging.Debugf("PostMessage %T:%T", msg, resp)
	} else {
		logging.Debugf("PostMessage %T", msg)
	}
	worker.Messages <- msg

	if cb != nil {
		worker.messageCallbacks[msg.getId()] = cb
	}
}

func (worker *Worker) ProcessMessage(msg WorkerMessage) WorkerMessage {
	if resp := msg.InResponseTo(); resp != nil {
		logging.Debugf("ProcessMessage %T(%d):%T(%d)", msg, msg.getId(), resp, resp.getId())
	} else {
		logging.Debugf("ProcessMessage %T(%d)", msg, msg.getId())
	}
	if inResponseTo := msg.InResponseTo(); inResponseTo != nil {
		if f, ok := worker.actionCallbacks[inResponseTo.getId()]; ok {
			f(msg)
			if _, ok := msg.(*Done); ok {
				delete(worker.actionCallbacks, inResponseTo.getId())
			}
		}
	}
	return msg
}

func (worker *Worker) ProcessAction(msg WorkerMessage) WorkerMessage {
	if resp := msg.InResponseTo(); resp != nil {
		logging.Debugf("ProcessAction %T(%d):%T(%d)", msg, msg.getId(), resp, resp.getId())
	} else {
		logging.Debugf("ProcessAction %T(%d)", msg, msg.getId())
	}
	if inResponseTo := msg.InResponseTo(); inResponseTo != nil {
		if f, ok := worker.messageCallbacks[inResponseTo.getId()]; ok {
			f(msg)
			if _, ok := msg.(*Done); ok {
				delete(worker.messageCallbacks, inResponseTo.getId())
			}
		}
	}
	return msg
}