mirror of
https://github.com/golang/go
synced 2024-11-19 04:24:39 -07:00
0bbdf54eff
This change modifies the invalidContent function to take a file change type. This allows us to eliminate the separate invalidateMetadata function. The logic of watching changed files is then further pushed into the caching layer. Updates golang/go#34218 Change-Id: Id31b3931c45ec408b6e7b4a362e00f9091ba4f70 Reviewed-on: https://go-review.googlesource.com/c/tools/+/201221 Run-TryBot: Rebecca Stambler <rstambler@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Cottrell <iancottrell@google.com>
67 lines
1.5 KiB
Go
67 lines
1.5 KiB
Go
// 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"
|
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
|
)
|
|
|
|
type watcher struct {
|
|
id uint64
|
|
callback func(changeType protocol.FileChangeType) bool
|
|
}
|
|
|
|
type WatchMap struct {
|
|
mu sync.Mutex
|
|
nextID uint64
|
|
watchers map[interface{}][]watcher
|
|
}
|
|
|
|
func NewWatchMap() *WatchMap {
|
|
return &WatchMap{watchers: make(map[interface{}][]watcher)}
|
|
}
|
|
func (w *WatchMap) Watch(key interface{}, callback func(protocol.FileChangeType) bool) func() {
|
|
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]
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func (w *WatchMap) Notify(key interface{}, changeType protocol.FileChangeType) bool {
|
|
// Make a copy of the watcher callbacks so we don't need to hold
|
|
// the mutex during the callbacks (to avoid deadlocks).
|
|
w.mu.Lock()
|
|
entries := w.watchers[key]
|
|
entriesCopy := make([]watcher, len(entries))
|
|
copy(entriesCopy, entries)
|
|
w.mu.Unlock()
|
|
|
|
var result bool
|
|
for _, entry := range entriesCopy {
|
|
result = entry.callback(changeType) || result
|
|
}
|
|
return result
|
|
}
|