sima

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

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

Go to latest
Published: Jan 14, 2018 License: MIT Imports: 10 Imported by: 0

README

sima

GoDoc License

Sima is a simple object to object or broadcast dispatching system. Any number of interested parties can subscribe to events. Signal receives can receive also signals from specific senders.

Installation

$ go get -u github.com/theodesp/sima

Usage

  1. Create a topic factory and re-use it for all signals yo want to create:
tf := NewTopicFactory()
onStart := NewSima(tf)
onEnd := NewSima(tf)
  1. After you have created your signals, just connect handlers for a particular topic or not. If you don't specify a topic then the handler will be assigned a ALL topic that defaults as a broadcast address.
// Subscribe to ALL
onStart.Connect(func(context context.Context, sender *Topic) interface{} {
		fmt.PrintF("OnStart called from Sender %+v", sender)
    return sender
	}, "")

// Subscribe to specific sender/topic
onEnd.Connect(func(context context.Context, sender *Topic) interface{} {
		fmt.PrintF("onEnd called from Sender %+v", sender)
    return sender
}, "on-end-sender")
  1. Now just send some messages and any registered participant will call the handler.
response := onStart.Dispatch(context.Background(), "") // will handle
response := onStart.Dispatch(context.Background(), "on-start-sender") // will not handle

response := onEnd.Dispatch(context.Background(), "") // will not handle
response := onEnd.Dispatch(context.Background(), "on-end-sender") // will handle
  • Checking for receivers existance
onEnd.HasRecieversFor("on-end-sender") // true
onEnd.HasRecieversFor("on-start-sender") // false

LICENCE

Copyright © 2017 Theo Despoudis MIT license

Documentation

Index

Constants

View Source
const ANY = "ANY"

This initial signal name is imported by default

View Source
const ANY_ID = iota
View Source
const NONE = "NONE"
View Source
const Version = "0.1.0"

The main version number that is being run at the moment.

Variables

View Source
var GitCommit string

The git commit that was compiled. This will be filled in by the compiler.

View Source
var VersionPrerelease = ""

A pre-release marker for the version. If this is "" (empty string) then it means that it is a final release. Otherwise, this is a pre-release such as "dev" (in development)

Functions

func GetFunctionName

func GetFunctionName(f interface{}) string

func HashValue

func HashValue(value interface{}) uint64

Types

type ReceiverType

type ReceiverType = func(context context.Context, sender *Topic) interface{}

type Sima

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

func NewSima

func NewSima(topicFactory *TopicFactory) *Sima

func (*Sima) Connect

func (s *Sima) Connect(receiver ReceiverType, senderName string) ReceiverType

Connects *receiver* of signal events sent by *sender*

func (*Sima) Disconnect

func (s *Sima) Disconnect(receiver ReceiverType, senderName string) bool

Disconnect *receiver* from this signal's events. Returns true if successful or false otherwise

func (*Sima) Dispatch

func (s *Sima) Dispatch(context context.Context, senderName string) []interface{}

Emit this signal on behalf of *sender*, passing on Context. Returns a list of results from the receivers

func (*Sima) GetReceiversFor

func (s *Sima) GetReceiversFor(senderName string) []ReceiverType

Emit this signal on behalf of *sender*, passing on Context. Returns a list of senders that accepted the signal

func (*Sima) HasReceiversFor

func (s *Sima) HasReceiversFor(senderName string) bool

True if there is a receiver for *sender* at the time of the call

type Topic

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

A constant topic symbol

func NewTopic

func NewTopic(name string) *Topic

type TopicFactory

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

func NewTopicFactory

func NewTopicFactory() *TopicFactory

Repeated calls of TopicFactory('name') will all return the same instance.

func (*TopicFactory) GetByName

func (f *TopicFactory) GetByName(name string) *Topic

func (*TopicFactory) Names

func (f *TopicFactory) Names() []interface{}

Jump to

Keyboard shortcuts

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