/
events.go
56 lines (45 loc) · 986 Bytes
/
events.go
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
package sqlite
import (
"fmt"
)
type Model struct {
Value any
}
func (m *Model) String() string {
return fmt.Sprintf("%v", m.Value)
}
func NewEvent(value any, executable Executable) Event {
return &EventModel{data: &Model{Value: value}, executable: executable}
}
type Event interface {
Execute(repo *Repository)
Send(Response)
Receive() Response
}
type EventModel struct {
responseChannel chan Response
data *Model
executable Executable
}
func (e *EventModel) Execute(repo *Repository) {
e.Send(e.executable(e.data, repo))
}
func (e *EventModel) getChannel() chan Response {
if e.responseChannel == nil {
e.responseChannel = make(chan Response, 1)
}
return e.responseChannel
}
func (e *EventModel) Send(val Response) {
e.getChannel() <- val
}
func (e *EventModel) Receive() Response {
return <-e.getChannel()
}
func NewResponse(value any, err error) Response {
return Response{value, err}
}
type Response struct {
Value any
Err error
}