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)
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 {
if file.Err != nil || proc == nil || (accept != nil && !accept.Accept(file)) {
s.output <- file
dispatch(file)
} else {
wg.Add(1)
go func(f *File) {
defer wg.Done()
if proc.Process(s, f) {
s.output <- f
dispatch(f)
} else {
gs.decFiles()
}
}(file)
}
}
wg.Wait()
if fin, ok := p.(Finalizer); ok {
s.err = fin.Finalize(s)
if fin != nil {
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 {
Finalize(ctx Context) error
Finalize(ctx Context, files []*File) error
}
type Processor interface {