-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathitems.go
More file actions
87 lines (71 loc) · 1.63 KB
/
items.go
File metadata and controls
87 lines (71 loc) · 1.63 KB
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 batchr
import "time"
type batchItems[V any] map[int]*itemsHolder[V]
func (i *batchItems[V]) add(idx int, item V) {
if holder, exists := (*i)[idx]; exists {
holder.add(item)
}
}
func (i *batchItems[V]) initNewItems(idx int) {
if _, exists := (*i)[idx]; !exists {
(*i)[idx] = newItemsHolder[V]()
}
}
func (i *batchItems[V]) unlinkOldItems(idx int) {
delete(*i, idx)
}
func (i *batchItems[V]) existsAndEmpty(idx int) (exists, empty bool) {
var holder *itemsHolder[V]
if holder, exists = (*i)[idx]; holder != nil && exists {
empty = holder.isEmpty()
}
return
}
func (i *batchItems[V]) nextItems(oldIdx, newIdx int) {
if _, exists := (*i)[oldIdx]; exists {
i.unlinkOldItems(oldIdx)
i.initNewItems(newIdx)
}
}
func (i *batchItems[V]) fetchItems(idx int) (r []V) {
if holder, exists := (*i)[idx]; exists {
r = holder.getItems()
}
return
}
func (i *batchItems[V]) fetchLastUpdated(idx int) (r *time.Time) {
if holder, exists := (*i)[idx]; exists {
r = holder.getLastUpdated()
}
return
}
func (i *batchItems[V]) size(idx int) (r int) {
if holder, exists := (*i)[idx]; exists {
r = len(holder.items)
}
return
}
type itemsHolder[V any] struct {
items []V
lastUpdated *time.Time
}
func newItemsHolder[V any]() *itemsHolder[V] {
return &itemsHolder[V]{}
}
func (h *itemsHolder[V]) add(item V) {
h.items = append(h.items, item)
now := time.Now()
h.lastUpdated = &now
}
func (h *itemsHolder[V]) isEmpty() bool {
return len(h.items) == 0
}
func (h *itemsHolder[V]) getItems() (r []V) {
if !h.isEmpty() {
r = h.items
}
return
}
func (h *itemsHolder[V]) getLastUpdated() *time.Time {
return h.lastUpdated
}