-
Notifications
You must be signed in to change notification settings - Fork 2
/
interface.go
40 lines (33 loc) · 1.53 KB
/
interface.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
package eventemitter
// EventType is a type of event, each type of event should have a different type
type EventType string
// HandleFunc is a handler function for a given event type
type HandleFunc func(arguments ...interface{})
// Listener is a container struct used to remove the listener
type Listener struct {
handler HandleFunc
}
// CaptureFunc is a capturer function that can capture all emitted events
type CaptureFunc func(event EventType, arguments ...interface{})
// Capturer is a container struct used to remove the capturer
type Capturer struct {
handler CaptureFunc
}
// Observable describes an object that can be listened to by event listeners and capturers
type Observable interface {
// AddListener adds a listener for the given event type
AddListener(event EventType, handler HandleFunc) (listener *Listener)
// ListenOnce adds a listener for the given event type that removes itself after it has been fired once
ListenOnce(event EventType, handler HandleFunc) (listener *Listener)
// AddCapturer adds an event capturer for all events
AddCapturer(handler CaptureFunc) (capturer *Capturer)
// RemoveListener removes the registered given listener for the given event
RemoveListener(event EventType, listener *Listener)
// RemoveCapturer removes the given capturer
RemoveCapturer(capturer *Capturer)
}
// EventEmitter is the interface which allows implementers to emit events
type EventEmitter interface {
// EmitEvent emits the given event to all listeners and capturers
EmitEvent(event EventType, arguments ...interface{})
}