Skip to content

Commit

Permalink
update singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
werbenhu committed Aug 7, 2023
1 parent 23b1733 commit 75b0546
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 13 deletions.
6 changes: 1 addition & 5 deletions README-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,14 @@ func main() {

### 使用全局的EventBus单例对象

为了更方便的使用EventBus, 这里有一个全局的EventBus单例对象,使用`eventbus.InitSingleton() `初始化这个单例对象,这个对象内部的channel是无缓冲的,直接使用`eventbus.Subscribe()`,`eventbus.Publish()`,`eventbus.Unsubscribe()`,将会调用该单例对象对应的方法。
为了更方便的使用EventBus, 这里有一个全局的EventBus单例对象,这个对象内部的channel是无缓冲的,直接使用`eventbus.Subscribe()`,`eventbus.Publish()`,`eventbus.Unsubscribe()`,将会调用该单例对象对应的方法。

```go
func handler(topic string, payload int) {
fmt.Printf("topic:%s, payload:%d\n", topic, payload)
}

func main() {

// 初始化单例对象
eventbus.InitSingleton()

// eventbus.Subscribe() 将调用全局单例singleton.Subscribe()方法
eventbus.Subscribe("testtopic", handler)

Expand Down
6 changes: 1 addition & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,14 @@ func main() {
```

### Using the global singleton object of EventBus
To make it more convenient to use EventBus, there is a global singleton object for EventBus. You can initialize this singleton object by calling `eventbus.InitSingleton()`. The internal channel of this object is unbuffered, and you can directly use `eventbus.Subscribe()`, `eventbus.Publish()`, and `eventbus.Unsubscribe()` to call the corresponding methods of the singleton object.
To make it more convenient to use EventBus, there is a global singleton object for EventBus. The internal channel of this object is unbuffered, and you can directly use `eventbus.Subscribe()`, `eventbus.Publish()`, and `eventbus.Unsubscribe()` to call the corresponding methods of the singleton object.

```go
func handler(topic string, payload int) {
fmt.Printf("topic:%s, payload:%d\n", topic, payload)
}

func main() {

// Initialize the singleton object
eventbus.InitSingleton()

// eventbus.Subscribe() will call the global singleton's Subscribe() method
eventbus.Subscribe("testtopic", handler)

Expand Down
2 changes: 1 addition & 1 deletion singleton.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var (
)

// InitSingleton initializes the singleton instance of EventBus, which will be created only when necessary.
func InitSingleton() {
func init() {
if singleton == nil {

// If singleton is nil, we create a new instance of EventBus using the New()
Expand Down
6 changes: 4 additions & 2 deletions singleton_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import (
)

func recreateSingleton() {
singleton = nil
InitSingleton()
if singleton != nil {
singleton.Close()
}
singleton = New()
}

func Test_InitSingleton(t *testing.T) {
Expand Down

0 comments on commit 75b0546

Please sign in to comment.