modes

package
v1.0.51 Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2022 License: MIT Imports: 5 Imported by: 0

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 新建消息总线.

func (*EventBus[DATA, KEY]) Has added in v1.0.51

func (p *EventBus[DATA, KEY]) Has(key KEY) bool

Has 消息类型是否有监听.

func (*EventBus[DATA, KEY]) Post added in v1.0.49

func (p *EventBus[DATA, KEY]) Post(event *Event[DATA, KEY])

Post 发送消息.

func (*EventBus[DATA, KEY]) Regist added in v1.0.51

func (p *EventBus[DATA, KEY]) Regist(objerver chan<- DATA, keys []KEY)

Regist 注册观察者.

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

func NewSubject[DATA any]() *Subject[DATA]

func (*Subject[DATA]) Notify

func (p *Subject[DATA]) Notify(data DATA)

Notify 通知所有监听者.

func (*Subject[DATA]) Register

func (p *Subject[DATA]) Register(observer chan<- DATA)

Register 监听者注册.

Jump to

Keyboard shortcuts

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