2019-05-17 10:15:22 -06:00
|
|
|
// Copyright 2019 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
2019-10-15 16:07:52 -06:00
|
|
|
|
2019-11-13 11:14:21 -07:00
|
|
|
"golang.org/x/tools/internal/lsp/source"
|
2019-05-17 10:15:22 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
type watcher struct {
|
|
|
|
id uint64
|
2019-11-13 11:14:21 -07:00
|
|
|
callback func(action source.FileAction) bool
|
2019-05-17 10:15:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type WatchMap struct {
|
|
|
|
mu sync.Mutex
|
|
|
|
nextID uint64
|
|
|
|
watchers map[interface{}][]watcher
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewWatchMap() *WatchMap {
|
|
|
|
return &WatchMap{watchers: make(map[interface{}][]watcher)}
|
|
|
|
}
|
2019-11-13 11:14:21 -07:00
|
|
|
|
|
|
|
func (w *WatchMap) Watch(key interface{}, callback func(source.FileAction) bool) func() {
|
2019-05-17 10:15:22 -06:00
|
|
|
w.mu.Lock()
|
|
|
|
defer w.mu.Unlock()
|
|
|
|
id := w.nextID
|
|
|
|
w.nextID++
|
|
|
|
w.watchers[key] = append(w.watchers[key], watcher{
|
|
|
|
id: id,
|
|
|
|
callback: callback,
|
|
|
|
})
|
|
|
|
return func() {
|
|
|
|
// unwatch if invoked
|
|
|
|
w.mu.Lock()
|
|
|
|
defer w.mu.Unlock()
|
|
|
|
// find and delete the watcher entry
|
|
|
|
entries := w.watchers[key]
|
|
|
|
for i, entry := range entries {
|
|
|
|
if entry.id == id {
|
|
|
|
// found it
|
|
|
|
entries[i] = entries[len(entries)-1]
|
|
|
|
entries = entries[:len(entries)-1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-13 11:14:21 -07:00
|
|
|
func (w *WatchMap) Notify(key interface{}, action source.FileAction) bool {
|
2019-07-03 10:46:23 -06:00
|
|
|
// Make a copy of the watcher callbacks so we don't need to hold
|
|
|
|
// the mutex during the callbacks (to avoid deadlocks).
|
2019-05-17 10:15:22 -06:00
|
|
|
w.mu.Lock()
|
2019-07-03 10:46:23 -06:00
|
|
|
entries := w.watchers[key]
|
|
|
|
entriesCopy := make([]watcher, len(entries))
|
|
|
|
copy(entriesCopy, entries)
|
|
|
|
w.mu.Unlock()
|
|
|
|
|
2019-10-15 16:07:52 -06:00
|
|
|
var result bool
|
2019-07-03 10:46:23 -06:00
|
|
|
for _, entry := range entriesCopy {
|
2019-11-13 11:14:21 -07:00
|
|
|
result = entry.callback(action) || result
|
2019-05-17 10:15:22 -06:00
|
|
|
}
|
2019-10-15 16:07:52 -06:00
|
|
|
return result
|
2019-05-17 10:15:22 -06:00
|
|
|
}
|