theodesp / sima

Sima is a simple object to object or broadcast dispatching system for Go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

About

Sima is a simple object to object or broadcast dispatching system for Go

License:MIT License


Languages

Language:Go 94.1%Language:Makefile 5.9%