Skip to content

Commit ee7de23

Browse files
committed
don't load packages multiple times (fixes #27)
1 parent 2f1c8dc commit ee7de23

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

playground/playground.go

+7-9
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@ func main() {
5959
scope.Set("showShareUrl", false)
6060

6161
packages := make(map[string]*compiler.Archive)
62-
var pkgsToLoad []string
62+
var pkgsToLoad map[string]bool
6363
importContext := compiler.NewImportContext(func(path string) (*compiler.Archive, error) {
6464
if pkg, found := packages[path]; found {
6565
return pkg, nil
6666
}
67-
pkgsToLoad = append(pkgsToLoad, path)
67+
pkgsToLoad[path] = true
6868
return &compiler.Archive{}, nil
6969
})
7070
fileSet := token.NewFileSet()
@@ -116,7 +116,7 @@ func main() {
116116
run = func(loadOnly bool) {
117117
output = nil
118118
scope.Set("output", output)
119-
pkgsToLoad = nil
119+
pkgsToLoad = make(map[string]bool)
120120

121121
file, err := parser.ParseFile(fileSet, "prog.go", []byte(scope.Get("code").String()), parser.ParseComments)
122122
if err != nil {
@@ -135,7 +135,7 @@ func main() {
135135
packages["main"] = mainPkg
136136
if err != nil && len(pkgsToLoad) == 0 {
137137
if list, ok := err.(compiler.ErrorList); ok {
138-
output := make([]Line, 0)
138+
var output []Line
139139
for _, entry := range list {
140140
output = append(output, Line{"type": "err", "content": entry.Error()})
141141
}
@@ -153,12 +153,10 @@ func main() {
153153

154154
if len(pkgsToLoad) != 0 {
155155
pkgsReceived = 0
156-
for _, p := range pkgsToLoad {
157-
path := p
158-
156+
for path := range pkgsToLoad {
159157
req := xhr.NewRequest("GET", "pkg/"+path+".a.js")
160158
req.ResponseType = xhr.ArrayBuffer
161-
go func() {
159+
go func(path string) {
162160
err := req.Send(nil)
163161
if err != nil || req.Status != 200 {
164162
scope.Apply(func() {
@@ -179,7 +177,7 @@ func main() {
179177
if pkgsReceived == len(pkgsToLoad) {
180178
run(loadOnly)
181179
}
182-
}()
180+
}(path)
183181
}
184182
return
185183
}

0 commit comments

Comments
 (0)