-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontainer.go
90 lines (75 loc) · 1.69 KB
/
container.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package simpledi
// Option
// inorder to configure container you can use provided options
type Option func(c *container) error
type container struct {
// providers
// list of providers who are introduced into container
providers []provider
// invokers
// list of invokes who container have to peforms
invoker *invoker
// collection
// list of provided items who we have inside container and we are going to use them
// to satisfy goven funcitons
collection []input
}
// New
// atlease you have to provide one option for container
func New(ops ...Option) (*container, error) {
c := container{
providers: nil,
invoker: nil,
collection: make([]input, 0),
}
for _, op := range ops {
err := op(&c)
if err != nil {
return nil, err
}
}
if c.invoker == nil {
return nil, DiNeedInvoke
}
return &c, nil
}
func (c *container) Run() error {
retryCout := 0
runnedProviders := 0
retry:
for {
anyOneRunned := false
for i := 0; i < len(c.providers); i++ {
if c.providers[i].isCalled {
continue
}
if c.providers[i].readytogo(c.collection) {
outputs := c.providers[i].call(c.collection)
if len(outputs) > 0 {
for _, o := range outputs { // outputs are gone used as input for others
i := input{
name: o.name,
typ: o.typ,
value: o.value,
}
c.collection = append(c.collection, i)
}
}
anyOneRunned = true
runnedProviders++
}
}
if anyOneRunned && runnedProviders == len(c.providers) {
break retry
}
if retryCout > 1 {
return DiCycleDetected
}
retryCout++
}
// run invokes
if !c.invoker.readytogo(c.collection) {
return DiInvokeNotSatisfied
}
return c.invoker.call(c.collection)
}