Skip to content

Commit

Permalink
Sequential create and defer
Browse files Browse the repository at this point in the history
  • Loading branch information
teivah committed Feb 26, 2020
1 parent 42278d5 commit e94d354
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 28 deletions.
15 changes: 4 additions & 11 deletions iterable_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,11 @@ func newCreateIterable(fs []Producer, opts ...Option) Iterable {
next := option.buildChannel()
ctx := option.buildContext()

wg := sync.WaitGroup{}
for _, f := range fs {
f := f
wg.Add(1)
go func() {
defer wg.Done()
f(ctx, next)
}()
}
go func() {
wg.Wait()
close(next)
defer close(next)
for _, f := range fs {
f(ctx, next)
}
}()

return &createIterable{
Expand Down
23 changes: 6 additions & 17 deletions iterable_defer.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
package rxgo

import (
"sync"
)

type deferIterable struct {
f []Producer
fs []Producer
opts []Option
}

func newDeferIterable(f []Producer, opts ...Option) Iterable {
return &deferIterable{
f: f,
fs: f,
opts: opts,
}
}
Expand All @@ -21,18 +17,11 @@ func (i *deferIterable) Observe(opts ...Option) <-chan Item {
next := option.buildChannel()
ctx := option.buildContext()

wg := sync.WaitGroup{}
for _, f := range i.f {
f := f
wg.Add(1)
go func() {
defer wg.Done()
f(ctx, next)
}()
}
go func() {
wg.Wait()
close(next)
defer close(next)
for _, f := range i.fs {
f(ctx, next)
}
}()

return next
Expand Down

0 comments on commit e94d354

Please sign in to comment.