1package memberlist
2
3// EventDelegate is a simpler delegate that is used only to receive
4// notifications about members joining and leaving. The methods in this
5// delegate may be called by multiple goroutines, but never concurrently.
6// This allows you to reason about ordering.
7type EventDelegate interface {
8	// NotifyJoin is invoked when a node is detected to have joined.
9	// The Node argument must not be modified.
10	NotifyJoin(*Node)
11
12	// NotifyLeave is invoked when a node is detected to have left.
13	// The Node argument must not be modified.
14	NotifyLeave(*Node)
15
16	// NotifyUpdate is invoked when a node is detected to have
17	// updated, usually involving the meta data. The Node argument
18	// must not be modified.
19	NotifyUpdate(*Node)
20}
21
22// ChannelEventDelegate is used to enable an application to receive
23// events about joins and leaves over a channel instead of a direct
24// function call.
25//
26// Care must be taken that events are processed in a timely manner from
27// the channel, since this delegate will block until an event can be sent.
28type ChannelEventDelegate struct {
29	Ch chan<- NodeEvent
30}
31
32// NodeEventType are the types of events that can be sent from the
33// ChannelEventDelegate.
34type NodeEventType int
35
36const (
37	NodeJoin NodeEventType = iota
38	NodeLeave
39	NodeUpdate
40)
41
42// NodeEvent is a single event related to node activity in the memberlist.
43// The Node member of this struct must not be directly modified. It is passed
44// as a pointer to avoid unnecessary copies. If you wish to modify the node,
45// make a copy first.
46type NodeEvent struct {
47	Event NodeEventType
48	Node  *Node
49}
50
51func (c *ChannelEventDelegate) NotifyJoin(n *Node) {
52	c.Ch <- NodeEvent{NodeJoin, n}
53}
54
55func (c *ChannelEventDelegate) NotifyLeave(n *Node) {
56	c.Ch <- NodeEvent{NodeLeave, n}
57}
58
59func (c *ChannelEventDelegate) NotifyUpdate(n *Node) {
60	c.Ch <- NodeEvent{NodeUpdate, n}
61}
62