-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
87 lines (79 loc) · 1.6 KB
/
utils.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
83
84
85
86
87
package objhtml
import (
// "fmt"
// "html"
"math/rand"
"strconv"
"strings"
"sync"
)
//=============================================
// Variable Declartions //
//=============================================
type (
//Threadsafe unique list of strings
unique struct {
list []string
mutex sync.Mutex
}
event struct {
clid string
id string
jscript string
reply chan string
}
)
var (
events = make(chan event, 1000) //event channel
replyid unique //Id for replies
replyqueue = make(chan event, 1000) //replies
Ids unique //Id for widgets
renderMutex sync.Mutex
handlers = map[string]func(){}
resources = map[string][]byte{}
)
//=============================================
// Practical //
//=============================================
func SetResource(files map[string][]byte) {
resources = files
}
func escape(s string) string {
s = strings.Replace(s, `"`, `\"`, -1)
s = strings.Replace(s, `'`, `\'`, -1)
return s
//return html.EscapeString(s)
}
func (i *unique) New(prefix string) string {
i.mutex.Lock()
defer i.mutex.Unlock()
if i.list == nil {
i.list = make([]string, 0, 100)
}
var id string
for {
id = prefix + strconv.Itoa(rand.Int())
for _, v := range i.list {
if v == id {
continue
}
}
break
}
i.list = append(i.list, id)
//i.mutex.Unlock()
return id
}
func (i *unique) Remove(id string) {
i.mutex.Lock()
defer i.mutex.Unlock()
if i.list == nil {
return
}
for j, v := range i.list {
if v == id {
i.list = append(i.list[:j], i.list[j+1:]...)
}
}
//i.mutex.Unlock()
}