event

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2023 License: MIT Imports: 6 Imported by: 2

README

event

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

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

脱胎于原项目 event.go

使用
package event_test

import (
	"fmt"
	"math"
	"testing"

	"github.com/Drelf2018/event"
	"github.com/Drelf2020/utils"
)

func TestAsyncEvent(t *testing.T) {
	a := event.Default()

	a.OnCommand("danmaku114", event.OnlyData(func(data int) {
		fmt.Printf("data: %v(%T)\n", data, data)
	}))

	a.OnRegexp(`danmaku\d`,
		event.WithData(
			func(e *event.Event, data int) {
				if data&1 == 0 {
					e.Store("sin", math.Sin(float64(data)))
				}
			},
		),
		func(e *event.Event) {
			var num float64
			err := e.Get("sin", &num, -1.0)
			utils.PanicErr(err)
			if num == -1.0 {
				println("sin: Didn't store the value of sin(data)")
			} else {
				fmt.Printf("sin: %v(%T)\n", num, num)
			}
		},
	)

	a.OnAll(
		func(e *event.Event) { fmt.Printf("%v\n", e) },
		func(e *event.Event) { e.Abort() },
		func(e *event.Event) { fmt.Println("Why still running!?") },
	)

	event.Heartbeat(1, 3, event.WithData(func(e *event.Event, count int) {
		println()
		a.Dispatch("danmaku114", count)
		if count == 2 {
			e.Abort()
		}
	}))
}
控制台
data: 0(int)
sin: 0(float64)
Event(__ALL__, 0, map[])

data: 1(int)
sin: Didn't store the value of sin(data)
Event(__ALL__, 1, map[])

sin: 0.9092974268256816(float64)
data: 2(int)
Event(__ALL__, 2, map[])
PASS
ok      github.com/Drelf2018/event      7.049s

Documentation

Index

Constants

View Source
const (
	MIN int    = -2147483648
	ALL string = "__ALL__"
)

Variables

This section is empty.

Functions

func Heartbeat

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

func OnlyData

func OnlyData[T any](handle func(T)) func(*Event)

func WithData

func WithData[T any](handles ...func(*Event, T)) func(*Event)

Types

type AsyncEvent

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

func New

func New[K comparable](all K) AsyncEvent[K]

func (*AsyncEvent[K]) Dispatch

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

func (*AsyncEvent[K]) On

func (a *AsyncEvent[K]) On(name string, cmd K, handles ...func(*Event)) func()

func (*AsyncEvent[K]) OnAll

func (a *AsyncEvent[K]) OnAll(handles ...func(*Event)) func()

func (*AsyncEvent[K]) OnCommand

func (a *AsyncEvent[K]) OnCommand(cmd K, handles ...func(*Event)) func()

func (*AsyncEvent[K]) Register

func (a *AsyncEvent[K]) Register(name string, match func(cmd, key K) bool)

type AsyncEventS

type AsyncEventS struct {
	AsyncEvent[string]
}

func Default

func Default() AsyncEventS

func (*AsyncEventS) OnRegexp

func (a *AsyncEventS) OnRegexp(pattern string, handles ...func(*Event)) func()

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) Data

func (e *Event) Data(x any) error

func (*Event) Get

func (e *Event) Get(name string, x any, _default any) error

func (*Event) New

func (e *Event) New()

func (*Event) Reset

func (e *Event) Reset()

func (*Event) Set

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

func (*Event) Store

func (e *Event) Store(name string, value any)

func (Event) String

func (e Event) String() string

Jump to

Keyboard shortcuts

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