elvish/eval/port.go

39 lines
710 B
Go
Raw Normal View History

package eval
import "os"
2016-02-09 03:11:20 +08:00
// Port conveys data stream. It always consists of a byte band and a channel band.
type Port struct {
File *os.File
Chan chan Value
CloseFile bool
CloseChan bool
}
2016-02-09 08:40:36 +08:00
// Fork returns a copy of a Port with the Close* flags unset.
func (p *Port) Fork() *Port {
return &Port{p.File, p.Chan, false, false}
}
// Close closes a Port.
2016-02-09 03:27:05 +08:00
func (p *Port) Close() {
2016-01-23 01:05:15 +08:00
if p == nil {
return
}
2016-02-09 03:11:20 +08:00
if p.CloseFile {
p.File.Close()
2016-01-23 01:05:15 +08:00
}
2016-02-09 03:11:20 +08:00
if p.CloseChan {
// Logger.Printf("closing channel %v", p.Chan)
2016-02-09 03:11:20 +08:00
close(p.Chan)
2016-01-23 01:05:15 +08:00
}
}
// ClosePorts closes a list of Ports.
2016-02-09 03:27:05 +08:00
func ClosePorts(ports []*Port) {
for _, port := range ports {
// Logger.Printf("closing port %d", i)
2016-02-09 03:27:05 +08:00
port.Close()
}
}