Skip to content

Commit dada667

Browse files
committed
Use honnef.co/go/js/xhr package for XHR.
1 parent 747dee2 commit dada667

File tree

1 file changed

+25
-28
lines changed

1 file changed

+25
-28
lines changed

playground/playground.go

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ import (
1313
"github.com/gopherjs/gopherjs/compiler"
1414
"github.com/gopherjs/gopherjs/js"
1515
"github.com/neelance/go-angularjs"
16-
"honnef.co/go/js/dom" // TODO: Should it be used?
16+
"honnef.co/go/js/dom"
17+
"honnef.co/go/js/xhr"
1718
)
1819

1920
type Line map[string]string
@@ -30,28 +31,26 @@ func main() {
3031
if strings.HasPrefix(dom.GetWindow().Location().Hash, "#/") {
3132
id := dom.GetWindow().Location().Hash[2:]
3233

33-
// TODO: Maybe use "honnef.co/go/js/xhr" now that there are more requests being done?
34-
req := js.Global.Get("XMLHttpRequest").New()
35-
req.Call("open", "GET", "http://"+snippetStoreHost+"/p/"+id, true)
36-
req.Set("responseType", "arraybuffer")
37-
req.Set("onload", func() {
38-
if req.Get("status").Int() != 200 {
34+
req := xhr.NewRequest("GET", "http://"+snippetStoreHost+"/p/"+id)
35+
req.ResponseType = xhr.ArrayBuffer
36+
go func() {
37+
err := req.Send(nil)
38+
if err != nil || req.Status != 200 {
3939
scope.Apply(func() {
4040
scope.Set("output", []Line{Line{"type": "err", "content": `cannot load snippet "` + id + `"`}})
4141
})
4242
return
4343
}
4444

45-
data := js.Global.Get("Uint8Array").New(req.Get("response")).Interface().([]byte)
45+
data := js.Global.Get("Uint8Array").New(req.Response).Interface().([]byte)
4646
scope.Apply(func() {
4747
scope.Set("code", string(data))
4848
})
49-
})
50-
req.Call("send")
49+
}()
5150
} else {
5251
scope.Set("code", "package main\n\nimport (\n\t\"fmt\"\n\t\"github.com/gopherjs/gopherjs/js\"\n)\n\nfunc main() {\n\tfmt.Println(\"Hello, playground\")\n\tjs.Global.Call(\"alert\", \"Hello, JavaScript\")\n\tprintln(\"Hello, JS console\")\n}\n")
5352
}
54-
//scope.Set("shareUrl", "")
53+
scope.Set("shareUrl", "")
5554
scope.Set("showShareUrl", false)
5655
scope.Set("showGenerated", false)
5756
scope.Set("generated", `(generated code will be shown here after clicking "Run")`)
@@ -154,18 +153,18 @@ func main() {
154153
for _, p := range pkgsToLoad {
155154
path := p
156155

157-
req := js.Global.Get("XMLHttpRequest").New()
158-
req.Call("open", "GET", "pkg/"+path+".a.js", true)
159-
req.Set("responseType", "arraybuffer")
160-
req.Set("onload", func() {
161-
if req.Get("status").Int() != 200 {
156+
req := xhr.NewRequest("GET", "pkg/"+path+".a.js")
157+
req.ResponseType = xhr.ArrayBuffer
158+
go func() {
159+
err := req.Send(nil)
160+
if err != nil || req.Status != 200 {
162161
scope.Apply(func() {
163162
scope.Set("output", []Line{Line{"type": "err", "content": `cannot load package "` + path + `"`}})
164163
})
165164
return
166165
}
167166

168-
data := js.Global.Get("Uint8Array").New(req.Get("response")).Interface().([]byte)
167+
data := js.Global.Get("Uint8Array").New(req.Response).Interface().([]byte)
169168
packages[path], err = compiler.ReadArchive(path+".a", path, bytes.NewReader(data), importContext.Packages)
170169
if err != nil {
171170
scope.Apply(func() {
@@ -177,8 +176,7 @@ func main() {
177176
if pkgsReceived == len(pkgsToLoad) {
178177
run(loadOnly)
179178
}
180-
})
181-
req.Call("send")
179+
}()
182180
}
183181
return
184182
}
@@ -212,18 +210,19 @@ func main() {
212210
})
213211

214212
scope.Set("share", func() {
215-
req := js.Global.Get("XMLHttpRequest").New()
216-
req.Call("open", "POST", "http://"+snippetStoreHost+"/share", true)
217-
req.Set("responseType", "arraybuffer")
218-
req.Set("onload", func() {
219-
if req.Get("status").Int() != 200 {
213+
req := xhr.NewRequest("POST", "http://"+snippetStoreHost+"/share")
214+
req.ResponseType = xhr.ArrayBuffer
215+
go func() {
216+
// TODO: Send as binary?
217+
err := req.Send(scope.Get("code").Str())
218+
if err != nil || req.Status != 200 {
220219
scope.Apply(func() {
221220
scope.Set("output", []Line{Line{"type": "err", "content": `cannot share snippet`}})
222221
})
223222
return
224223
}
225224

226-
data := js.Global.Get("Uint8Array").New(req.Get("response")).Interface().([]byte)
225+
data := js.Global.Get("Uint8Array").New(req.Response).Interface().([]byte)
227226
scope.Apply(func() {
228227
id := string(data)
229228

@@ -238,9 +237,7 @@ func main() {
238237
dom.GetWindow().Document().GetElementByID("share-url").(*dom.HTMLInputElement).Select()
239238
}()
240239
})
241-
})
242-
// TODO: Should be sending as binary blob or is text okay?
243-
req.Call("send", scope.Get("code").Str())
240+
}()
244241
})
245242
})
246243
}

0 commit comments

Comments
 (0)