Skip to content

Commit

Permalink
playground: Make format synchronous.
Browse files Browse the repository at this point in the history
Previous code would generate "$rootScope:inprog" AngularJS error (see
https://docs.angularjs.org/error/$rootScope/inprog).

I'm not fully sure what changed to cause that, but it seems this code
can now be executed synchronously without a goroutine (i.e., it makes
no blocking calls). According to AngularJS documentation (linked
above), that is one possible cause of this error:

> This error is often seen when interacting with an API that is
sometimes sync and sometimes async.

Perhaps something changed to cause formatting to no longer by
asynchronous.

Remove the goroutine and scope.Apply calls. It seems to work, but I'm
not familiar with AngularJS enough to be confident this is the best
thing to do. I'm also not sure what exactly changed to trigger the
original error.

Related to #64. Hopefully we can stop relying on AngularJS soon, and
this potential problem can go away/wouldn't have happened.
  • Loading branch information
dmitshur committed Feb 17, 2017
1 parent 5801b5d commit 723a993
Showing 1 changed file with 15 additions and 21 deletions.
36 changes: 15 additions & 21 deletions playground/playground.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,27 +206,21 @@ func main() {
}()

scope.Set("format", func() {
go func() {
code := []byte(scope.Get("code").String())
var out []byte
var err error
switch scope.Get("imports").Bool() {
case true:
out, err = imports.Process("prog.go", code, nil)
case false:
out, err = format.Source(code)
}
if err != nil {
scope.Apply(func() {
scope.Set("output", []Line{Line{"type": "err", "content": err.Error()}})
})
return
}
scope.Apply(func() {
scope.Set("code", string(out))
scope.Set("output", []Line{})
})
}()
code := []byte(scope.Get("code").String())
var out []byte
var err error
switch scope.Get("imports").Bool() {
case true:
out, err = imports.Process("prog.go", code, nil)
case false:
out, err = format.Source(code)
}
if err != nil {
scope.Set("output", []Line{Line{"type": "err", "content": err.Error()}})
return
}
scope.Set("code", string(out))
scope.Set("output", []Line{})
})

scope.Set("share", func() {
Expand Down

0 comments on commit 723a993

Please sign in to comment.