Skip to content

Commit

Permalink
Add test and go doc badges to readme
Browse files Browse the repository at this point in the history
  • Loading branch information
jsphpl committed Dec 1, 2023
1 parent 46e85ba commit 7109460
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 21 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ name: Unit Test
on: [push]

jobs:
build:
test:
name: Unit Test

runs-on: ubuntu-latest
strategy:
Expand Down
43 changes: 23 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,50 +1,53 @@
# typevent

[![Unit Tests](https://github.com/sehrgutesoftware/typevent/actions/workflows/test.yml/badge.svg)](https://github.com/sehrgutesoftware/typevent/actions/workflows/test.yml)
[![Go Reference](https://pkg.go.dev/badge/github.com/sehrgutesoftware/typevent.svg)](https://pkg.go.dev/github.com/sehrgutesoftware/typevent)

typevent provides type-safe event messaging channels for Go. They can be used to implement Pub/Sub schemes without the need for type assertions, streamlining the application code that uses the event channels.

At the core it consists of a generic `Channel` interface with the following methods:

```go
type Channel[E Event] interface {
// Emit emits an event of type E on the channel.
Emit(E) error
// Subscribe registers a handler for events of type E on the channel.
Subscribe(ctx context.Context, handler Handler[E]) (Subscription, error)
// Emit emits an event of type E on the channel.
Emit(E) error
// Subscribe registers a handler for events of type E on the channel.
Subscribe(ctx context.Context, handler Handler[E]) (Subscription, error)
}
```

The package currently provides one implementation of the interface, using [Redis Pub/Sub](https://redis.io/docs/interact/pubsub/) as the backing distribution system. Usage can look as follows:

```go
import (
"context"
"fmt"
"context"
"fmt"

redisclient "github.com/redis/go-redis/v9"
"github.com/sehrgutesoftware/typevent/redis"
redisclient "github.com/redis/go-redis/v9"
"github.com/sehrgutesoftware/typevent/redis"
)

func ExampleNewChannel() {
type event string

// Create a new channel using redis Pub/Sub as the underlying event bus.
client := redisclient.NewClient(&redisclient.Options{Addr: "localhost:6379"})
// Create a new channel using redis Pub/Sub as the underlying event bus.
client := redisclient.NewClient(&redisclient.Options{Addr: "localhost:6379"})

// conf holds the redis client used by the channel
conf := redis.NewConfig(client)
conf := redis.NewConfig(client)

// This is where we create the channel that can be used to emit and subscribe to events
channel := redis.NewChannel[event](conf, "CHANNEL_NAME")
channel := redis.NewChannel[event](conf, "CHANNEL_NAME")

// Register a subscriber for the channel.
sub, _ := channel.Subscribe(context.Background(), func(ctx context.Context, ev event) error {
fmt.Printf("subscriber says: %s\n", ev)
return nil
})
defer sub.Close()
// Register a subscriber for the channel.
sub, _ := channel.Subscribe(context.Background(), func(ctx context.Context, ev event) error {
fmt.Printf("subscriber says: %s\n", ev)
return nil
})
defer sub.Close()

// Emit an event on the channel.
channel.Emit("Hello World!")
// Emit an event on the channel.
channel.Emit("Hello World!")
}

```
Expand Down

0 comments on commit 7109460

Please sign in to comment.