GitHub - sadlil/go-trigger: A Global event triggerer for golang. Defines functions as event with id string. Trigger the event anywhere from your project. (original) (raw)

go-trigger

Go Trigger is a global event trigger for golang. Define an event with a task specified to that event and Trigger it from anywhere you want.

Get The Package

$ go get -u github.com/sadlil/go-trigger

How to switch to a specific version

go get the package. Go to the package directory in your $GOPATH/src. Change the tag using git.go install the package.

$ go get -u github.com/sadlil/go-trigger $ cd $GOPATH/src/github.com/sadlil/go-trigger $ git checkout tags/ $ go install

Currently available Tags

How To Use

1. Global Events

Import the package into your code. Add events with trigger.On method. And call that event handler with trigger.Fire method. All the events added like this will be global events. You can call Fire from anywhere.

package main

import ( "github.com/sadlil/go-trigger" "fmt" )

func main() { trigger.On("first-event", func() { // Do Some Task Here. fmt.Println("Done") }) trigger.Fire("first-event") }

You can define your events from another package

trigger.On("second-event", packagename.FunctionName) trigger.Fire("second-event")

You can define events with parameteres and return types.

func TestFunc(a, b int) int { return a + b }

// Call them using trigger.On("third-event", TestFunc) values, err := trigger.Fire("third-event", 5, 6)

// IMPORTANT : You need to type convert Your Returned Values using // values[0].Int()

You can define your event in one package and trigger it another package. Your event and trigger are global. Define anywhere, fire anywhere. You can define any function in any package as event you only need to import the function's specified package where you define the event. Where you trigger the event, you do not need to import it there.

//--------------------------------------------- package a

func AFunction(one, two int) int { return one + two }

//--------------------------------------------- package b import ( "yourdirectory/a" "github.com/sadlil/go-trigger" )

func() { trigger.On("new-event", a.AFunction) }

//--------------------------------------------- package c import ( "github.com/sadlil/go-trigger" )

func() { values, err := trigger.Fire("new-event", 10, 10) // You don't need to import package a here. fmt.Println(values[0].Int()) }

You can run events in background with FireBackground()

func main() { trigger.On("first-event", func() { for i := 1; i <= 1000; i++ { fmt.Println(i) } }) channel, err := trigger.FireBackground("first-event") fmt.Println("Event runs") //read the returned channel values := <- channel

trigger.FireBackground("first-event") fmt.Println("Running 2nd Event") }

2. Local Events

Trigger instance that will not effecct the global event. All event added to an local event instace can call only via this trigger instance. This is implementation of plugable Trigger interface.

Create a local trigger instance,

package main

import ( "github.com/sadlil/go-trigger" "fmt" )

func main() { t := trigger.New() t.On("first-event", func() { // Do Some Task Here. fmt.Println("Done") }) t.Fire("first-event")

// t2 is another trigger instance that will be separate from t1. t2 := trigger.New() t2.On("first-event", func() { // Do Some Task Here. fmt.Println("Done") }) t2.Fire("first-event") }

All other methods are availabe on any local trigger instance

Available methods

On(event string, task interface{}) error

Fire(event string, params ...interface{}) ([]reflect.Value, error)

FireBackground(event string, params ...interface{}) (chan []reflect.Value, error)

Clear(event string) error

ClearEvents()

HasEvent(event string) bool

Events() []string

EventCount() int

Under Development Features

  1. Multiple event handler for a event.

Licence

Licenced under MIT Licence
Any Suggestions and Bug Report will be gladly appreciated.