1414// You should have received a copy of the GNU Lesser General Public License
1515// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
1616
17- // Package event implements an event multiplexer .
17+ // Package event deals with subscriptions to real-time events .
1818package event
1919
2020import (
@@ -25,33 +25,22 @@ import (
2525 "time"
2626)
2727
28- // Event is a time-tagged notification pushed to subscribers.
29- type Event struct {
28+ // TypeMuxEvent is a time-tagged notification pushed to subscribers.
29+ type TypeMuxEvent struct {
3030 Time time.Time
3131 Data interface {}
3232}
3333
34- // Subscription is implemented by event subscriptions.
35- type Subscription interface {
36- // Chan returns a channel that carries events.
37- // Implementations should return the same channel
38- // for any subsequent calls to Chan.
39- Chan () <- chan * Event
40-
41- // Unsubscribe stops delivery of events to a subscription.
42- // The event channel is closed.
43- // Unsubscribe can be called more than once.
44- Unsubscribe ()
45- }
46-
4734// A TypeMux dispatches events to registered receivers. Receivers can be
4835// registered to handle events of certain type. Any operation
4936// called after mux is stopped will return ErrMuxClosed.
5037//
5138// The zero value is ready to use.
39+ //
40+ // Deprecated: use Feed
5241type TypeMux struct {
5342 mutex sync.RWMutex
54- subm map [reflect.Type ][]* muxsub
43+ subm map [reflect.Type ][]* TypeMuxSubscription
5544 stopped bool
5645}
5746
@@ -61,7 +50,7 @@ var ErrMuxClosed = errors.New("event: mux closed")
6150// Subscribe creates a subscription for events of the given types. The
6251// subscription's channel is closed when it is unsubscribed
6352// or the mux is closed.
64- func (mux * TypeMux ) Subscribe (types ... interface {}) Subscription {
53+ func (mux * TypeMux ) Subscribe (types ... interface {}) * TypeMuxSubscription {
6554 sub := newsub (mux )
6655 mux .mutex .Lock ()
6756 defer mux .mutex .Unlock ()
@@ -72,15 +61,15 @@ func (mux *TypeMux) Subscribe(types ...interface{}) Subscription {
7261 close (sub .postC )
7362 } else {
7463 if mux .subm == nil {
75- mux .subm = make (map [reflect.Type ][]* muxsub )
64+ mux .subm = make (map [reflect.Type ][]* TypeMuxSubscription )
7665 }
7766 for _ , t := range types {
7867 rtyp := reflect .TypeOf (t )
7968 oldsubs := mux .subm [rtyp ]
8069 if find (oldsubs , sub ) != - 1 {
8170 panic (fmt .Sprintf ("event: duplicate type %s in Subscribe" , rtyp ))
8271 }
83- subs := make ([]* muxsub , len (oldsubs )+ 1 )
72+ subs := make ([]* TypeMuxSubscription , len (oldsubs )+ 1 )
8473 copy (subs , oldsubs )
8574 subs [len (oldsubs )] = sub
8675 mux .subm [rtyp ] = subs
@@ -92,7 +81,7 @@ func (mux *TypeMux) Subscribe(types ...interface{}) Subscription {
9281// Post sends an event to all receivers registered for the given type.
9382// It returns ErrMuxClosed if the mux has been stopped.
9483func (mux * TypeMux ) Post (ev interface {}) error {
95- event := & Event {
84+ event := & TypeMuxEvent {
9685 Time : time .Now (),
9786 Data : ev ,
9887 }
@@ -125,7 +114,7 @@ func (mux *TypeMux) Stop() {
125114 mux .mutex .Unlock ()
126115}
127116
128- func (mux * TypeMux ) del (s * muxsub ) {
117+ func (mux * TypeMux ) del (s * TypeMuxSubscription ) {
129118 mux .mutex .Lock ()
130119 for typ , subs := range mux .subm {
131120 if pos := find (subs , s ); pos >= 0 {
@@ -139,7 +128,7 @@ func (mux *TypeMux) del(s *muxsub) {
139128 s .mux .mutex .Unlock ()
140129}
141130
142- func find (slice []* muxsub , item * muxsub ) int {
131+ func find (slice []* TypeMuxSubscription , item * TypeMuxSubscription ) int {
143132 for i , v := range slice {
144133 if v == item {
145134 return i
@@ -148,14 +137,15 @@ func find(slice []*muxsub, item *muxsub) int {
148137 return - 1
149138}
150139
151- func posdelete (slice []* muxsub , pos int ) []* muxsub {
152- news := make ([]* muxsub , len (slice )- 1 )
140+ func posdelete (slice []* TypeMuxSubscription , pos int ) []* TypeMuxSubscription {
141+ news := make ([]* TypeMuxSubscription , len (slice )- 1 )
153142 copy (news [:pos ], slice [:pos ])
154143 copy (news [pos :], slice [pos + 1 :])
155144 return news
156145}
157146
158- type muxsub struct {
147+ // TypeMuxSubscription is a subscription established through TypeMux.
148+ type TypeMuxSubscription struct {
159149 mux * TypeMux
160150 created time.Time
161151 closeMu sync.Mutex
@@ -166,13 +156,13 @@ type muxsub struct {
166156 // postC can be set to nil without affecting the return value of
167157 // Chan.
168158 postMu sync.RWMutex
169- readC <- chan * Event
170- postC chan <- * Event
159+ readC <- chan * TypeMuxEvent
160+ postC chan <- * TypeMuxEvent
171161}
172162
173- func newsub (mux * TypeMux ) * muxsub {
174- c := make (chan * Event )
175- return & muxsub {
163+ func newsub (mux * TypeMux ) * TypeMuxSubscription {
164+ c := make (chan * TypeMuxEvent )
165+ return & TypeMuxSubscription {
176166 mux : mux ,
177167 created : time .Now (),
178168 readC : c ,
@@ -181,16 +171,16 @@ func newsub(mux *TypeMux) *muxsub {
181171 }
182172}
183173
184- func (s * muxsub ) Chan () <- chan * Event {
174+ func (s * TypeMuxSubscription ) Chan () <- chan * TypeMuxEvent {
185175 return s .readC
186176}
187177
188- func (s * muxsub ) Unsubscribe () {
178+ func (s * TypeMuxSubscription ) Unsubscribe () {
189179 s .mux .del (s )
190180 s .closewait ()
191181}
192182
193- func (s * muxsub ) closewait () {
183+ func (s * TypeMuxSubscription ) closewait () {
194184 s .closeMu .Lock ()
195185 defer s .closeMu .Unlock ()
196186 if s .closed {
@@ -205,7 +195,7 @@ func (s *muxsub) closewait() {
205195 s .postMu .Unlock ()
206196}
207197
208- func (s * muxsub ) deliver (event * Event ) {
198+ func (s * TypeMuxSubscription ) deliver (event * TypeMuxEvent ) {
209199 // Short circuit delivery if stale event
210200 if s .created .After (event .Time ) {
211201 return
0 commit comments