forked from charmbracelet/bubbletea
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cancelreader.go
72 lines (56 loc) · 1.63 KB
/
cancelreader.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package tea
import (
"fmt"
"io"
"sync"
)
var errCanceled = fmt.Errorf("read cancelled")
// cancelReader is a io.Reader whose Read() calls can be cancelled without data
// being consumed. The cancelReader has to be closed.
type cancelReader interface {
io.ReadCloser
// Cancel cancels ongoing and future reads an returns true if it succeeded.
Cancel() bool
}
// fallbackCancelReader implements cancelReader but does not actually support
// cancelation during an ongoing Read() call. Thus, Cancel() always returns
// false. However, after calling Cancel(), new Read() calls immediately return
// errCanceled and don't consume any data anymore.
type fallbackCancelReader struct {
r io.Reader
cancelled bool
}
// newFallbackCancelReader is a fallback for newCancelReader that cannot
// actually cancel an ongoing read but will immediately return on future reads
// if it has been cancelled.
func newFallbackCancelReader(reader io.Reader) (cancelReader, error) {
return &fallbackCancelReader{r: reader}, nil
}
func (r *fallbackCancelReader) Read(data []byte) (int, error) {
if r.cancelled {
return 0, errCanceled
}
return r.r.Read(data)
}
func (r *fallbackCancelReader) Cancel() bool {
r.cancelled = true
return false
}
func (r *fallbackCancelReader) Close() error {
return nil
}
// cancelMixin represents a goroutine-safe cancelation status.
type cancelMixin struct {
unsafeCancelled bool
lock sync.Mutex
}
func (c *cancelMixin) isCancelled() bool {
c.lock.Lock()
defer c.lock.Unlock()
return c.unsafeCancelled
}
func (c *cancelMixin) setCancelled() {
c.lock.Lock()
defer c.lock.Unlock()
c.unsafeCancelled = true
}