mirror of https://github.com/chzyer/readline.git
lazy fire ioloop (#77)
This commit is contained in:
parent
bc5c91eb5b
commit
edfa7c9dbf
18
std.go
18
std.go
|
@ -66,12 +66,13 @@ func Line(prompt string) (string, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
type CancelableStdin struct {
|
type CancelableStdin struct {
|
||||||
mutex sync.Mutex
|
mutex sync.Mutex
|
||||||
stop chan struct{}
|
stop chan struct{}
|
||||||
notify chan struct{}
|
notify chan struct{}
|
||||||
data []byte
|
data []byte
|
||||||
read int
|
read int
|
||||||
err error
|
err error
|
||||||
|
ioloopFired bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewCancelableStdin() *CancelableStdin {
|
func NewCancelableStdin() *CancelableStdin {
|
||||||
|
@ -79,7 +80,6 @@ func NewCancelableStdin() *CancelableStdin {
|
||||||
notify: make(chan struct{}),
|
notify: make(chan struct{}),
|
||||||
stop: make(chan struct{}),
|
stop: make(chan struct{}),
|
||||||
}
|
}
|
||||||
go c.ioloop()
|
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -99,6 +99,10 @@ loop:
|
||||||
func (c *CancelableStdin) Read(b []byte) (n int, err error) {
|
func (c *CancelableStdin) Read(b []byte) (n int, err error) {
|
||||||
c.mutex.Lock()
|
c.mutex.Lock()
|
||||||
defer c.mutex.Unlock()
|
defer c.mutex.Unlock()
|
||||||
|
if !c.ioloopFired {
|
||||||
|
c.ioloopFired = true
|
||||||
|
go c.ioloop()
|
||||||
|
}
|
||||||
|
|
||||||
c.data = b
|
c.data = b
|
||||||
c.notify <- struct{}{}
|
c.notify <- struct{}{}
|
||||||
|
|
Loading…
Reference in New Issue