Skip to content

Commit e94d354

Browse files
committed
Sequential create and defer
1 parent 42278d5 commit e94d354

File tree

2 files changed

+10
-28
lines changed

2 files changed

+10
-28
lines changed

iterable_create.go

+4-11
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,11 @@ func newCreateIterable(fs []Producer, opts ...Option) Iterable {
1818
next := option.buildChannel()
1919
ctx := option.buildContext()
2020

21-
wg := sync.WaitGroup{}
22-
for _, f := range fs {
23-
f := f
24-
wg.Add(1)
25-
go func() {
26-
defer wg.Done()
27-
f(ctx, next)
28-
}()
29-
}
3021
go func() {
31-
wg.Wait()
32-
close(next)
22+
defer close(next)
23+
for _, f := range fs {
24+
f(ctx, next)
25+
}
3326
}()
3427

3528
return &createIterable{

iterable_defer.go

+6-17
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
package rxgo
22

3-
import (
4-
"sync"
5-
)
6-
73
type deferIterable struct {
8-
f []Producer
4+
fs []Producer
95
opts []Option
106
}
117

128
func newDeferIterable(f []Producer, opts ...Option) Iterable {
139
return &deferIterable{
14-
f: f,
10+
fs: f,
1511
opts: opts,
1612
}
1713
}
@@ -21,18 +17,11 @@ func (i *deferIterable) Observe(opts ...Option) <-chan Item {
2117
next := option.buildChannel()
2218
ctx := option.buildContext()
2319

24-
wg := sync.WaitGroup{}
25-
for _, f := range i.f {
26-
f := f
27-
wg.Add(1)
28-
go func() {
29-
defer wg.Done()
30-
f(ctx, next)
31-
}()
32-
}
3320
go func() {
34-
wg.Wait()
35-
close(next)
21+
defer close(next)
22+
for _, f := range i.fs {
23+
f(ctx, next)
24+
}
3625
}()
3726

3827
return next

0 commit comments

Comments
 (0)