-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
82 lines (71 loc) · 1.88 KB
/
util.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package caddy_esbuild_plugin
import (
"fmt"
"github.com/fsnotify/fsnotify"
"go.uber.org/zap"
"reflect"
"unsafe"
)
func (m *Esbuild) watchFiles(files []string) {
// creates a new file watcher
watcher, err := fsnotify.NewWatcher()
if err != nil {
fmt.Println("ERROR", err)
}
//
done := make(chan bool)
defer watcher.Close()
defer close(done)
//
go func() {
for {
select {
// watch for events
case event := <-watcher.Events:
if event.Op == fsnotify.Write {
done <- true
m.logger.Debug("File changed, rebuilding", zap.String("filename", event.Name))
m.Rebuild()
return
}
case err := <-watcher.Errors:
m.logger.Error("Failed to watch!", zap.Error(err))
case <-m.globalQuit:
if !isChanClosed(done) {
done <- true
}
return
}
}
}()
for _, file := range files {
if err := watcher.Add(file); err != nil {
m.logger.Error("Failed to watch file", zap.Error(err), zap.String("file", file))
}
}
<-done
}
func isChanClosed(ch interface{}) bool {
if reflect.TypeOf(ch).Kind() != reflect.Chan {
panic("only channels!")
}
// get interface value pointer, from cgo_export
// typedef struct { void *t; void *v; } GoInterface;
// then get channel real pointer
cptr := *(*uintptr)(unsafe.Pointer(
unsafe.Pointer(uintptr(unsafe.Pointer(&ch)) + unsafe.Sizeof(uint(0))),
))
// this function will return true if chan.closed > 0
// see hchan on https://github.com/golang/go/blob/master/src/runtime/chan.go
// type hchan struct {
// qcount uint // total data in the queue
// dataqsiz uint // size of the circular queue
// buf unsafe.Pointer // points to an array of dataqsiz elements
// elemsize uint16
// closed uint32
// **
cptr += unsafe.Sizeof(uint(0)) * 2
cptr += unsafe.Sizeof(unsafe.Pointer(uintptr(0)))
cptr += unsafe.Sizeof(uint16(0))
return *(*uint32)(unsafe.Pointer(cptr)) > 0
}