Skip to content

Commit d08215e

Browse files
committed
renamed and improved http.XHRTransport
1 parent 7eddd28 commit d08215e

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

compiler/natives/net/http/http.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,23 @@ import (
1212
"github.com/gopherjs/gopherjs/js"
1313
)
1414

15-
var DefaultTransport RoundTripper = &XhrTransport{
16-
inflight: map[*Request]*js.Object{},
17-
}
15+
var DefaultTransport RoundTripper = &XHRTransport{}
1816

19-
type XhrTransport struct {
17+
type XHRTransport struct {
2018
inflight map[*Request]*js.Object
2119
}
2220

23-
func (t *XhrTransport) RoundTrip(req *Request) (*Response, error) {
21+
func (t *XHRTransport) RoundTrip(req *Request) (*Response, error) {
2422
xhrConstructor := js.Global.Get("XMLHttpRequest")
2523
if xhrConstructor == js.Undefined {
2624
return nil, errors.New("net/http: XMLHttpRequest not available")
2725
}
2826
xhr := xhrConstructor.New()
2927
xhr.Set("responseType", "arraybuffer")
28+
29+
if t.inflight == nil {
30+
t.inflight = map[*Request]*js.Object{}
31+
}
3032
t.inflight[req] = xhr
3133
defer delete(t.inflight, req)
3234

@@ -78,6 +80,8 @@ func (t *XhrTransport) RoundTrip(req *Request) (*Response, error) {
7880
}
7981
}
8082

81-
func (t *XhrTransport) CancelRequest(req *Request) {
82-
t.inflight[req].Call("abort")
83+
func (t *XHRTransport) CancelRequest(req *Request) {
84+
if xhr, ok := t.inflight[req]; ok {
85+
xhr.Call("abort")
86+
}
8387
}

0 commit comments

Comments
 (0)