feat: add extra event system

This commit is contained in:
langhuihui
2023-12-05 19:12:03 +08:00
parent 9e2ca2a670
commit 48d6e277e4
4 changed files with 36 additions and 7 deletions

View File

@@ -26,7 +26,7 @@ type Config struct {
Default any //默认值 Default any //默认值
Enum []struct { Enum []struct {
Label string `json:"label"` Label string `json:"label"`
Value string `json:"value"` Value any `json:"value"`
} }
name string // 小写 name string // 小写
propsMap map[string]*Config propsMap map[string]*Config
@@ -144,12 +144,16 @@ func (config *Config) Parse(s any, prefix ...string) {
if len(kvs) != 2 { if len(kvs) != 2 {
continue continue
} }
var tmp struct {
Value any
}
yaml.Unmarshal([]byte(fmt.Sprintf("value: %s", strings.TrimSpace(kvs[0]))), &tmp)
prop.Enum = append(prop.Enum, struct { prop.Enum = append(prop.Enum, struct {
Label string `json:"label"` Label string `json:"label"`
Value string `json:"value"` Value any `json:"value"`
}{ }{
Label: strings.TrimSpace(kvs[1]), Label: strings.TrimSpace(kvs[1]),
Value: strings.TrimSpace(kvs[0]), Value: tmp.Value,
}) })
} }
} }
@@ -272,6 +276,9 @@ func (config *Config) schema(index int) (r any) {
"title": config.name, "title": config.name,
} }
for i, v := range config.props { for i, v := range config.props {
if strings.HasPrefix(v.tag.Get("desc"), "废弃") {
continue
}
r.Properties[v.name] = v.schema(i) r.Properties[v.name] = v.schema(i)
} }
return r return r
@@ -427,6 +434,9 @@ func (config *Config) GetFormily() (r Formily) {
Properties: make(map[string]any), Properties: make(map[string]any),
} }
for i, v := range config.props { for i, v := range config.props {
if strings.HasPrefix(v.tag.Get("desc"), "废弃") {
continue
}
r.Schema.Properties[v.name] = v.schema(i) r.Schema.Properties[v.name] = v.schema(i)
} }
return return

View File

@@ -6,7 +6,7 @@ type Property struct {
Description string `json:"description"` Description string `json:"description"`
Enum []struct { Enum []struct {
Label string `json:"label"` Label string `json:"label"`
Value string `json:"value"` Value any `json:"value"`
} `json:"enum,omitempty"` } `json:"enum,omitempty"`
Items *Object `json:"items,omitempty"` Items *Object `json:"items,omitempty"`
Properties map[string]any `json:"properties,omitempty"` Properties map[string]any `json:"properties,omitempty"`

View File

@@ -1,6 +1,7 @@
package engine package engine
import ( import (
"reflect"
"time" "time"
"m7s.live/engine/v4/common" "m7s.live/engine/v4/common"
@@ -104,3 +105,17 @@ type InviteTrackEvent struct {
func InviteTrack(name string, suber ISubscriber) { func InviteTrack(name string, suber ISubscriber) {
EventBus <- InviteTrackEvent{Event: CreateEvent(name), ISubscriber: suber} EventBus <- InviteTrackEvent{Event: CreateEvent(name), ISubscriber: suber}
} }
var handlers = make(map[reflect.Type][]any)
func ListenEvent[T any](handler func(event T)) {
t := reflect.TypeOf(handler).In(0)
handlers[t] = append(handlers[t], handler)
}
func EmitEvent[T any](event T) {
t := reflect.TypeOf(event)
for _, handler := range handlers[t] {
handler.(func(event T))(event)
}
}

View File

@@ -34,10 +34,14 @@ func (pub *Pusher) startPush(pusher IPusher) {
Pushers.Store(pub.RemoteURL, pusher) Pushers.Store(pub.RemoteURL, pusher)
defer Pushers.Delete(pub.RemoteURL) defer Pushers.Delete(pub.RemoteURL)
defer pusher.Disconnect() defer pusher.Disconnect()
var startTime time.Time
for pusher.Info("start push"); pusher.Reconnect(); pusher.Warn("restart push") { for pusher.Info("start push"); pusher.Reconnect(); pusher.Warn("restart push") {
if time.Since(startTime) < 5*time.Second {
time.Sleep(5 * time.Second)
}
startTime = time.Now()
if err = pusher.Subscribe(pub.StreamPath, pusher); err != nil { if err = pusher.Subscribe(pub.StreamPath, pusher); err != nil {
pusher.Error("push subscribe", zap.Error(err)) pusher.Error("push subscribe", zap.Error(err))
time.Sleep(time.Second * 5)
} else { } else {
stream := pusher.GetSubscriber().Stream stream := pusher.GetSubscriber().Stream
if err = pusher.Connect(); err != nil { if err = pusher.Connect(); err != nil {