sonar

package module
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Feb 11, 2021 License: Apache-2.0 Imports: 4 Imported by: 0

README

sonar

Build Status Go Report Card Coverage Godoc Releases LICENSE

Installation
go get -u github.com/jackal-xmpp/sonar
Usage

The sonar package exposes an interface for posting and listening event messages.

Example
import (
	"fmt"
	"context"

	"github.com/jackal-xmpp/sonar"
)

func main() {
	a := sonar.New()

    hnd := func(ctx context.Context, event sonar.Event) error {
        eStr := event.Info().(string) // cast event info parameter 
        
        // ...do some useful stuff...
        return nil
    }   
    sub := a.Subscribe("user_created", hnd)
    
    e := sonar.NewEventBuilder("user_created").
        WithInfo("event info").
        Build()

    _ = a.Post(context.TODO(), e)
}
Contributing
  • Fork it
  • Create your feature branch (git checkout -b my-new-feature)
  • Commit your changes (git commit -am 'Add some feature')
  • Push to the branch (git push origin my-new-feature)
  • Create new Pull Request
License

Apache License 2.0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Event

type Event interface {
	// Name represents the name of the event.
	Name() string

	// Info contains the data related to a particular event.
	Info() interface{}

	// Sender represents the object that generated a particular event.
	Sender() interface{}

	// Time specifies the time in which a concrete event was generated.
	Time() time.Time
}

Event represents a container for information broadcast through an sonar to all registered handlers.

type EventBuilder

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

EventBuilder represents a generic event builder.

func NewEventBuilder

func NewEventBuilder(eventName string) *EventBuilder

NewEventBuilder returns a new event builder instance.

func (*EventBuilder) Build

func (b *EventBuilder) Build() Event

Build returns a new generated event.

func (*EventBuilder) WithInfo

func (b *EventBuilder) WithInfo(info interface{}) *EventBuilder

WithInfo attaches info field to the builder.

func (*EventBuilder) WithSender

func (b *EventBuilder) WithSender(sender interface{}) *EventBuilder

WithSender attaches sender object to the builder.

type Handler

type Handler func(ctx context.Context, event Event) error

Handler defines a generic event handler function.

type Sonar

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

Sonar defines common publish/subscribe interface.

func New

func New() *Sonar

New returns a new initialized sonar instance.

func (*Sonar) Post

func (s *Sonar) Post(ctx context.Context, event Event) error

Post posts a given event to the sonar.

func (*Sonar) Subscribe

func (s *Sonar) Subscribe(topic string, hnd Handler) SubID

Subscribe adds an entry to the sonar to receive events that passed to the provided handler.

func (*Sonar) Unsubscribe

func (s *Sonar) Unsubscribe(subID SubID)

Unsubscribe removes an existing entry from the sonar.

type SubID added in v0.12.0

type SubID uint64

SubID represents a unique subscription identifier

Jump to

Keyboard shortcuts

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