-
-
Notifications
You must be signed in to change notification settings - Fork 355
/
storage.go
183 lines (145 loc) · 3.22 KB
/
storage.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package app
import (
"encoding/json"
"sync"
"github.com/maxence-charriere/go-app/v9/pkg/errors"
)
// BrowserStorage is the interface that describes a web browser storage.
type BrowserStorage interface {
// Set sets the value to the given key. The value must be json convertible.
Set(k string, v interface{}) error
// Get gets the item associated to the given key and store it in the given
// value.
// It returns an error if v is not a pointer.
Get(k string, v interface{}) error
// Del deletes the item associated with the given key.
Del(k string)
// Len returns the number of items stored.
Len() int
// Key returns the key of the item associated to the given index.
Key(i int) (string, error)
// Clear deletes all items.
Clear()
}
type memoryStorage struct {
mu sync.RWMutex
data map[string][]byte
}
func newMemoryStorage() *memoryStorage {
return &memoryStorage{
data: make(map[string][]byte),
}
}
func (s *memoryStorage) Set(k string, v interface{}) error {
b, err := json.Marshal(v)
if err != nil {
return err
}
s.mu.Lock()
s.data[k] = b
s.mu.Unlock()
return nil
}
func (s *memoryStorage) Get(k string, v interface{}) error {
s.mu.RLock()
d, ok := s.data[k]
if !ok {
s.mu.RUnlock()
return nil
}
s.mu.RUnlock()
return json.Unmarshal(d, v)
}
func (s *memoryStorage) Del(k string) {
s.mu.Lock()
delete(s.data, k)
s.mu.Unlock()
}
func (s *memoryStorage) Clear() {
s.mu.Lock()
for k := range s.data {
delete(s.data, k)
}
s.mu.Unlock()
}
func (s *memoryStorage) Len() int {
s.mu.RLock()
l := len(s.data)
s.mu.RUnlock()
return l
}
func (s *memoryStorage) Key(i int) (string, error) {
s.mu.RLock()
defer s.mu.RUnlock()
j := 0
for k := range s.data {
if i == j {
return k, nil
}
j++
}
return "", errors.New("index out of range").
Tag("index", i).
Tag("len", s.Len())
}
type jsStorage struct {
name string
mutex sync.RWMutex
}
func newJSStorage(name string) *jsStorage {
return &jsStorage{name: name}
}
func (s *jsStorage) Set(k string, v interface{}) (err error) {
defer func() {
r := recover()
if r != nil {
err = errors.New("setting storage value failed").
Tag("storage-type", s.name).
Tag("key", k).
Wrap(r.(error))
}
}()
s.mutex.Lock()
defer s.mutex.Unlock()
b, err := json.Marshal(v)
if err != nil {
return err
}
Window().Get(s.name).Call("setItem", k, btos(b))
return nil
}
func (s *jsStorage) Get(k string, v interface{}) error {
s.mutex.RLock()
defer s.mutex.RUnlock()
item := Window().Get(s.name).Call("getItem", k)
if !item.Truthy() {
return nil
}
return json.Unmarshal(stob(item.String()), v)
}
func (s *jsStorage) Del(k string) {
s.mutex.Lock()
defer s.mutex.Unlock()
Window().Get(s.name).Call("removeItem", k)
}
func (s *jsStorage) Clear() {
s.mutex.Lock()
defer s.mutex.Unlock()
Window().Get(s.name).Call("clear")
}
func (s *jsStorage) Len() int {
s.mutex.Lock()
defer s.mutex.Unlock()
return s.len()
}
func (s *jsStorage) len() int {
return Window().Get(s.name).Get("length").Int()
}
func (s *jsStorage) Key(i int) (string, error) {
if l := s.len(); i < 0 || i >= l {
return "", errors.New("index out of range").
Tag("index", i).
Tag("len", l)
}
return Window().Get(s.name).Call("key", i).String(), nil
}