mirror of
https://github.com/v2fly/v2ray-core.git
synced 2025-12-26 03:55:26 -05:00
errors.Combine
This commit is contained in:
@@ -2,26 +2,12 @@ package task
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
"v2ray.com/core/common"
|
||||
"v2ray.com/core/common/signal/semaphore"
|
||||
)
|
||||
|
||||
type Task func() error
|
||||
|
||||
type MultiError []error
|
||||
|
||||
func (e MultiError) Error() string {
|
||||
var r strings.Builder
|
||||
common.Must2(r.WriteString("multierr: "))
|
||||
for _, err := range e {
|
||||
common.Must2(r.WriteString(err.Error()))
|
||||
common.Must2(r.WriteString(" | "))
|
||||
}
|
||||
return r.String()
|
||||
}
|
||||
|
||||
type executionContext struct {
|
||||
ctx context.Context
|
||||
tasks []Task
|
||||
@@ -90,30 +76,6 @@ func Sequential(tasks ...Task) ExecutionOption {
|
||||
}
|
||||
}
|
||||
|
||||
func SequentialAll(tasks ...Task) ExecutionOption {
|
||||
return func(c *executionContext) {
|
||||
switch len(tasks) {
|
||||
case 0:
|
||||
return
|
||||
case 1:
|
||||
c.tasks = append(c.tasks, tasks[0])
|
||||
default:
|
||||
c.tasks = append(c.tasks, func() error {
|
||||
var merr MultiError
|
||||
for _, task := range tasks {
|
||||
if err := task(); err != nil {
|
||||
merr = append(merr, err)
|
||||
}
|
||||
}
|
||||
if len(merr) == 0 {
|
||||
return nil
|
||||
}
|
||||
return merr
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func OnSuccess(task Task) ExecutionOption {
|
||||
return func(c *executionContext) {
|
||||
c.onSuccess = task
|
||||
|
||||
Reference in New Issue
Block a user