Skip to content

Commit 6906904

Browse files
committed
event: make TypeMux zero value ready to use
1 parent 10bbf26 commit 6906904

2 files changed

Lines changed: 12 additions & 13 deletions

File tree

‎event/event.go‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ type Subscription interface {
2323
// A TypeMux dispatches events to registered receivers. Receivers can be
2424
// registered to handle events of certain type. Any operation
2525
// called after mux is stopped will return ErrMuxClosed.
26+
//
27+
// The zero value is ready to use.
2628
type TypeMux struct {
2729
mutex sync.RWMutex
2830
subm map[reflect.Type][]*muxsub
@@ -32,21 +34,18 @@ type TypeMux struct {
3234
// ErrMuxClosed is returned when Posting on a closed TypeMux.
3335
var ErrMuxClosed = errors.New("event: mux closed")
3436

35-
// NewTypeMux creates a running mux.
36-
func NewTypeMux() *TypeMux {
37-
return &TypeMux{subm: make(map[reflect.Type][]*muxsub)}
38-
}
39-
4037
// Subscribe creates a subscription for events of the given types. The
4138
// subscription's channel is closed when it is unsubscribed
4239
// or the mux is closed.
4340
func (mux *TypeMux) Subscribe(types ...interface{}) Subscription {
4441
sub := newsub(mux)
4542
mux.mutex.Lock()
4643
if mux.stopped {
47-
mux.mutex.Unlock()
4844
close(sub.postC)
4945
} else {
46+
if mux.subm == nil {
47+
mux.subm = make(map[reflect.Type][]*muxsub)
48+
}
5049
for _, t := range types {
5150
rtyp := reflect.TypeOf(t)
5251
oldsubs := mux.subm[rtyp]
@@ -55,8 +54,8 @@ func (mux *TypeMux) Subscribe(types ...interface{}) Subscription {
5554
subs[len(oldsubs)] = sub
5655
mux.subm[rtyp] = subs
5756
}
58-
mux.mutex.Unlock()
5957
}
58+
mux.mutex.Unlock()
6059
return sub
6160
}
6261

‎event/event_test.go‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
type testEvent int
1111

1212
func TestSub(t *testing.T) {
13-
mux := NewTypeMux()
13+
mux := new(TypeMux)
1414
defer mux.Stop()
1515

1616
sub := mux.Subscribe(testEvent(0))
@@ -28,7 +28,7 @@ func TestSub(t *testing.T) {
2828
}
2929

3030
func TestMuxErrorAfterStop(t *testing.T) {
31-
mux := NewTypeMux()
31+
mux := new(TypeMux)
3232
mux.Stop()
3333

3434
sub := mux.Subscribe(testEvent(0))
@@ -41,7 +41,7 @@ func TestMuxErrorAfterStop(t *testing.T) {
4141
}
4242

4343
func TestUnsubscribeUnblockPost(t *testing.T) {
44-
mux := NewTypeMux()
44+
mux := new(TypeMux)
4545
defer mux.Stop()
4646

4747
sub := mux.Subscribe(testEvent(0))
@@ -62,7 +62,7 @@ func TestUnsubscribeUnblockPost(t *testing.T) {
6262

6363
func TestMuxConcurrent(t *testing.T) {
6464
rand.Seed(time.Now().Unix())
65-
mux := NewTypeMux()
65+
mux := new(TypeMux)
6666
defer mux.Stop()
6767

6868
recv := make(chan int)
@@ -111,7 +111,7 @@ func emptySubscriber(mux *TypeMux, types ...interface{}) {
111111
}
112112

113113
func BenchmarkPost3(b *testing.B) {
114-
var mux = NewTypeMux()
114+
var mux = new(TypeMux)
115115
defer mux.Stop()
116116
emptySubscriber(mux, testEvent(0))
117117
emptySubscriber(mux, testEvent(0))
@@ -123,7 +123,7 @@ func BenchmarkPost3(b *testing.B) {
123123
}
124124

125125
func BenchmarkPostConcurrent(b *testing.B) {
126-
var mux = NewTypeMux()
126+
var mux = new(TypeMux)
127127
defer mux.Stop()
128128
emptySubscriber(mux, testEvent(0))
129129
emptySubscriber(mux, testEvent(0))

0 commit comments

Comments
 (0)