Remove unnecessary context wrappers

This commit is contained in:
世界 2023-12-16 15:40:14 +08:00
parent a99c3108a7
commit b0cd7af416
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
6 changed files with 12 additions and 10 deletions

9
box.go
View file

@ -55,7 +55,7 @@ func New(options Options) (*Box, error) {
ctx = context.Background()
}
ctx = service.ContextWithDefaultRegistry(ctx)
ctx = pause.ContextWithDefaultManager(ctx)
ctx = pause.WithDefaultManager(ctx)
experimentalOptions := common.PtrValueOrDefault(options.Experimental)
applyDebugOptions(common.PtrValueOrDefault(experimentalOptions.Debug))
var needCacheFile bool
@ -157,10 +157,13 @@ func New(options Options) (*Box, error) {
preServices2 := make(map[string]adapter.Service)
postServices := make(map[string]adapter.Service)
if needCacheFile {
cacheFile := cachefile.New(ctx, common.PtrValueOrDefault(experimentalOptions.CacheFile))
preServices1["cache file"] = cacheFile
cacheFile := service.FromContext[adapter.CacheFile](ctx)
if cacheFile == nil {
cacheFile = cachefile.New(ctx, common.PtrValueOrDefault(experimentalOptions.CacheFile))
service.MustRegister[adapter.CacheFile](ctx, cacheFile)
}
preServices1["cache file"] = cacheFile
}
if needClashAPI {
clashAPIOptions := common.PtrValueOrDefault(experimentalOptions.ClashAPI)
clashAPIOptions.ModeList = experimental.CalculateClashModeList(options.Options)

View file

@ -44,8 +44,6 @@ func NewService(configContent string, platformInterface PlatformInterface) (*Box
ctx = filemanager.WithDefault(ctx, sWorkingPath, sTempPath, sUserID, sGroupID)
urlTestHistoryStorage := urltest.NewHistoryStorage()
ctx = service.ContextWithPtr(ctx, urlTestHistoryStorage)
pauseManager := pause.NewDefaultManager(ctx)
ctx = pause.ContextWithManager(ctx, pauseManager)
platformWrapper := &platformInterfaceWrapper{iif: platformInterface, useProcFS: platformInterface.UseProcFS()}
instance, err := box.New(box.Options{
Context: ctx,
@ -63,7 +61,7 @@ func NewService(configContent string, platformInterface PlatformInterface) (*Box
cancel: cancel,
instance: instance,
urlTestHistoryStorage: urlTestHistoryStorage,
pauseManager: pauseManager,
pauseManager: service.FromContext[pause.Manager](ctx),
}, nil
}

View file

@ -228,7 +228,7 @@ func NewURLTestGroup(
idleTimeout: idleTimeout,
history: history,
close: make(chan struct{}),
pauseManager: pause.ManagerFromContext(ctx),
pauseManager: service.FromContext[pause.Manager](ctx),
interruptGroup: interrupt.NewGroup(),
interruptExternalConnections: interruptExternalConnections,
}, nil

View file

@ -23,6 +23,7 @@ import (
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
"github.com/sagernet/sing/common/x/list"
"github.com/sagernet/sing/service"
"github.com/sagernet/sing/service/pause"
"github.com/sagernet/wireguard-go/conn"
"github.com/sagernet/wireguard-go/device"
@ -61,7 +62,7 @@ func NewWireGuard(ctx context.Context, router adapter.Router, logger log.Context
},
ctx: ctx,
workers: options.Workers,
pauseManager: pause.ManagerFromContext(ctx),
pauseManager: service.FromContext[pause.Manager](ctx),
}
peers, err := wireguard.ParsePeers(options)
if err != nil {

View file

@ -125,7 +125,7 @@ func NewRouter(
autoDetectInterface: options.AutoDetectInterface,
defaultInterface: options.DefaultInterface,
defaultMark: options.DefaultMark,
pauseManager: pause.ManagerFromContext(ctx),
pauseManager: service.FromContext[pause.Manager](ctx),
platformInterface: platformInterface,
needWIFIState: hasRule(options.Rules, isWIFIRule) || hasDNSRule(dnsOptions.Rules, isWIFIDNSRule),
needPackageManager: C.IsAndroid && platformInterface == nil && common.Any(inbounds, func(inbound option.Inbound) bool {

View file

@ -55,7 +55,7 @@ func NewRemoteRuleSet(ctx context.Context, router adapter.Router, logger logger.
logger: logger,
options: options,
updateInterval: updateInterval,
pauseManager: pause.ManagerFromContext(ctx),
pauseManager: service.FromContext[pause.Manager](ctx),
}
}