Changing finalizer behavior

This commit is contained in:
Alex Yatskov 2015-12-17 16:16:18 +09:00
parent 4b36ade9bb
commit 4b5f9abd80
2 changed files with 27 additions and 6 deletions

View File

@ -191,27 +191,48 @@ func (gs *goldsmith) chain(s *stage, p Plugin) {
accept, _ := p.(Accepter) accept, _ := p.(Accepter)
proc, _ := p.(Processor) proc, _ := p.(Processor)
fin, _ := p.(Finalizer)
var (
wg sync.WaitGroup
mtx sync.Mutex
files []*File
)
dispatch := func(f *File) {
if fin == nil {
s.output <- f
} else {
mtx.Lock()
files = append(files, f)
mtx.Unlock()
}
}
var wg sync.WaitGroup
for file := range s.input { for file := range s.input {
if file.Err != nil || proc == nil || (accept != nil && !accept.Accept(file)) { if file.Err != nil || proc == nil || (accept != nil && !accept.Accept(file)) {
s.output <- file dispatch(file)
} else { } else {
wg.Add(1) wg.Add(1)
go func(f *File) { go func(f *File) {
defer wg.Done() defer wg.Done()
if proc.Process(s, f) { if proc.Process(s, f) {
s.output <- f dispatch(f)
} else { } else {
gs.decFiles() gs.decFiles()
} }
}(file) }(file)
} }
} }
wg.Wait() wg.Wait()
if fin, ok := p.(Finalizer); ok { if fin != nil {
s.err = fin.Finalize(s) if s.err = fin.Finalize(s, files); s.err == nil {
for _, file := range files {
s.output <- file
}
}
} }
} }

View File

@ -68,7 +68,7 @@ type Initializer interface {
} }
type Finalizer interface { type Finalizer interface {
Finalize(ctx Context) error Finalize(ctx Context, files []*File) error
} }
type Processor interface { type Processor interface {