-
Notifications
You must be signed in to change notification settings - Fork 7
/
interfaces.go
44 lines (36 loc) · 1.03 KB
/
interfaces.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
package routine
import (
"context"
"github.com/x-mod/errors"
)
var (
//ErrNoneExecutor error
ErrNoneExecutor = errors.New("none executor")
//ErrNoneContext error
ErrNoneContext = errors.New("none context")
//ErrNonePlan error
ErrNonePlan = errors.New("none plan")
)
//Executor interface definition
type Executor interface {
//Execute before stopping make sure all subroutines stopped
Execute(context.Context) error
}
//ExecutorFunc definition
type ExecutorFunc func(context.Context) error
//Execute ExecutorFunc implemention of Executor
func (f ExecutorFunc) Execute(ctx context.Context) error {
return f(ctx)
}
// ExecutorMiddleware is a function that middlewares can implement to be
// able to chain.
type ExecutorMiddleware func(Executor) Executor
// UseExecutorMiddleware wraps a Executor in one or more middleware.
func UseExecutorMiddleware(exec Executor, middleware ...ExecutorMiddleware) Executor {
// Apply in reverse order.
for i := len(middleware) - 1; i >= 0; i-- {
m := middleware[i]
exec = m(exec)
}
return exec
}