sonar

package module
v2.0.5 Latest Latest
Warning

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

Go to latest
Published: May 9, 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/v2
Usage

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

Example
import (
	"fmt"
	"context"

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

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

    hnd := func(ctx context.Context, event *sonar.Event) (halt bool, err error) {
        eStr := event.Info().(string) // cast event info parameter 
        
        // ...do some useful stuff...
        return false, nil
    }
    a.Subscribe("user_created", hnd)

    _ = a.Post(context.TODO(), "user_created", &sonar.Event{
        Info: "event info",
    })
}
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 struct {
	// Info contains the associated event info.
	Info interface{}

	// Sender is the object that generated the event.
	Sender interface{}
}

Event defines a sonar event type.

type Handler

type Handler func(ctx context.Context, event *Event) (halt bool, err 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, topic string, ev *Event) (halted bool, err error)

Post posts a given event to the sonar. All topic associated handlers will be executed in order based on the predefined priority value. Execution will be halted either if a handler returns an error or explicitly indicates it.

func (*Sonar) Subscribe

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

Subscribe adds a new handler to a given topic applying default priority.

func (*Sonar) SubscribePriority

func (s *Sonar) SubscribePriority(topic string, hnd Handler, priority int32)

SubscribePriority adds a new handler to a given topic providing an execution priority value. hnd priority may be any number (including negative). Handlers with a higher priority are executed first.

func (*Sonar) Unsubscribe

func (s *Sonar) Unsubscribe(topic string, hnd Handler)

Unsubscribe removes a topic registered handler.

Jump to

Keyboard shortcuts

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