forked from gookit/goutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
group.go
57 lines (46 loc) · 1.03 KB
/
group.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package goutil
import (
"context"
"github.com/gookit/goutil/structs"
"github.com/gookit/goutil/syncs"
)
// ErrGroup is a collection of goroutines working on subtasks that
// are part of the same overall task.
type ErrGroup = syncs.ErrGroup
// NewCtxErrGroup instance
func NewCtxErrGroup(ctx context.Context, limit ...int) (*ErrGroup, context.Context) {
return syncs.NewCtxErrGroup(ctx, limit...)
}
// NewErrGroup instance
func NewErrGroup(limit ...int) *ErrGroup {
return syncs.NewErrGroup(limit...)
}
// RunFn func
type RunFn func(ctx *structs.Data) error
// QuickRun struct
type QuickRun struct {
ctx *structs.Data
// err error
fns []RunFn
}
// NewQuickRun instance
func NewQuickRun() *QuickRun {
return &QuickRun{
ctx: structs.NewData(),
}
}
// Add func for run
func (p *QuickRun) Add(fns ...RunFn) *QuickRun {
p.fns = append(p.fns, fns...)
return p
}
// Run all func
func (p *QuickRun) Run() error {
for i, fn := range p.fns {
p.ctx.Set("index", i)
if err := fn(p.ctx); err != nil {
return err
}
}
return nil
}