Remove unnecessary context wrappers

This commit is contained in:
世界 2023-12-16 15:27:27 +08:00
parent cdb9908442
commit c9319a35ee
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
2 changed files with 11 additions and 6 deletions

View file

@ -6,17 +6,17 @@ import (
"github.com/sagernet/sing/service"
)
// Deprecated: use service.ContextWith instead.
func ManagerFromContext(ctx context.Context) Manager {
return service.FromContext[Manager](ctx)
}
// Deprecated: use service.ContextWith instead.
func ContextWithManager(ctx context.Context, manager Manager) context.Context {
return service.ContextWith[Manager](ctx, manager)
}
// Deprecated: use WithDefaultManager instead.
func ContextWithDefaultManager(ctx context.Context) context.Context {
if service.FromContext[Manager](ctx) != nil {
return ctx
}
return service.ContextWith[Manager](ctx, NewDefaultManager(ctx))
return WithDefaultManager(ctx)
}

View file

@ -6,6 +6,7 @@ import (
"github.com/sagernet/sing/common/atomic"
"github.com/sagernet/sing/common/x/list"
"github.com/sagernet/sing/service"
)
type defaultManager struct {
@ -18,16 +19,20 @@ type defaultManager struct {
callbacks list.List[Callback]
}
func NewDefaultManager(ctx context.Context) Manager {
func WithDefaultManager(ctx context.Context) context.Context {
if service.FromContext[Manager](ctx) != nil {
return ctx
}
devicePauseChan := make(chan struct{})
networkPauseChan := make(chan struct{})
close(devicePauseChan)
close(networkPauseChan)
return &defaultManager{
manager := &defaultManager{
ctx: ctx,
devicePause: devicePauseChan,
networkPause: networkPauseChan,
}
return service.ContextWith[Manager](ctx, manager)
}
func (d *defaultManager) DevicePause() {