eventbus

package module
v0.0.0-...-ba632fe Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 14, 2023 License: MIT Imports: 3 Imported by: 0

README

event-bus

Overview

This is an event bus library for usage in Go programs. It is a powerful and straightforward event management tool for Go applications. It enables easy subscription to and publication of events on various topics identified by string-based topics. It also allows publishers to supply data. This library is ideal for applications requiring a simple yet effective event-driven architecture.

Features

  • Event Subscription: Subscribe to events using string-based topics.
  • Generic Event Data: Publish events with generic/typed data.
  • Sequential Event Processing: Processes published events in sequence within a single Go routine.
  • Subscription Management: Manage subscriptions with subscriber IDs, allowing for easy unregistration.
  • Dynamic Buffer Size: Configure the internal buffer size for event handling.

Installation

Install the library using the following Go command:

go get github.com/enterprizesoftware/event-bus

Getting Started

Initialization
package main

import (
	"github.com/enterprizesoftware/event-bus"
)

func main() {
    bus := eventbus.New[string]()
    defer bus.Stop()
}
Subscribing to Topics
sub, err := bus.Subscribe("my-topic", func(msg string) error {
    fmt.Println("Received message:", msg)
    return nil
})
if err != nil {
    // handle subscription error
}
Publishing Events
bus.Publish("my-topic", "Hello, EventBus!")
Unsubscribing from Topics

// Handle - locates and removes the subscription with specified handle ID.
bus.Unsubscribe(eventbus.Handle(sub.Handle))

// Topic - removes all subscriptions for a givent topic.
bus.Unsubscribe(eventbus.Topic("my-topic"))

// Subscriber - removes all subscriptions for a given subscriber.
bus.Unsubscribe(eventbus.Subscriber(sub))

// Topic & Subscriber - removes all subscriptions for a given topic and subscriber.
bus.Unsubscribe(eventbus.Topic("my-topic"), eventbus.Subscriber(sub))
Error Handling

If an error or a panic occurs during the execution of messageHandler, then the system will call an error handler if one is provided.


sub, err := bus.Subscribe("my-topic", messageHandler, eventbus.Error(myErrorHandler))
Customizing EventBus
bus := eventbus.New[string](eventbus.BufferSize(200))

Contributing

We welcome contributions to this project. Please submit pull requests with your proposed changes or improvements.

License

This library is under the MIT License. See the LICENSE file in the repository for more details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type EventBus

type EventBus[T any] struct {
	// contains filtered or unexported fields
}

func New

func New[T any](opts ...Option) *EventBus[T]

func (*EventBus[T]) Publish

func (b *EventBus[T]) Publish(topic string, data T)

func (*EventBus[T]) Start

func (b *EventBus[T]) Start() *EventBus[T]

func (*EventBus[T]) Stop

func (b *EventBus[T]) Stop()

func (*EventBus[T]) Subscribe

func (b *EventBus[T]) Subscribe(topic string, handler func(T) error, opts ...SubscriptionOption) (Subscription[T], error)

func (*EventBus[T]) Unsubscribe

func (b *EventBus[T]) Unsubscribe(opts ...SubscriptionOption)

type Option

type Option func(*eventBusInfo)

func BufferSize

func BufferSize(size int) Option

type Subscription

type Subscription[T any] struct {
	// contains filtered or unexported fields
}

type SubscriptionOption

type SubscriptionOption func(builder *subscriptionInfo)

func Error

func Error(fn func(error)) SubscriptionOption

func Handle

func Handle(handle int64) SubscriptionOption

func Replace

func Replace() SubscriptionOption

func Subscriber

func Subscriber(id string) SubscriptionOption

func Topic

func Topic(topic string) SubscriptionOption

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL