From edfa7c9dbf799edc239f9fcaa75ae08d443fb8d4 Mon Sep 17 00:00:00 2001 From: chzyer <0@0xdf.com> Date: Sun, 4 Sep 2016 20:49:14 +0800 Subject: [PATCH] lazy fire ioloop (#77) --- std.go | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/std.go b/std.go index db7901b..28439f9 100644 --- a/std.go +++ b/std.go @@ -66,12 +66,13 @@ func Line(prompt string) (string, error) { } type CancelableStdin struct { - mutex sync.Mutex - stop chan struct{} - notify chan struct{} - data []byte - read int - err error + mutex sync.Mutex + stop chan struct{} + notify chan struct{} + data []byte + read int + err error + ioloopFired bool } func NewCancelableStdin() *CancelableStdin { @@ -79,7 +80,6 @@ func NewCancelableStdin() *CancelableStdin { notify: make(chan struct{}), stop: make(chan struct{}), } - go c.ioloop() return c } @@ -99,6 +99,10 @@ loop: func (c *CancelableStdin) Read(b []byte) (n int, err error) { c.mutex.Lock() defer c.mutex.Unlock() + if !c.ioloopFired { + c.ioloopFired = true + go c.ioloop() + } c.data = b c.notify <- struct{}{}