Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/theodesp/sima

Sima is a simple object to object or broadcast dispatching system for Go
https://github.com/theodesp/sima

broadcast-reciever dispatcher golang-library object-to-object signalling

Last synced: 20 days ago
JSON representation

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

Awesome Lists containing this project

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
```bash
$ go get -u github.com/theodesp/sima
```

## Usage
1. Create a topic factory and re-use it for all signals yo want to create:

```go
tf := NewTopicFactory()
onStart := NewSima(tf)
onEnd := NewSima(tf)
```

2. 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.
```go
// 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")
```

3. Now just send some messages and any registered participant will call the handler.
```go
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**
```go
onEnd.HasRecieversFor("on-end-sender") // true
onEnd.HasRecieversFor("on-start-sender") // false
```

## LICENCE
Copyright © 2017 Theo Despoudis MIT license