mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-24 18:02:02 +01:00
uidriver/mobile: Use timer.NewTimer instead of time.After
timer.NewTimer is much better than time.After in terms of memory usage. See also https://golang.org/pkg/time/#After
This commit is contained in:
parent
ca907e2846
commit
13ce1c28b4
@ -232,13 +232,15 @@ func decode(context *audio.Context, buf []byte, try int) (*Stream, error) {
|
||||
u8.Release()
|
||||
|
||||
timeout := time.Duration(math.Pow(2, float64(try))) * time.Second
|
||||
t := time.NewTimer(timeout)
|
||||
defer t.Stop()
|
||||
|
||||
select {
|
||||
case err := <-ch:
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
case <-time.After(timeout):
|
||||
case <-t.C:
|
||||
// Sometimes decode fails without calling the callbacks (#464).
|
||||
// Let's just try again in this case.
|
||||
return nil, errTimeout
|
||||
|
@ -63,12 +63,16 @@ func (u *UserInterface) Render(chError <-chan error) error {
|
||||
return errors.New("ui: chError must not be nil")
|
||||
}
|
||||
// TODO: Check this is called on the rendering thread
|
||||
|
||||
t := time.NewTimer(500 * time.Millisecond)
|
||||
defer t.Stop()
|
||||
|
||||
select {
|
||||
case err := <-chError:
|
||||
return err
|
||||
case renderCh <- struct{}{}:
|
||||
return opengl.Get().DoWork(renderEndCh)
|
||||
case <-time.After(500 * time.Millisecond):
|
||||
case <-t.C:
|
||||
// This function must not be blocked. We need to break for timeout.
|
||||
return nil
|
||||
}
|
||||
@ -247,10 +251,13 @@ func (u *UserInterface) scaleImpl() float64 {
|
||||
func (u *UserInterface) update(context driver.UIContext) error {
|
||||
render:
|
||||
for {
|
||||
t := time.NewTimer(500 * time.Millisecond)
|
||||
defer t.Stop()
|
||||
|
||||
select {
|
||||
case <-renderCh:
|
||||
break render
|
||||
case <-time.After(500 * time.Millisecond):
|
||||
case <-t.C:
|
||||
context.SuspendAudio()
|
||||
continue
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user