| // 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. |
| watchers map[interface{}][]watcher |
| func NewWatchMap() *WatchMap { |
| return &WatchMap{watchers: make(map[interface{}][]watcher)} |
| func (w *WatchMap) Watch(key interface{}, callback func()) func() { |
| w.watchers[key] = append(w.watchers[key], watcher{ |
| // find and delete the watcher entry |
| entries := w.watchers[key] |
| for i, entry := range entries { |
| entries[i] = entries[len(entries)-1] |
| entries = entries[:len(entries)-1] |
| func (w *WatchMap) Notify(key interface{}) { |
| for _, entry := range w.watchers[key] { |