godiator

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

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

Go to latest
Published: Aug 9, 2018 License: Apache-2.0 Imports: 1 Imported by: 0

README

godiator

Build Status

Godiator is Simple Pub&Sub implementation For Golang.

Get Package

go get github.com/blueskan/godiator

Simple Usage Example
package main

import (
    "fmt"

    . "github.com/blueskan/godiator"
)

func main() {
	godiator := NewGodiator()

	// Subscription
	godiator.
		Subscribe("notification:user:registration", func(event Event) {
			// E-mail handler

			fmt.Println(event.Payload.(string) + ": Sending email...")
		}).
		Subscribe("notification:user:registration", func(event Event) {
			// Sms handler

			fmt.Println(event.Payload.(string) + ": Sending sms...")
		})

	// Publish
	godiator.Publish(Event{
		Name:    "notification:user:registration",
		Payload: "New user created",
	})

}

You can send any type of payload maps, arrays, another structs or even nil (default value)

GoDoc

https://godoc.org/github.com/blueskan/godiator

TODO
  • Async message handling (just for now it's just handling sync)
  • Any pull requests or ideas will be appreciated

Documentation

Overview

Package godiator is simple Pub&Sub package. You can simply adds any numbers of events and adds any numbers of handlers. You can publish events and handles this events with your handlers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Event

type Event struct {
	Name    string
	Payload interface{}
}

Event struct is simply contains name as string and payload as any type of value, you can send maps, arrays, simple strings or even nil (interface{} default value)

type Godiator

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

Godiator struct is representing main struct for Godiator, also its store subscriptions.

func NewGodiator

func NewGodiator() *Godiator

NewGodiator Create new godiator instance

func (*Godiator) Publish

func (gb *Godiator) Publish(event Event) error

Publish events to correct handlers, also this method returns error if it can`t find any handlers for event.

func (*Godiator) Subscribe

func (gb *Godiator) Subscribe(eventName string, eventHandler Handler) *Godiator

Subscribe events with event name and binds with event handler also this method returns godiator instance you can chain any number of handlers same or different events.

type Handler

type Handler func(Event)

Handler type represent func()Event type represented as EventHandler.

Jump to

Keyboard shortcuts

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