Skip to content

Commit 954fb5f

Browse files
authored
Abstract out the context cancel logic for reuse (#7)
1 parent 2748646 commit 954fb5f

File tree

3 files changed

+34
-36
lines changed

3 files changed

+34
-36
lines changed

evaluate.go

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ package typescript
33
import (
44
"context"
55
"fmt"
6-
"github.com/clarkmcc/go-typescript/packages"
7-
_ "github.com/clarkmcc/go-typescript/versions/v4.7.2"
8-
"github.com/dop251/goja"
96
"io"
107
"io/ioutil"
118
"strings"
9+
10+
"github.com/clarkmcc/go-typescript/packages"
11+
_ "github.com/clarkmcc/go-typescript/versions/v4.7.2"
12+
"github.com/dop251/goja"
1213
)
1314

1415
type EvaluateOptionFunc func(cfg *EvaluateConfig)
@@ -102,22 +103,8 @@ func EvaluateCtx(ctx context.Context, src io.Reader, opts ...EvaluateOptionFunc)
102103
for _, fn := range opts {
103104
fn(cfg)
104105
}
105-
done := make(chan struct{})
106-
started := make(chan struct{})
106+
done := startInterruptable(ctx, cfg.Runtime)
107107
defer close(done)
108-
go func() {
109-
// Inform the parent go-routine that we've started, this prevents a race condition where the
110-
// runtime would beat the context cancellation in unit tests even though the context started
111-
// out in a 'cancelled' state.
112-
close(started)
113-
select {
114-
case <-ctx.Done():
115-
cfg.Runtime.Interrupt("context halt")
116-
case <-done:
117-
return
118-
}
119-
}()
120-
<-started
121108
if cfg.HasEvaluateBefore() {
122109
for _, s := range cfg.EvaluateBefore {
123110
b, err := ioutil.ReadAll(s)
@@ -130,6 +117,7 @@ func EvaluateCtx(ctx context.Context, src io.Reader, opts ...EvaluateOptionFunc)
130117
}
131118
}
132119
}
120+
133121
b, err := ioutil.ReadAll(src)
134122
if err != nil {
135123
return nil, fmt.Errorf("reading src: %w", err)
@@ -149,9 +137,7 @@ func EvaluateCtx(ctx context.Context, src io.Reader, opts ...EvaluateOptionFunc)
149137
WithRuntime(cfg.Runtime),
150138
WithPreventCancellation(),
151139
}
152-
for _, opt := range cfg.TranspileOptions {
153-
opts = append(opts, opt)
154-
}
140+
opts = append(opts, cfg.TranspileOptions...)
155141
for _, h := range cfg.ScriptPreTranspileHooks {
156142
script, err = h(script)
157143
if err != nil {

interrupt.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package typescript
2+
3+
import (
4+
"context"
5+
6+
"github.com/dop251/goja"
7+
)
8+
9+
func startInterruptable(ctx context.Context, vm *goja.Runtime) chan struct{} {
10+
done := make(chan struct{})
11+
started := make(chan struct{})
12+
go func() {
13+
// Inform the parent go-routine that we've started, this prevents a race condition where the
14+
// runtime would beat the context cancellation in unit tests even though the context started
15+
// out in a 'cancelled' state.
16+
close(started)
17+
select {
18+
case <-ctx.Done():
19+
vm.Interrupt("context halt")
20+
case <-done:
21+
return
22+
}
23+
}()
24+
<-started
25+
return done
26+
}

transpiler.go

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,8 @@ func TranspileCtx(ctx context.Context, script io.Reader, opts ...TranspileOption
3030
}
3131
// Handle context cancellation
3232
if !cfg.PreventCancellation {
33-
done := make(chan struct{})
34-
started := make(chan struct{})
33+
done := startInterruptable(ctx, cfg.Runtime)
3534
defer close(done)
36-
go func() {
37-
// Inform the parent go-routine that we've started, this prevents a race condition where the
38-
// runtime would beat the context cancellation in unit tests even though the context started
39-
// out in a 'cancelled' state.
40-
close(started)
41-
select {
42-
case <-ctx.Done():
43-
cfg.Runtime.Interrupt("halt")
44-
case <-done:
45-
return
46-
}
47-
}()
48-
<-started
4935
}
5036
err := cfg.Initialize()
5137
if err != nil {

0 commit comments

Comments
 (0)