This repository has been archived by the owner on Nov 8, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
event.go
85 lines (68 loc) · 1.95 KB
/
event.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
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
package bake
/*
// Event represents an event that occurs on a target.
type Event interface {
event()
}
func (*CreatePackageEvent) event() {}
// CreatePackageEvent represents the creation of a package on a workspace.
type CreatePackageEvent struct {
Package *Package
}
func (*ReadFileEvent) event() {}
// ReadFileEvent represents a read of a file by a target.
type ReadFileEvent struct {
Target Target
File *File
}
// WriteFileEvent represents a write of a file by a target.
type WriteFileEvent struct {
Target Target
File *File
}
// EventHandler represents an object that can receive events.
type EventHandler interface {
HandleEvent(Event)
}
// EventHandlerFunc is a adapter to allow functions to implement EventHandler.
type EventHandlerFunc func(Event)
// HandleEvent calls fn(e).
func (fn EventHandlerFunc) HandleEvent(e Event) { fn(e) }
// EventDispatcher represents an object that can register handlers and dispatch events.
type EventDispatcher interface {
AddHandler(h EventHandler)
RemoveHandler(h EventHandler)
}
// dispatcher manages event handlers and dispatches events.
type dispatcher struct {
handlers []EventHandler
}
// newDispatcher returns a new instance of dispatcher.
func newDispatcher() *dispatcher {
return &dispatcher{}
}
// addHandler adds h to the set of event listeners.
func (d *dispatcher) addHandler(h EventHandler) {
d.handlers = append(d.handlers, h)
}
// removeHandler deletes h from the set of event listeners.
func (d *dispatcher) removeHandler(h EventHandler) {
for i, handler := range d.handlers {
if handler == h {
copy(d.handlers[i:], d.handlers[i+1:])
d.handlers[len(d.handlers)-1] = nil
d.handlers = d.handlers[:len(d.handlers)-1]
}
}
}
// dispatch sends e to all registered handlers.
func (d *dispatcher) dispatch(e Event) {
for _, h := range d.handlers {
h.HandleEvent(e)
}
}
type handlerFuncEntry struct {
EventHandlerFunc
}
func (h handlerFuncEntry) HandleEvent(e Event) { h.EventHandlerFunc(e) }
*/