Documentation ¶
Index ¶
- func Chain(a ...interface{}) interface{}
- func NameOfFunc(fn interface{}) string
- type Hook
- type HookAsyncResultListener
- type HookAsyncRunner
- type Hooks
- type HooksMap
- type Hub
- func (h *Hub) AttachLogger(logger Logger)
- func (h *Hub) GetHooks(name string) (Hooks, bool)
- func (h *Hub) GetHooksFunc(fn interface{}) (Hooks, bool)
- func (h *Hub) Register(name string, callback interface{}) *Hook
- func (h *Hub) RegisterFunc(hookFunc interface{}, callback interface{}) *Hook
- func (h *Hub) Remove(name string, callback interface{}) bool
- func (h *Hub) RemoveFunc(fn interface{}, callback interface{}) bool
- func (h *Hub) RemoveHooks(name string) bool
- func (h *Hub) Run(name string, payloads ...interface{})
- func (h *Hub) RunFunc(hookFunc interface{}, payloads ...interface{})
- type Logger
- type Notifier
- type Priority
- type Registry
- type Source
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Chain ¶
func Chain(a ...interface{}) interface{}
Example ¶
package main import ( "fmt" ) var msgHub = NewHub() type message struct { from string to string body string } func newMessage(from, to, body string) message { fmt.Println("newMessage") return message{ from: from, to: to, body: body, } } // sender part func send(from, to, body string) { msgHub.RunFunc(send, from, to, body) } // receiver part func filterMessage(msg message) error { fmt.Printf("filtering message: %#v\n", msg) if msg.from == "from name" { return nil } return fmt.Errorf("bad message") } func printAnyErr(err error) { if err != nil { fmt.Println(err.Error()) } } func sendValidMessage(msg message) { fmt.Println("Sending message...") fmt.Printf("%#v\n", msg) } func main() { // flow : // -> newMessage(from,to,body) -> filterMessage(message) -> receive(message) // |< if Any error then the printAnyErr will be executed and the chain will be stopped. msgHub.RegisterFunc(send, Chain(newMessage, filterMessage, sendValidMessage, printAnyErr)) send("from name", "to name", "message contents") }
Output: newMessage filtering message: hooks.message{from:"from name", to:"to name", body:"message contents"} Sending message... hooks.message{from:"from name", to:"to name", body:"message contents"}
func NameOfFunc ¶
func NameOfFunc(fn interface{}) string
NameOfFunc returns the name of a function, developers can use that to get the name of a hook function, i.e: instead of RegisterHookFunc(myFunc), user can use RegisterHook(NameOfFunc(myFunc),...) and so on.
Types ¶
type Hook ¶
type Hook struct { // ID will be likely to be used at the future, internally, in order to transfer hooks from other machine to another // (yes I have plans to make it net compatible.) ID string Owner *Hub Name string // Multiple hooks can have the same name, is the event. Source Source Callback reflect.Value // if remains zero then order matters on execution, // they are defaulted to the "IDLE" which doesn't matters if you don't care, // it has nothing to do with performance, is a matter of order. // each group of hooks has its own group, so the priority is per Name in the HooksMap. // // Read-only value, use SetPriority if you want to alt it. Priority Priority // hiher number is the first. // optional descriptionist fields Description string }
func (*Hook) PrioritizeAboveOf ¶
Example ¶
hub := NewHub() hub.Register("myhook", func() { fmt.Println("last") }).SetPriority(Idle) // defaults to Idle already. hub.Register("myhook", func() { fmt.Println("third") }).SetPriority(High) hub.Register("myhook", func() { fmt.Println("forth") }).SetPriority(Normal) firstHook := hub.Register("myhook", func() { fmt.Println("first") }).SetPriority(Realtime) // or anything secondHook := hub.Register("myhook", func() { fmt.Println("second") }).SetPriority(Realtime) // even if Realtime, it can be priortized, remember we work with integers, Priority is just a type of int. firstHook.PrioritizeAboveOf(secondHook) hub.Run("myhook")
Output: first second third forth last
func (*Hook) RunAsync ¶
func (h *Hook) RunAsync(payloads ...interface{}) *HookAsyncRunner
func (*Hook) SetPriority ¶
Example ¶
hub := NewHub() hub.Register("myhook", func() { fmt.Println("last") }).SetPriority(Idle) // defaults to Idle already. hub.Register("myhook", func() { fmt.Println("second") }).SetPriority(Normal) hub.Register("myhook", func() { fmt.Println("third") }).SetPriority(BelowNormal) hub.Register("myhook", func() { fmt.Println("first") }).SetPriority(High) hub.Run("myhook")
Output: first second third last
type HookAsyncResultListener ¶
type HookAsyncRunner ¶
type HookAsyncRunner struct {
// contains filtered or unexported fields
}
func (*HookAsyncRunner) OnComplete ¶
func (runner *HookAsyncRunner) OnComplete(listeners ...HookAsyncResultListener)
type Hub ¶
type Hub struct {
// contains filtered or unexported fields
}
func (*Hub) AttachLogger ¶
func (*Hub) GetHooksFunc ¶
func (*Hub) Register ¶
Example ¶
var ( myHub = NewHub() SayHook = "SAY_HOOK" send = func(message string) { myHub.Run(SayHook, message) } ) // Register two listeners on the same hook "SAY_HOOK". // Order of registrations order until one of the hook listener's // Priority changed, as we see below. myHub.Register(SayHook, func(message string) { fmt.Println("Processing the incoming message: " + message) }).SetPriority(Idle) // default priority myHub.Register(SayHook, func(message string) { fmt.Println("Incoming message: " + message) }).SetPriority(Realtime) // highest priority var messages = []string{ "message1", "message2", } for _, msg := range messages { send(msg) }
Output: Incoming message: message1 Processing the incoming message: message1 Incoming message: message2 Processing the incoming message: message2
func (*Hub) RegisterFunc ¶
func (*Hub) Remove ¶
Remove removes a hook based on a hook name and its callback.
Returns true if the removal succeed.
func (*Hub) RemoveFunc ¶
Remove removes a hook based on a function name and its callback.
Same as Remove(NameOfFunc(fn), callback).
Returns true if the removal succeed.
func (*Hub) RemoveHooks ¶
RemoveHooks removes all registered hooks sharing the same name.
Returns true if the removal succeed.
func (*Hub) Run ¶
Example ¶
///TODO: fill this example with hook funcs. fmt.Println("TODO")
Output: TODO
Example (Second) ¶
hub := NewHub() hub.Register("hook1", func() { time.Sleep(1 * time.Second) fmt.Println("hey from hook1") }) hub.Register("hook2", func() { fmt.Println("hey from hook2") }) go hub.Run("hook1") hub.Run("hook2") // wait for hook1, it's too long, temporary we don't have a way to wait,but it's todo. time.Sleep(2 * time.Second) // hook2 should be printed first. // because the hooks1' time.Sleep runs in goroutine. // // At the future we should develop it in order to be able to set // the hook2 in Async state in order to be executed with other 'async' // hooks inside different goroutines, or inside a group of goroutines, we will see. // Also: // Be able to .Wait for specific callbacks or a group of them or globally.
Output: hey from hook2 hey from hook1
type Source ¶
func GetCurrentHookSource ¶
func GetCurrentHookSource() Source
GetCurrentHookSource returns the current hook's source who calls the current listener, it can be only called inside a hook's callback.
func GetCurrentRunner ¶
func GetCurrentRunner() Source
GetCurrentRunner returns the caller who calls the current listener, it can be only called inside a hook's callback.
func ReadSource ¶
func ReadSourceFunc ¶
func ReadSourceFunc(fn interface{}) Source