event

package module
Version: v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2017 License: MIT Imports: 3 Imported by: 2

README

Event

Build Status codecov Go Report Card GoDoc

This is package implements pattern-observer

Fast example

import (
	"github.com/agoalofalife/event"
)

func main() {
	// create struct
	e := event.New()

	// subscriber 
	e.Add("push.email", func(text string){
    	// some logic 
    }, "text")
    
    // init event
    e.Fire("push.email") // or e.Go("push.email")
}

let us consider an example:

  • You must first create the structure
  • Next, the first argument declares the name of the event (string type), second argument executes when the event occurs, the third argument is passed a list of arguments, which are substituted in the parameters of the second argument.
  • In the end you need to run the event. There are two methods available "Go" and his alias "Fire"

The subscriber function method

type Email struct {
	count int
}
func (e *Email) Push() {
	e.count += 1
	fmt.Printf("Push email again, count %d \n", e.count)
}
func main() {
	e := event.New()
	
	email := new(Email)
	
	e.Add(email, email.Push)
	e.Fire(email)
	e.Fire(email)
}
// output 
// Push email again, count 1 
// Push email again, count 2 
  • In this example we sign the event method structure

Read more information in examples 👍

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetName

func GetName(structure interface{}) string

GetName sugar syntax

Types

type Dispatcher

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

Dispatcher Base struct store listeners list listeners display map events :

    event [name-events] --
			     -- [number-iterate]
			         -- type => [type-structure]
				 -- structure => [structure]
				 -- arguments => ...arguments slice : interface{}

func New added in v0.2.0

func New() *Dispatcher

New empty create Dispatcher

func (*Dispatcher) Add

func (dispatcher *Dispatcher) Add(name interface{}, performing interface{}, parameters ...interface{}) (flag bool, err error)

Add new listeners

func (*Dispatcher) Destroy

func (dispatcher *Dispatcher) Destroy(event string)

Destroy or untie event

func (*Dispatcher) Fire

func (dispatcher *Dispatcher) Fire(event interface{}, parameters ...interface{}) (err error)

Fire alias Go method

func (*Dispatcher) Go

func (dispatcher *Dispatcher) Go(event interface{}, parameters ...interface{}) (err error)

Go alias method Fire; parameters override

func (*Dispatcher) Untie

func (dispatcher *Dispatcher) Untie(pointer interface{})

Untie Listeners events

Source Files

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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