-
Notifications
You must be signed in to change notification settings - Fork 84
/
bind.go
32 lines (25 loc) · 905 Bytes
/
bind.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
package event
import "github.com/oakmound/oak/v2/dlog"
// BindPriority is called by entities. Entities pass in a bindable function,
// and a set of options which are parsed out.
// Returns a binding that can used to unbind this binding later.
func (eb *Bus) BindPriority(fn Bindable, opt BindingOption) {
eb.pendingMutex.Lock()
eb.binds = append(eb.binds, UnbindOption{opt, fn})
eb.pendingMutex.Unlock()
}
// Bind adds a function to the event bus tied to the given callerID
// to be called when the event name is triggered.
func (eb *Bus) Bind(fn Bindable, name string, callerID int) {
bOpt := BindingOption{}
bOpt.Event = Event{
Name: name,
CallerID: callerID,
}
dlog.Verb("Binding ", callerID, " with name ", name)
eb.BindPriority(fn, bOpt)
}
// GlobalBind binds on the bus to the cid 0, a non entity.
func (eb *Bus) GlobalBind(fn Bindable, name string) {
eb.Bind(fn, name, 0)
}