Skip to content

Commit

Permalink
main: use wasm-fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
eihigh committed May 31, 2024
1 parent 0a2bec9 commit 26b2e17
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ module example.com/game

go 1.22.3

require github.com/hajimehoshi/ebiten/v2 v2.7.4
require (
github.com/hajimehoshi/ebiten/v2 v2.7.4
marwan.io/wasm-fetch v0.1.0
)

require (
github.com/ebitengine/gomobile v0.0.0-20240518074828-e86332849895 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
marwan.io/wasm-fetch v0.1.0 h1:0kKeCsry7IQfPmxj78FUWtO6qwXIaQqMmZbzbZz2rkw=
marwan.io/wasm-fetch v0.1.0/go.mod h1:PlaZmevjPJGIy/BaqMtVjUKvrA8CgrDh+aomnuhmTO8=
9 changes: 5 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package main

import (
"bytes"
"fmt"
"image/color"
"io"
"net/http"
"os"
"path/filepath"
"runtime"

"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"

fetch "marwan.io/wasm-fetch"
)

type game struct {
Expand Down Expand Up @@ -48,12 +50,11 @@ func main() {
func open(name string) (io.ReadCloser, error) {
name = filepath.Clean(name)
if runtime.GOOS == "js" {
// TODO: use more lightweight method such as marwan-at-work/wasm-fetch
resp, err := http.Get(name)
resp, err := fetch.Fetch(name, &fetch.Opts{})
if err != nil {
return nil, err
}
return resp.Body, nil
return io.NopCloser(bytes.NewReader(resp.Body)), nil
}

return os.Open(name)
Expand Down

0 comments on commit 26b2e17

Please sign in to comment.