Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type DelayEventBus ¶ added in v1.0.49
type DelayEventBus[DATA any, KEY comparable] struct { EventBus[DATA, KEY] // contains filtered or unexported fields }
DelayEventBus 延迟消息总线.
Example ¶
package main import ( "fmt" "sort" "time" "github.com/xuender/oils/modes" ) func main() { bus := modes.NewDelayEventBus[int, string](time.Millisecond) ob1 := make(chan int) ob2 := make(chan int) out := []string{} go func() { for num := range ob1 { out = append(out, fmt.Sprintf("%d ob1", num)) } }() go func() { for num := range ob2 { out = append(out, fmt.Sprintf("%d ob2", num)) } }() bus.Regist(ob1, []string{"click", "key"}) bus.Regist(ob2, []string{"touch", "click"}) bus.Post(modes.NewEvent("startup", 0)) bus.Post(modes.NewEvent("touch", 0)) bus.Post(modes.NewEvent("touch", 3)) bus.Post(modes.NewEvent("touch", 1)) bus.Post(modes.NewEvent("click", 1)) bus.Post(modes.NewEvent("click", 2)) time.Sleep(time.Millisecond) bus.Post(modes.NewEvent("key", 3)) time.Sleep(time.Millisecond * 2) close(ob1) bus.Post(modes.NewEvent("click", 4)) time.Sleep(time.Millisecond * 2) sort.Strings(out) for _, str := range out { fmt.Println(str) } }
Output: 1 ob2 2 ob1 2 ob2 3 ob1 4 ob2
func NewDelayEventBus ¶ added in v1.0.49
func NewDelayEventBus[DATA any, KEY comparable](interval time.Duration) *DelayEventBus[DATA, KEY]
NewDelayEventBus 新建延迟消息总线.
func NewDelayEventBusBySize ¶ added in v1.0.49
func NewDelayEventBusBySize[DATA any, KEY comparable](interval time.Duration, size int) *DelayEventBus[DATA, KEY]
NewDelayEventBusBySize 根据缓存尺寸新建延迟消息总线.
func (*DelayEventBus[DATA, KEY]) Post ¶ added in v1.0.49
func (p *DelayEventBus[DATA, KEY]) Post(event *Event[DATA, KEY])
Post 发送消息.
type Event ¶ added in v1.0.49
type Event[DATA any, KEY comparable] struct { Key KEY Data DATA }
func NewEvent ¶ added in v1.0.49
func NewEvent[DATA any, KEY comparable](key KEY, data DATA) *Event[DATA, KEY]
type EventBus ¶ added in v1.0.49
type EventBus[DATA any, KEY comparable] struct { // contains filtered or unexported fields }
EventBus 消息总线.
Example ¶
package main import ( "fmt" "sort" "time" "github.com/xuender/oils/modes" ) func main() { bus := modes.NewEventBus[int, string]() ob1 := make(chan int) ob2 := make(chan int) out := []string{} go func() { for num := range ob1 { out = append(out, fmt.Sprintf("%d ob1", num)) } }() go func() { for num := range ob2 { out = append(out, fmt.Sprintf("%d ob2", num)) } }() fmt.Println(bus.Has("key")) bus.Regist(ob1, []string{"click", "key"}) fmt.Println(bus.Has("key")) bus.Regist(ob2, []string{"touch", "click"}) bus.Post(modes.NewEvent("startup", 0)) bus.Post(modes.NewEvent("touch", 1)) bus.Post(modes.NewEvent("click", 2)) time.Sleep(time.Millisecond) bus.Post(modes.NewEvent("key", 3)) close(ob1) time.Sleep(time.Millisecond) bus.Post(modes.NewEvent("key", 4)) bus.Post(modes.NewEvent("click", 4)) time.Sleep(time.Millisecond) fmt.Println(bus.Has("key")) sort.Strings(out) for _, str := range out { fmt.Println(str) } }
Output: false true false 1 ob2 2 ob1 2 ob2 3 ob1 4 ob2
func NewEventBus ¶ added in v1.0.49
func NewEventBus[DATA any, KEY comparable]() *EventBus[DATA, KEY]
NewEventBus 新建消息总线.
type Poster ¶ added in v1.0.51
type Poster[DATA any, KEY comparable] interface { Post(*Event[DATA, KEY]) }
type Register ¶ added in v1.0.51
type Register[DATA any, KEY comparable] interface { Regist(chan<- DATA, []KEY) }
type Subject ¶
type Subject[DATA any] struct { // contains filtered or unexported fields }
Subject 主题.
Example ¶
package main import ( "fmt" "time" "github.com/xuender/oils/modes" ) func main() { sub := modes.NewSubject[string]() update := make(chan string) go func() { for str := range update { fmt.Println(str) } }() sub.Register(update) sub.Notify("test1") sub.Notify("test2") time.Sleep(time.Millisecond) close(update) sub.Notify("test3") }
Output: test1 test2
func NewSubject ¶ added in v1.0.49
Click to show internal directories.
Click to hide internal directories.