Skip to content

lockp111/go-eventbus

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-eventbus

An golang event bus model

Usage

go get

go get -u github.com/lockp111/go-eventbus

New()

Create a new bus struct reference

bus := eventbus.New[string]()

On(topic string, e ...Event)

Subscribe event

type ready struct{
}

func (e ready) Dispatch(_ ...string){
    fmt.Println("I am ready!")
}

bus.On("ready", &ready{})

You can also subscribe multiple events for example:

type run struct{
}

func (e run) Dispatch(_ ...string){
    fmt.Println("I am run!")
}

bus.On("ready", &ready{}, &ready{}).On("run", &run{})

Off(topic string, e ...Event)

Unsubscribe event

e := &ready{}
bus.On("ready", e)
bus.Off("ready", e)

You can also unsubscribe multiple events for example:

e1 := &ready{}
e2 := &ready{}
bus.On("ready", e1, e2)
bus.Off("ready", e1, e2)

You can unsubscribe all events for example:

bus.On("ready", &ready{}, &ready{})
bus.Off("ready")

You can unsubscribe all topics for example:

bus.On("ready", &ready{}, &ready{})
bus.On("run", &run{})
bus.Clean()

Trigger(topic string, msg ...any)

Dispatch events

bus.Trigger("ready")

You can also dispatch multiple events for example:

bus.Trigger("ready", "1", "2")

You can also dispatch all events for example:

bus.Trigger(ALL, "1")