-
Notifications
You must be signed in to change notification settings - Fork 1
/
file_listeners.go
59 lines (53 loc) · 1.27 KB
/
file_listeners.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
48
49
50
51
52
53
54
55
56
57
58
59
package main
import (
"sync"
"github.com/gin-gonic/gin"
)
var listenersMutex = &sync.Mutex{}
var listeners = map[string][]chan interface{}{}
func notifyFileListeners(cleanedPath string) {
listenersMutex.Lock()
defer listenersMutex.Unlock()
if listenersForThisFile, ok := listeners[cleanedPath]; ok {
for _, l := range listenersForThisFile {
select {
case l <- nil:
default:
}
}
}
}
func waitForFileChange(c *gin.Context, cleanedPath string) {
listenerChan := make(chan interface{})
func() {
listenersMutex.Lock()
defer listenersMutex.Unlock()
if _, ok := listeners[cleanedPath]; !ok {
listeners[cleanedPath] = []chan interface{}{}
}
listeners[cleanedPath] = append(listeners[cleanedPath], listenerChan)
}()
select {
case <-listenerChan:
case <-c.Request.Context().Done():
func() {
listenersMutex.Lock()
defer listenersMutex.Unlock()
if listOfListeners, ok := listeners[cleanedPath]; ok {
newList := make([]chan interface{}, 0, len(listOfListeners)-1)
for _, l := range listOfListeners {
if l != listenerChan {
newList = append(newList, l)
}
}
if len(newList) == 0 {
delete(listeners, cleanedPath)
} else {
listeners[cleanedPath] = newList
}
}
}()
c.String(200, "cancelled")
return
}
}