-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
72 lines (65 loc) · 1.32 KB
/
context.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package rxp
import (
"context"
"errors"
)
type contextKey struct{}
// With
// 把 Executors 关联到 context.Context
func With(ctx context.Context, exec Executors) context.Context {
return context.WithValue(ctx, contextKey{}, exec)
}
// From
// 从 context.Context 获取 Executors
//
// 注意,必须先 With ,否则会 panic 。
func From(ctx context.Context) Executors {
execs, ok := TryFrom(ctx)
if !ok {
panic("rxp: there is no executors in context")
return nil
}
return execs
}
// TryFrom
// 尝试从 context.Context 获取 Executors
func TryFrom(ctx context.Context) (Executors, bool) {
value := ctx.Value(contextKey{})
if value == nil {
return nil, false
}
exec, ok := value.(Executors)
if ok && exec != nil {
return exec, true
}
return nil, false
}
// TryExecute
// 尝试执行一个任务
//
// 注意,必须先 With 。
func TryExecute(ctx context.Context, task Task) bool {
if task == nil {
return false
}
exec, ok := TryFrom(ctx)
if !ok {
return false
}
return exec.TryExecute(ctx, task)
}
// Execute
// 执行一个任务
//
// 注意,必须先 With 。
func Execute(ctx context.Context, task Task) (err error) {
if task == nil {
return
}
exec, ok := TryFrom(ctx)
if !ok {
return errors.New("rxp: there is no executors in context")
}
err = exec.Execute(ctx, task)
return
}