eventbus

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2021 License: MIT Imports: 1 Imported by: 0

README

Go EventBus

Introduction

This package provides a simple yet powerful event bus.

  • Simple Pub/Sub
  • Async Publishing of events
  • Wildcard Support

Installation

go get github.com/dtomasi/go-event-bus
package main

import "github.com/dtomasi/go-event-bus"

Usage

Simple

Subscribe and Publish events using a simple callback function

package main

import "github.com/dtomasi/go-event-bus"

func main()  {
     
    // Create a new instance
    eb := eventbus.NewEventBus()
    
    // Subscribe to "foo:baz" - or use a wildcard like "foo:*"
    eb.SubscribeCallback("foo:baz", func(topic string, data interface{}) {
        println(topic)
        println(data)
    })
    
    // Publish data to topic
    eb.Publish("foo:baz", "bar")
}
Synchronous using Channels

Subscribe using a EventChannel

package main

import "github.com/dtomasi/go-event-bus"

func main()  {
     
    // Create a new instance
    eb := eventbus.NewEventBus()
    
    // Subscribe to "foo:baz" - or use a wildcard like "foo:*"
	eventChannel := eb.Subscribe("foo:baz")

	// Subscribe with existing channel use
	// eb.SubscribeChannel("foo:*", eventChannel)
	
    // Wait for the incoming event on the channel
    go func() {
        evt :=<-eventChannel
        println(evt.Topic)
        println(evt.Data)
        
        // Tell eventbus that you are done
        // This is only needed for synchronous publishing
        evt.Done()
    }()

    // Publish data to topic
    eb.Publish("foo:baz", "bar")
}
Async

Publish asynchronously

package main

import "github.com/dtomasi/go-event-bus"

func main()  {
     
    // Create a new instance
    eb := eventbus.NewEventBus()

	// Subscribe to "foo:baz" - or use a wildcard like "foo:*"
	eventChannel := eb.Subscribe("foo:baz")

	// Subscribe with existing channel use
	// eb.SubscribeChannel("foo:*", eventChannel)

    // Wait for the incoming event on the channel
    go func() {
        evt :=<-eventChannel
        println(evt.Topic)
        println(evt.Data)
    }()

    // Publish data to topic asynchronously
    eb.PublishAsync("foo:baz", "bar")
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CallbackFunc

type CallbackFunc func(topic string, data interface{})

Defines a CallbackFunc

type Event

type Event struct {
	Data  interface{}
	Topic string
	// contains filtered or unexported fields
}

Event holds topic name and data

func (*Event) Done

func (e *Event) Done()

Done calls Done on sync.WaitGroup if set

type EventBus

type EventBus struct {
	// contains filtered or unexported fields
}

EventBus stores the information about subscribers interested for a particular topic

func GetBus

func GetBus() *EventBus

GetBus returns the default EventBus instance

func NewEventBus

func NewEventBus() *EventBus

NewEventBus returns a new EventBus instance

func (*EventBus) Publish

func (eb *EventBus) Publish(topic string, data interface{})

Publish data to a topic and wait for all subscribers to finish This function creates a waitGroup internally. All subscribers must call Done() function on Event

func (*EventBus) PublishAsync

func (eb *EventBus) PublishAsync(topic string, data interface{})

PublishAsync data to a topic asynchronously This function returns a bool channel which indicates that all subscribers where called

func (*EventBus) Subscribe

func (eb *EventBus) Subscribe(topic string) EventChannel

Subscribe to a topic passing a EventChannel

func (*EventBus) SubscribeCallback

func (eb *EventBus) SubscribeCallback(topic string, callable CallbackFunc)

SubscribeCallback provides a simple wrapper that allows to directly register CallbackFunc instead of channels

func (*EventBus) SubscribeChannel added in v1.0.0

func (eb *EventBus) SubscribeChannel(topic string, ch EventChannel)

Subscribe with a given Channel

type EventChannel

type EventChannel chan Event

EventChannel is a channel which can accept an Event

func NewEventChannel

func NewEventChannel() EventChannel

Creates a new EventChannel

Jump to

Keyboard shortcuts

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