forked from sourcegraph/go-langserver
/
cancel.go
47 lines (42 loc) · 974 Bytes
/
cancel.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package langserver
import (
"context"
"sync"
"github.com/sourcegraph/jsonrpc2"
)
// cancel manages $/cancelRequest by keeping track of running commands
type cancel struct {
mu sync.Mutex
m map[jsonrpc2.ID]func()
}
// WithCancel is like context.WithCancel, except you can also cancel via
// calling c.Cancel with the same id.
func (c *cancel) WithCancel(ctx context.Context, id jsonrpc2.ID) (context.Context, func()) {
ctx, cancel := context.WithCancel(ctx)
c.mu.Lock()
if c.m == nil {
c.m = make(map[jsonrpc2.ID]func())
}
c.m[id] = cancel
c.mu.Unlock()
return ctx, func() {
c.mu.Lock()
delete(c.m, id)
c.mu.Unlock()
cancel()
}
}
// Cancel will cancel the request with id. If the request has already been
// cancelled or not been tracked before, Cancel is a noop.
func (c *cancel) Cancel(id jsonrpc2.ID) {
var cancel func()
c.mu.Lock()
if c.m != nil {
cancel = c.m[id]
delete(c.m, id)
}
c.mu.Unlock()
if cancel != nil {
cancel()
}
}