mirror of
https://github.com/Monibuca/engine.git
synced 2025-10-05 16:46:58 +08:00
feat: add extra event system
This commit is contained in:
@@ -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
|
||||||
@@ -332,7 +339,7 @@ func (config *Config) schema(index int) (r any) {
|
|||||||
case reflect.Map:
|
case reflect.Map:
|
||||||
var children []struct {
|
var children []struct {
|
||||||
Key string `json:"mkey"`
|
Key string `json:"mkey"`
|
||||||
Value any `json:"mvalue"`
|
Value any `json:"mvalue"`
|
||||||
}
|
}
|
||||||
p := Property{
|
p := Property{
|
||||||
Type: "array",
|
Type: "array",
|
||||||
@@ -402,7 +409,7 @@ func (config *Config) schema(index int) (r any) {
|
|||||||
for iter.Next() {
|
for iter.Next() {
|
||||||
children = append(children, struct {
|
children = append(children, struct {
|
||||||
Key string `json:"mkey"`
|
Key string `json:"mkey"`
|
||||||
Value any `json:"mvalue"`
|
Value any `json:"mvalue"`
|
||||||
}{
|
}{
|
||||||
Key: iter.Key().String(),
|
Key: iter.Key().String(),
|
||||||
Value: iter.Value().Interface(),
|
Value: iter.Value().Interface(),
|
||||||
@@ -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
|
||||||
|
@@ -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"`
|
||||||
|
15
events.go
15
events.go
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@@ -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 {
|
||||||
|
Reference in New Issue
Block a user