mirror of
https://github.com/Monibuca/engine.git
synced 2025-10-06 00:56:58 +08:00
feat: add extra event system
This commit is contained in:
@@ -26,7 +26,7 @@ type Config struct {
|
||||
Default any //默认值
|
||||
Enum []struct {
|
||||
Label string `json:"label"`
|
||||
Value string `json:"value"`
|
||||
Value any `json:"value"`
|
||||
}
|
||||
name string // 小写
|
||||
propsMap map[string]*Config
|
||||
@@ -144,12 +144,16 @@ func (config *Config) Parse(s any, prefix ...string) {
|
||||
if len(kvs) != 2 {
|
||||
continue
|
||||
}
|
||||
var tmp struct {
|
||||
Value any
|
||||
}
|
||||
yaml.Unmarshal([]byte(fmt.Sprintf("value: %s", strings.TrimSpace(kvs[0]))), &tmp)
|
||||
prop.Enum = append(prop.Enum, struct {
|
||||
Label string `json:"label"`
|
||||
Value string `json:"value"`
|
||||
Value any `json:"value"`
|
||||
}{
|
||||
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,
|
||||
}
|
||||
for i, v := range config.props {
|
||||
if strings.HasPrefix(v.tag.Get("desc"), "废弃") {
|
||||
continue
|
||||
}
|
||||
r.Properties[v.name] = v.schema(i)
|
||||
}
|
||||
return r
|
||||
@@ -332,7 +339,7 @@ func (config *Config) schema(index int) (r any) {
|
||||
case reflect.Map:
|
||||
var children []struct {
|
||||
Key string `json:"mkey"`
|
||||
Value any `json:"mvalue"`
|
||||
Value any `json:"mvalue"`
|
||||
}
|
||||
p := Property{
|
||||
Type: "array",
|
||||
@@ -402,7 +409,7 @@ func (config *Config) schema(index int) (r any) {
|
||||
for iter.Next() {
|
||||
children = append(children, struct {
|
||||
Key string `json:"mkey"`
|
||||
Value any `json:"mvalue"`
|
||||
Value any `json:"mvalue"`
|
||||
}{
|
||||
Key: iter.Key().String(),
|
||||
Value: iter.Value().Interface(),
|
||||
@@ -427,6 +434,9 @@ func (config *Config) GetFormily() (r Formily) {
|
||||
Properties: make(map[string]any),
|
||||
}
|
||||
for i, v := range config.props {
|
||||
if strings.HasPrefix(v.tag.Get("desc"), "废弃") {
|
||||
continue
|
||||
}
|
||||
r.Schema.Properties[v.name] = v.schema(i)
|
||||
}
|
||||
return
|
||||
|
@@ -6,7 +6,7 @@ type Property struct {
|
||||
Description string `json:"description"`
|
||||
Enum []struct {
|
||||
Label string `json:"label"`
|
||||
Value string `json:"value"`
|
||||
Value any `json:"value"`
|
||||
} `json:"enum,omitempty"`
|
||||
Items *Object `json:"items,omitempty"`
|
||||
Properties map[string]any `json:"properties,omitempty"`
|
||||
|
15
events.go
15
events.go
@@ -1,6 +1,7 @@
|
||||
package engine
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"time"
|
||||
|
||||
"m7s.live/engine/v4/common"
|
||||
@@ -104,3 +105,17 @@ type InviteTrackEvent struct {
|
||||
func InviteTrack(name string, suber ISubscriber) {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
@@ -34,10 +34,14 @@ func (pub *Pusher) startPush(pusher IPusher) {
|
||||
Pushers.Store(pub.RemoteURL, pusher)
|
||||
defer Pushers.Delete(pub.RemoteURL)
|
||||
defer pusher.Disconnect()
|
||||
var startTime time.Time
|
||||
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 {
|
||||
pusher.Error("push subscribe", zap.Error(err))
|
||||
time.Sleep(time.Second * 5)
|
||||
} else {
|
||||
stream := pusher.GetSubscriber().Stream
|
||||
if err = pusher.Connect(); err != nil {
|
||||
|
Reference in New Issue
Block a user