Skip to content

Commit 811b7d8

Browse files
committed
Simplify Open to return io.ReadCloser.
This allows the browser implementation to return the underlying io.ReaderCloser directly, without having to first cache the entire thing. That enables streaming of the underlying source to be possible (which is possible in browser after Fetch Transport, see gopherjs/gopherjs#454. If seeking is desired, it can be added by the user on top of the returned io.ReadCloser. In many cases, seeking is not very critical and easy to avoid, and definitely not worth the expensive overhead of always supporting it. This is a slightly breaking API change if you depend on seeking, but easy to work around in client code.
1 parent aee868d commit 811b7d8

File tree

2 files changed

+6
-20
lines changed

2 files changed

+6
-20
lines changed

browser.go

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,14 @@
33
package glfw
44

55
import (
6-
"bytes"
76
"errors"
87
"fmt"
98
"io"
10-
"io/ioutil"
119
"log"
1210
"net/http"
1311
"runtime"
1412

1513
"github.com/gopherjs/gopherjs/js"
16-
"golang.org/x/tools/godoc/vfs"
1714
"honnef.co/go/js/dom"
1815
)
1916

@@ -778,29 +775,18 @@ const (
778775
ModSuper
779776
)
780777

781-
// Open opens a named asset.
782-
func Open(name string) (vfs.ReadSeekCloser, error) {
778+
// Open opens a named asset. It's the caller's responsibility to close it when done.
779+
func Open(name string) (io.ReadCloser, error) {
783780
resp, err := http.Get(name)
784781
if err != nil {
785782
return nil, err
786783
}
787-
defer resp.Body.Close()
788784
if resp.StatusCode != 200 {
789785
return nil, fmt.Errorf("non-200 status: %s", resp.Status)
790786
}
791-
b, err := ioutil.ReadAll(resp.Body)
792-
if err != nil {
793-
return nil, err
794-
}
795-
return nopCloser{bytes.NewReader(b)}, nil
787+
return resp.Body, nil
796788
}
797789

798-
type nopCloser struct {
799-
io.ReadSeeker
800-
}
801-
802-
func (nopCloser) Close() error { return nil }
803-
804790
// ---
805791

806792
func WaitEvents() {

desktop.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
package glfw
44

55
import (
6+
"io"
67
"os"
78
"runtime"
89

910
"github.com/go-gl/glfw/v3.1/glfw"
10-
"golang.org/x/tools/godoc/vfs"
1111
)
1212

1313
func init() {
@@ -372,10 +372,10 @@ const (
372372
ModSuper = ModifierKey(glfw.ModSuper)
373373
)
374374

375-
// Open opens a named asset.
375+
// Open opens a named asset. It's the caller's responsibility to close it when done.
376376
//
377377
// For now, assets are read directly from the current working directory.
378-
func Open(name string) (vfs.ReadSeekCloser, error) {
378+
func Open(name string) (io.ReadCloser, error) {
379379
return os.Open(name)
380380
}
381381

0 commit comments

Comments
 (0)