-
Notifications
You must be signed in to change notification settings - Fork 2
/
series.go
208 lines (169 loc) · 5.06 KB
/
series.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package async
/*
Series is a shorthand function to List.RunSeries without having to manually
create a new list, add the routines, etc.
*/
func Series(routines []Routine, callbacks ...Done) {
l := New()
l.Multiple(routines...)
l.RunSeries(callbacks...)
}
/*
SeriesParallel is a shorthand function to List.RunSeriesParallel without
having to manually create a new list, add the routines, etc.
*/
func SeriesParallel(routines []Routine, callbacks ...Done) {
l := New()
l.Multiple(routines...)
l.RunSeriesParallel(callbacks...)
}
/*
RunSeries will run all of the Routine functions in a series effect.
If there is an error, series will immediately exit and trigger the
callbacks with the error.
There are no arguments passed between the routines that are used in series.
It is just for commands that need to run asynchronously without seeing the
results of its previous routine.
For example, take a look at one of the tests for this function:
func TestSeries(t *testing.T) {
counter := 0
Status("Calling Series")
async.Series([]async.Routine{
func(done async.Done, args ...interface{}) {
Status("Increasing counter...")
counter++
done(nil)
},
func(done async.Done, args ...interface{}) {
Status("Increasing counter...")
counter++
done(nil)
},
func(done async.Done, args ...interface{}) {
Status("Increasing counter...")
counter++
done(nil)
},
func(done async.Done, args ...interface{}) {
Status("Increasing counter...")
counter++
done(nil)
},
}, func(err error, results ...interface{}) {
if err != nil {
t.Errorf("Unexpected error: %s", err)
return
}
if counter != 4 {
t.Errorf("Not all routines were completed.")
return
}
Status("Counter: %d", counter)
})
}
*/
func (l *List) RunSeries(callbacks ...Done) {
fall := fallSeries(l, callbacks...)
next := nextSeries(l, callbacks...)
l.Wait.Add(l.Len())
fall(next)
}
/*
RunSeriesParallel all of the Routine functions in a series effect, and in
parallel mode.
If there is an error, any further results will be discarded but it will not
immediately exit. It will continue to run all of the other Routine functions
that were passed into it. This is because by the time the error is sent, the
goroutines have already been started. At this current time, there is no way
to cancel a sleep timer in Go.
There are no arguments passed between the routines that are used in series.
It is just for commands that need to run asynchronously without seeing the
results of its previous routine.
For example, take a look at one of the tests for this function:
func TestSeriesParallel(t *testing.T) {
counter := 0
Status("Calling Series")
async.SeriesParallel([]async.Routine{
func(done async.Done, args ...interface{}) {
Status("Increasing counter...")
counter++
done(nil)
},
func(done async.Done, args ...interface{}) {
Status("Increasing counter...")
counter++
done(nil)
},
func(done async.Done, args ...interface{}) {
Status("Increasing counter...")
counter++
done(nil)
},
func(done async.Done, args ...interface{}) {
Status("Increasing counter...")
counter++
done(nil)
},
}, func(err error, results ...interface{}) {
if err != nil {
t.Errorf("Unexpected error: %s", err)
return
}
if counter != 4 {
t.Errorf("Not all routines were completed.")
return
}
Status("Counter: %d", counter)
})
}
*/
func (l *List) RunSeriesParallel(callbacks ...Done) {
var routines []Routine
for l.Len() > 0 {
e := l.Front()
_, r := l.Remove(e)
routines = append(routines, func(routine Routine) Routine {
return func(done Done, args ...interface{}) {
r(func(err error, args ...interface{}) {
// As with our normal RunSeries, we do not want to handle any args
// that are returned. We only want to return if an error occurred.
done(err)
})
}
}(r))
}
l.Wait.Add(l.Len())
Parallel(routines, callbacks...)
}
func fallSeries(l *List, callbacks ...Done) func(Done, ...interface{}) {
return func(next Done, args ...interface{}) {
e := l.Front()
_, r := l.Remove(e)
// Run the first series routine and give it the next function, and
// any arguments that were provided
go r(next)
l.Wait.Wait()
}
}
func nextSeries(l *List, callbacks ...Done) Done {
fall := fallSeries(l, callbacks...)
return func(err error, args ...interface{}) {
next := nextSeries(l, callbacks...)
l.Wait.Done()
if err != nil || l.Len() == 0 {
// Just in case it's an error, let's make sure we've cleared
// all of the sync.WaitGroup waits that we initiated.
for i := 0; i < l.Len(); i++ {
l.Wait.Done()
}
// Send the results to the callbacks
for i := 0; i < len(callbacks); i++ {
callbacks[i](err)
}
return
}
// Run the next series routine with any arguments that were provided
fall(next)
return
}
}