@@ -13,7 +13,8 @@ import (
13
13
"github.com/gopherjs/gopherjs/compiler"
14
14
"github.com/gopherjs/gopherjs/js"
15
15
"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"
17
18
)
18
19
19
20
type Line map [string ]string
@@ -30,28 +31,26 @@ func main() {
30
31
if strings .HasPrefix (dom .GetWindow ().Location ().Hash , "#/" ) {
31
32
id := dom .GetWindow ().Location ().Hash [2 :]
32
33
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 {
39
39
scope .Apply (func () {
40
40
scope .Set ("output" , []Line {Line {"type" : "err" , "content" : `cannot load snippet "` + id + `"` }})
41
41
})
42
42
return
43
43
}
44
44
45
- data := js .Global .Get ("Uint8Array" ).New (req .Get ( "response" ) ).Interface ().([]byte )
45
+ data := js .Global .Get ("Uint8Array" ).New (req .Response ).Interface ().([]byte )
46
46
scope .Apply (func () {
47
47
scope .Set ("code" , string (data ))
48
48
})
49
- })
50
- req .Call ("send" )
49
+ }()
51
50
} else {
52
51
scope .Set ("code" , "package main\n \n import (\n \t \" fmt\" \n \t \" github.com/gopherjs/gopherjs/js\" \n )\n \n func main() {\n \t fmt.Println(\" Hello, playground\" )\n \t js.Global.Call(\" alert\" , \" Hello, JavaScript\" )\n \t println(\" Hello, JS console\" )\n }\n " )
53
52
}
54
- // scope.Set("shareUrl", "")
53
+ scope .Set ("shareUrl" , "" )
55
54
scope .Set ("showShareUrl" , false )
56
55
scope .Set ("showGenerated" , false )
57
56
scope .Set ("generated" , `(generated code will be shown here after clicking "Run")` )
@@ -154,18 +153,18 @@ func main() {
154
153
for _ , p := range pkgsToLoad {
155
154
path := p
156
155
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 {
162
161
scope .Apply (func () {
163
162
scope .Set ("output" , []Line {Line {"type" : "err" , "content" : `cannot load package "` + path + `"` }})
164
163
})
165
164
return
166
165
}
167
166
168
- data := js .Global .Get ("Uint8Array" ).New (req .Get ( "response" ) ).Interface ().([]byte )
167
+ data := js .Global .Get ("Uint8Array" ).New (req .Response ).Interface ().([]byte )
169
168
packages [path ], err = compiler .ReadArchive (path + ".a" , path , bytes .NewReader (data ), importContext .Packages )
170
169
if err != nil {
171
170
scope .Apply (func () {
@@ -177,8 +176,7 @@ func main() {
177
176
if pkgsReceived == len (pkgsToLoad ) {
178
177
run (loadOnly )
179
178
}
180
- })
181
- req .Call ("send" )
179
+ }()
182
180
}
183
181
return
184
182
}
@@ -212,18 +210,19 @@ func main() {
212
210
})
213
211
214
212
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 {
220
219
scope .Apply (func () {
221
220
scope .Set ("output" , []Line {Line {"type" : "err" , "content" : `cannot share snippet` }})
222
221
})
223
222
return
224
223
}
225
224
226
- data := js .Global .Get ("Uint8Array" ).New (req .Get ( "response" ) ).Interface ().([]byte )
225
+ data := js .Global .Get ("Uint8Array" ).New (req .Response ).Interface ().([]byte )
227
226
scope .Apply (func () {
228
227
id := string (data )
229
228
@@ -238,9 +237,7 @@ func main() {
238
237
dom .GetWindow ().Document ().GetElementByID ("share-url" ).(* dom.HTMLInputElement ).Select ()
239
238
}()
240
239
})
241
- })
242
- // TODO: Should be sending as binary blob or is text okay?
243
- req .Call ("send" , scope .Get ("code" ).Str ())
240
+ }()
244
241
})
245
242
})
246
243
}
0 commit comments