-
Notifications
You must be signed in to change notification settings - Fork 0
/
fragments.go
74 lines (63 loc) · 1.64 KB
/
fragments.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
package html
import "sync"
// Fragment defines a thread safe fragment of HTML.
type Fragment struct {
mu sync.RWMutex
head bool
val string
}
var (
headFragments []*Fragment
headFragmentsLock = sync.RWMutex{}
bodyFragments []*Fragment
bodyFragmentsLock = sync.RWMutex{}
)
// Get is used to get the contents of a fragment.
func (f *Fragment) Get() string {
f.mu.RLock()
x := f.val
f.mu.RUnlock()
return x
}
// Write is used to write a value and trigger a rewrite of whatever fragment chain this belongs to.
func (f *Fragment) Write(val string) {
f.mu.Lock()
f.val = val
f.mu.Unlock()
htmlPartsLock.Lock()
res := ""
if f.head {
headFragmentsLock.RLock()
defer headFragmentsLock.RLock()
for _, v := range headFragments {
res += v.Get()
}
additionalHead = res
} else {
bodyFragmentsLock.RLock()
defer bodyFragmentsLock.RUnlock()
for _, v := range bodyFragments {
res += v.Get()
}
additionalBody = res
}
htmlPartsLock.Unlock()
}
// NewHeadFragment is used to make a new thread safe HTML head element that can be get/set. This will update what is in
// the additional part of the head.
func NewHeadFragment(content string) *Fragment {
headFragmentsLock.Lock()
f := &Fragment{val: content}
headFragments = append(headFragments, f)
headFragmentsLock.Unlock()
return f
}
// NewBodyFragment is used to make a new thread safe HTML body element that can be get/set. This will update what is in
// the additional part of the head.
func NewBodyFragment(content string) *Fragment {
bodyFragmentsLock.Lock()
f := &Fragment{val: content}
bodyFragments = append(bodyFragments, f)
bodyFragmentsLock.Unlock()
return f
}