Cancle and signal errors shouldnt be voided
This commit is contained in:
+5
-7
@@ -13,7 +13,7 @@ type Loop struct {
|
|||||||
HandlerFunctions map[reflect.Type][]reflect.Value
|
HandlerFunctions map[reflect.Type][]reflect.Value
|
||||||
|
|
||||||
Ctx context.Context
|
Ctx context.Context
|
||||||
cancel context.CancelFunc
|
Cancel context.CancelFunc
|
||||||
eventSources []reflect.SelectCase
|
eventSources []reflect.SelectCase
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -69,7 +69,6 @@ var DontForwardErr = errors.New("dont forward")
|
|||||||
var WhileClosingErr = errors.New("error while closing")
|
var WhileClosingErr = errors.New("error while closing")
|
||||||
|
|
||||||
// CloseNonCriticalErr will close even when the event is non-critical
|
// CloseNonCriticalErr will close even when the event is non-critical
|
||||||
var CloseNonCriticalErr = errors.New("close non-critical")
|
|
||||||
var ContextDoneErr = errors.New("context done")
|
var ContextDoneErr = errors.New("context done")
|
||||||
var RecvNotOkErr = errors.New("recv not ok")
|
var RecvNotOkErr = errors.New("recv not ok")
|
||||||
|
|
||||||
@@ -126,7 +125,7 @@ func (l *Loop) RegisterCustomType(eventType reflect.Type, hv reflect.Value) {
|
|||||||
v := hv.Call([]reflect.Value{reflect.ValueOf(packet)})[0]
|
v := hv.Call([]reflect.Value{reflect.ValueOf(packet)})[0]
|
||||||
if !v.IsNil() {
|
if !v.IsNil() {
|
||||||
err := v.Interface().(error)
|
err := v.Interface().(error)
|
||||||
if errors.Is(err, CloseNonCriticalErr) {
|
if errors.Is(err, HandlerDoneErr{}) || errors.Is(err, DontForwardErr) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
slog.Error("handler failed", "err", err)
|
slog.Error("handler failed", "err", err)
|
||||||
@@ -204,10 +203,8 @@ func (l *Loop) Fire(event any) (err error) {
|
|||||||
func (l *Loop) ContextDoneListener() (chSending <-chan any) {
|
func (l *Loop) ContextDoneListener() (chSending <-chan any) {
|
||||||
ch := make(chan any)
|
ch := make(chan any)
|
||||||
go func() {
|
go func() {
|
||||||
select {
|
<-l.Ctx.Done()
|
||||||
case <-l.Ctx.Done():
|
|
||||||
ch <- ContextDoneErr
|
ch <- ContextDoneErr
|
||||||
}
|
|
||||||
}()
|
}()
|
||||||
return ch
|
return ch
|
||||||
}
|
}
|
||||||
@@ -238,12 +235,13 @@ func (l *Loop) StartLoop() (err error) {
|
|||||||
if closeErr != nil {
|
if closeErr != nil {
|
||||||
err = errors.Join(err, errors.Join(WhileClosingErr, closeErr))
|
err = errors.Join(err, errors.Join(WhileClosingErr, closeErr))
|
||||||
}
|
}
|
||||||
|
l.Cancel()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewLoop() (l *Loop) {
|
func NewLoop() (l *Loop) {
|
||||||
l = &Loop{}
|
l = &Loop{}
|
||||||
l.Ctx, l.cancel = context.WithCancel(context.Background())
|
l.Ctx, l.Cancel = context.WithCancel(context.Background())
|
||||||
l.HandlerFunctions = make(map[reflect.Type][]reflect.Value)
|
l.HandlerFunctions = make(map[reflect.Type][]reflect.Value)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user