event

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2024 License: MIT Imports: 6 Imported by: 1

README

event

异步事件分发类,可组合后使用。

参数 handles 为不定长,事件触发时按照链式顺序逐一调用,可使用 e.Abort() 中断。

脱胎于原项目 event.go

使用
package event_test

import (
	"errors"
	"strconv"
	"testing"
	"time"

	"github.com/Drelf2018/event"
)

func TestAsyncEvent(t *testing.T) {
	evt := event.AsyncEventS[int]{}

	evt.OnEqual("1", func(e *event.Event, v int) {
		t.Logf("OnEqual:  %v\n", v)
		e.Abort()
	}, func(*event.Event, int) {
		panic(errors.New("this error will not panic"))
	})

	evt.OnRegexp(`\d+`, func(e *event.Event, v int) {
		t.Logf("OnRegexp: %v\n", v)
	})

	ch := evt.Channel(strconv.Itoa)
	defer close(ch)

	event.Heartbeat(0, 1000, func(b *event.Beat) {
		t.Logf("Heartbeat#%v\n", b.Count)
		ch <- b.Count
		if b.Count == 2 {
			time.Sleep(500 * time.Millisecond)
			b.Abort()
		}
	})
}
控制台
=== RUN   TestAsyncEvent
    async_test.go:30: Heartbeat#1
    async_test.go:16: OnEqual:  1
    async_test.go:23: OnRegexp: 1
    async_test.go:30: Heartbeat#2
    async_test.go:23: OnRegexp: 2
--- PASS: TestAsyncEvent (1.50s)
PASS
ok      github.com/Drelf2018/event      1.532s

Documentation

Index

Constants

View Source
const (
	EQUAL  string = "equal"
	REGEXP string = "regexp"
)

Variables

This section is empty.

Functions

func Dispatch added in v0.4.0

func Dispatch[K comparable, V any](chains []Chain[V], cmd K, data V)

func Heartbeat

func Heartbeat(initdead, keepalive float64, f func(*Beat))

单位是毫秒

Types

type AsyncEvent

type AsyncEvent[K comparable, V any] struct {
	// contains filtered or unexported fields
}

func New

func New[K comparable, V any]() AsyncEvent[K, V]

func (*AsyncEvent[K, V]) Channel added in v0.4.0

func (a *AsyncEvent[K, V]) Channel(key func(V) K) chan<- V

func (*AsyncEvent[K, V]) Del added in v0.4.0

func (a *AsyncEvent[K, V]) Del(name string, cmd K, index int) error

func (*AsyncEvent[K, V]) Dispatch

func (a *AsyncEvent[K, V]) Dispatch(cmd K, data V)

func (*AsyncEvent[K, V]) On

func (a *AsyncEvent[K, V]) On(name string, cmd K, handles ...Handle[V]) (int, error)

func (*AsyncEvent[K, V]) OnEqual added in v0.3.0

func (a *AsyncEvent[K, V]) OnEqual(cmd K, handles ...Handle[V]) (int, error)

func (*AsyncEvent[K, V]) Register

func (a *AsyncEvent[K, V]) Register(name string, rule Rule[K, V]) *AsyncEvent[K, V]

func (*AsyncEvent[K, V]) Rule added in v0.2.0

func (a *AsyncEvent[K, V]) Rule(name string) (r Rule[K, V], err error)

type AsyncEventS

type AsyncEventS[V any] struct {
	AsyncEvent[string, V]
	// contains filtered or unexported fields
}

func Default

func Default[V any]() AsyncEventS[V]

func (*AsyncEventS[V]) OnRegexp

func (a *AsyncEventS[V]) OnRegexp(pattern string, handles ...Handle[V]) (int, error)

type Beat added in v0.4.0

type Beat struct {
	Event
	Count int
	// contains filtered or unexported fields
}

func (*Beat) Abort added in v0.4.0

func (b *Beat) Abort()

type Chain added in v0.4.0

type Chain[V any] []Handle[V]

type Equal added in v0.3.0

type Equal[K comparable, V any] map[K][]Chain[V]

func (Equal[K, V]) Add added in v0.3.0

func (e Equal[K, V]) Add(cmd K, chain Chain[V]) (int, error)

func (Equal[K, V]) Del added in v0.3.0

func (e Equal[K, V]) Del(cmd K, index int) error

func (Equal[K, V]) Dispatch added in v0.4.0

func (e Equal[K, V]) Dispatch(cmd K, data V)

type Event

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

func (*Event) Abort

func (e *Event) Abort()

func (*Event) Cmd

func (e *Event) Cmd() string

func (*Event) Get

func (e *Event) Get(key string, default_ ...any) any

func (*Event) OnGet added in v0.3.1

func (e *Event) OnGet(x ...any)

func (*Event) OnNew added in v0.3.1

func (e *Event) OnNew()

func (*Event) OnPut added in v0.3.1

func (e *Event) OnPut()

func (*Event) Set

func (e *Event) Set(key string, value any)

func (Event) String

func (e Event) String() string

type Handle added in v0.4.0

type Handle[V any] func(*Event, V)

type Regexp added in v0.2.0

type Regexp[V any] Equal[string, V]

func (Regexp[V]) Add added in v0.4.0

func (r Regexp[V]) Add(cmd string, chain Chain[V]) (int, error)

func (Regexp[V]) Del added in v0.4.0

func (r Regexp[V]) Del(cmd string, index int) error

func (Regexp[V]) Dispatch added in v0.4.0

func (r Regexp[V]) Dispatch(cmd string, data V)

func (Regexp[V]) Match added in v0.2.0

func (r Regexp[V]) Match(cmd, key string) bool

type Rule added in v0.3.0

type Rule[K comparable, V any] interface {
	Add(cmd K, chain Chain[V]) (int, error)
	Del(cmd K, index int) error
	Dispatch(cmd K, data V)
}

Jump to

Keyboard shortcuts

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