audio: Avoid using time.After on browsers for performance

This commit is contained in:
Hajime Hoshi 2017-07-02 17:27:07 +09:00
parent 45ca2a1f79
commit d0778b5253
3 changed files with 53 additions and 1 deletions

View File

@ -430,7 +430,7 @@ func (p *Player) readToBuffer(length int) (int, error) {
if len(r.data) > 0 {
p.buf = append(p.buf, r.data...)
}
case <-time.After(15 * time.Millisecond):
case <-timeoutIfPossible(15 * time.Millisecond):
if l := length - len(p.buf); l > 0 {
empty := make([]uint8, l)
p.buf = append(p.buf, empty...)

27
audio/timeout_js.go Normal file
View File

@ -0,0 +1,27 @@
// Copyright 2017 The Ebiten Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// +build js
package audio
import (
"time"
)
func timeoutIfPossible(t time.Duration) <-chan time.Time {
// time.After uses setTimeout and causes performance problems.
// This function returns null and actually blocks forever.
return nil
}

25
audio/timeout_notjs.go Normal file
View File

@ -0,0 +1,25 @@
// Copyright 2017 The Ebiten Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// +build !js
package audio
import (
"time"
)
func timeoutIfPossible(t time.Duration) <-chan time.Time {
return time.After(t)
}