mirror of
https://github.com/walles/moar.git
synced 2024-09-11 12:15:43 +03:00
Fix synchronization
After testing it. We used to crash, you are not allowed to close already-closed channels.
This commit is contained in:
parent
1395ea5628
commit
350c370db6
24
m/reader.go
24
m/reader.go
@ -45,8 +45,8 @@ type Reader struct {
|
|||||||
|
|
||||||
highlightingStyle chan chroma.Style
|
highlightingStyle chan chroma.Style
|
||||||
|
|
||||||
// This channel will be closed either when the first byte arrives or when
|
// This channel expects to be read exactly once. All other uses will lead to
|
||||||
// the stream ends, whichever happens first.
|
// undefined behavior.
|
||||||
doneWaitingForFirstByte chan bool
|
doneWaitingForFirstByte chan bool
|
||||||
|
|
||||||
// For telling the UI it should recheck the --quit-if-one-screen conditions.
|
// For telling the UI it should recheck the --quit-if-one-screen conditions.
|
||||||
@ -125,7 +125,13 @@ func (reader *Reader) readStream(stream io.Reader, originalFileName *string, onD
|
|||||||
var err error
|
var err error
|
||||||
for keepReadingLine {
|
for keepReadingLine {
|
||||||
lineBytes, keepReadingLine, err = bufioReader.ReadLine()
|
lineBytes, keepReadingLine, err = bufioReader.ReadLine()
|
||||||
close(reader.doneWaitingForFirstByte)
|
|
||||||
|
// Async write, it might already have been written to
|
||||||
|
select {
|
||||||
|
case reader.doneWaitingForFirstByte <- true:
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
completeLine = append(completeLine, lineBytes...)
|
completeLine = append(completeLine, lineBytes...)
|
||||||
continue
|
continue
|
||||||
@ -176,6 +182,14 @@ func (reader *Reader) readStream(stream io.Reader, originalFileName *string, onD
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If the stream was empty we never got any first byte. Make sure people
|
||||||
|
// stop waiting in this case. Async write since it might already have been
|
||||||
|
// written to.
|
||||||
|
select {
|
||||||
|
case reader.doneWaitingForFirstByte <- true:
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
|
||||||
if onDone != nil {
|
if onDone != nil {
|
||||||
onDone()
|
onDone()
|
||||||
}
|
}
|
||||||
@ -236,7 +250,7 @@ func newReaderFromStream(reader io.Reader, originalFileName *string, formatter c
|
|||||||
moreLinesAdded: make(chan bool, 1),
|
moreLinesAdded: make(chan bool, 1),
|
||||||
maybeDone: make(chan bool, 1),
|
maybeDone: make(chan bool, 1),
|
||||||
highlightingStyle: make(chan chroma.Style, 1),
|
highlightingStyle: make(chan chroma.Style, 1),
|
||||||
doneWaitingForFirstByte: make(chan bool),
|
doneWaitingForFirstByte: make(chan bool, 1),
|
||||||
highlightingDone: &highlightingDone,
|
highlightingDone: &highlightingDone,
|
||||||
done: &done,
|
done: &done,
|
||||||
}
|
}
|
||||||
@ -276,7 +290,7 @@ func NewReaderFromText(name string, text string) *Reader {
|
|||||||
lines: lines,
|
lines: lines,
|
||||||
done: &done,
|
done: &done,
|
||||||
highlightingDone: &highlightingDone,
|
highlightingDone: &highlightingDone,
|
||||||
doneWaitingForFirstByte: make(chan bool),
|
doneWaitingForFirstByte: make(chan bool, 1),
|
||||||
}
|
}
|
||||||
if name != "" {
|
if name != "" {
|
||||||
returnMe.name = &name
|
returnMe.name = &name
|
||||||
|
Loading…
Reference in New Issue
Block a user