Skip to content

Commit

Permalink
update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
werbenhu committed Aug 7, 2023
1 parent 75b0546 commit 4e0f6bf
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 77 deletions.
76 changes: 37 additions & 39 deletions README-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,45 +35,7 @@ EventBus同时支持同步和异步的方式发布消息。EventBus使用一个C
同步的方式下EventBus不使用channel,而是通过直接调用handler将消息传递给订阅者。如果想同步的方式发布消息,使用eventbus.PublishSync()函数即可。


### EventBus 示例
```go
package main

import (
"fmt"
"time"

"github.com/werbenhu/eventbus"
)

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

func main() {
bus := eventbus.New()

// Subscribe() 订阅一个主题,如果handler不是函数则返回错误。
// handler必须有两个参数:第一个参数必须是字符串类型,
// handler的第二个参数类型必须与 `Publish()` 中的 payload 类型一致。
bus.Subscribe("testtopic", handler)

// 异步方式发布消息
bus.Publish("testtopic", 100)

// 同步方式发布消息
bus.PublishSync("testtopic", 200)

// 订阅者接收消息。为了确保订阅者可以接收完所有消息的异步消息,这里在取消订阅之前给了一点延迟。
time.Sleep(time.Millisecond)
bus.Unsubscribe("testtopic", handler)
bus.Close()
}
```

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

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

```go
func handler(topic string, payload int) {
Expand Down Expand Up @@ -110,6 +72,42 @@ func main() {
}
```

### 创建eventbus对象
```go
package main

import (
"fmt"
"time"

"github.com/werbenhu/eventbus"
)

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

func main() {
bus := eventbus.New()

// Subscribe() 订阅一个主题,如果handler不是函数则返回错误。
// handler必须有两个参数:第一个参数必须是字符串类型,
// handler的第二个参数类型必须与 `Publish()` 中的 payload 类型一致。
bus.Subscribe("testtopic", handler)

// 异步方式发布消息
bus.Publish("testtopic", 100)

// 同步方式发布消息
bus.PublishSync("testtopic", 200)

// 订阅者接收消息。为了确保订阅者可以接收完所有消息的异步消息,这里在取消订阅之前给了一点延迟。
time.Sleep(time.Millisecond)
bus.Unsubscribe("testtopic", handler)
bus.Close()
}
```

## 使用Pipe代替Channel

Pipe 将通道封装成泛型对象,泛型参数对应channle里的类型,这里没有主题的概念。
Expand Down
75 changes: 37 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,43 @@ In EventBus, each topic corresponds to a channel. The `Publish()` method pushes

In the synchronous way, EventBus does not use channels, but passes payloads to subscribers by calling the handler directly. To publish messages synchronously, use the `eventbus.PublishSync()` function.

### eventbus example
### Simple Usage

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

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

var wg sync.WaitGroup
wg.Add(1)
go func() {
// Asynchronously publish messages
for i := 0; i < 100; i++ {
// eventbus.Publish() will call the global singleton's Publish() method
eventbus.Publish("testtopic", i)
}
// Synchronously publish messages
for i := 100; i < 200; i++ {
eventbus.PublishSync("testtopic", i)
}
wg.Done()
}()
wg.Wait()

time.Sleep(time.Millisecond)
// eventbus.Unsubscribe() will call the global singleton's Unsubscribe() method
eventbus.Unsubscribe("testtopic", handler)

// eventbus.Close() will call the global singleton's Close() method
eventbus.Close()
}
```

### Creating an EventBus Object
```go
package main

Expand Down Expand Up @@ -78,43 +114,6 @@ 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. 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() {
// eventbus.Subscribe() will call the global singleton's Subscribe() method
eventbus.Subscribe("testtopic", handler)

var wg sync.WaitGroup
wg.Add(1)
go func() {
// Asynchronously publish messages
for i := 0; i < 100; i++ {
// eventbus.Publish() will call the global singleton's Publish() method
eventbus.Publish("testtopic", i)
}
// Synchronously publish messages
for i := 100; i < 200; i++ {
eventbus.PublishSync("testtopic", i)
}
wg.Done()
}()
wg.Wait()

time.Sleep(time.Millisecond)
// eventbus.Unsubscribe() will call the global singleton's Unsubscribe() method
eventbus.Unsubscribe("testtopic", handler)

// eventbus.Close() will call the global singleton's Close() method
eventbus.Close()
}
```

## Use Pipe instead of channel

Pipe is a wrapper for a channel without the concept of topics, with the generic parameter corresponding to the type of the channel. `eventbus.NewPipe[T]()` is equivalent to `make(chan T)`. Publishers publish messages, and subscribers receive messages. You can use the `Pipe.Publish()` method instead of `chan <-`, and the `Pipe.Subscribe()` method instead of `<-chan`.
Expand Down

0 comments on commit 4e0f6bf

Please sign in to comment.