@@ -50,32 +50,46 @@ func (x *executor) Execute(req *Request, re ResponseEmitter, env Environment) er
50
50
return err
51
51
}
52
52
}
53
+ maybeStartPostRun := func (formatters PostRunMap ) <- chan error {
54
+ var (
55
+ postRun func (Response , ResponseEmitter ) error
56
+ postRunCh = make (chan error )
57
+ )
53
58
54
- // contains the error returned by PostRun
55
- errCh := make (chan error , 1 )
56
- if cmd .PostRun != nil {
57
- if typer , ok := re .(interface {
58
- Type () PostRunType
59
- }); ok && cmd .PostRun [typer .Type ()] != nil {
60
- var (
61
- res Response
62
- lower = re
63
- )
64
-
65
- re , res = NewChanResponsePair (req )
59
+ if postRun == nil {
60
+ close (postRunCh )
61
+ return postRunCh
62
+ }
66
63
67
- go func () {
68
- defer close (errCh )
69
- errCh <- lower .CloseWithError (cmd .PostRun [typer .Type ()](res , lower ))
70
- }()
64
+ // check if we have a formatter for this emitter type
65
+ typer , isTyper := re .(interface {
66
+ Type () PostRunType
67
+ })
68
+ if isTyper &&
69
+ formatters [typer .Type ()] != nil {
70
+ postRun = formatters [typer .Type ()]
71
+ } else {
72
+ close (postRunCh )
73
+ return postRunCh
71
74
}
72
- } else {
73
- // not using this channel today
74
- close (errCh )
75
+
76
+ // redirect emitter to us
77
+ // and start waiting for emissions
78
+ var (
79
+ postRes Response
80
+ postEmitter = re
81
+ )
82
+ re , postRes = NewChanResponsePair (req )
83
+ go func () {
84
+ defer close (postRunCh )
85
+ postRunCh <- postEmitter .CloseWithError (postRun (postRes , postEmitter ))
86
+ }()
87
+ return postRunCh
75
88
}
76
89
90
+ postRunCh := maybeStartPostRun (cmd .PostRun )
77
91
runCloseErr := re .CloseWithError (cmd .Run (req , re , env ))
78
- postCloseErr := <- errCh
92
+ postCloseErr := <- postRunCh
79
93
switch runCloseErr {
80
94
case ErrClosingClosedEmitter , nil :
81
95
default :
0 commit comments