/
tile_container.go
84 lines (66 loc) · 1.4 KB
/
tile_container.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
package app
import (
"sync"
"github.com/itglobal/dashboard/tile"
)
type tileContainer struct {
manager *tileManager
provider tile.Provider
tiles map[tile.ID]*tileObject
mutex sync.Mutex
hasChanges bool
}
func newTileContainer(manager *tileManager, provider tile.Provider) *tileContainer {
return &tileContainer{
manager: manager,
provider: provider,
tiles: make(map[tile.ID]*tileObject),
}
}
func (u *tileContainer) BeginUpdate() {
u.mutex.Lock()
}
func (u *tileContainer) GetTiles() []tile.Tile {
var tiles = make([]tile.Tile, len(u.tiles))
i := 0
for _, t := range u.tiles {
tiles[i] = t
i++
}
return tiles
}
func (u *tileContainer) AddOrUpdateTile(id tile.ID) tile.Tile {
tile, exists := u.tiles[id]
if !exists {
tile = newTileObject(u, id)
u.tiles[id] = tile
u.hasChanges = true
}
return tile
}
func (u *tileContainer) RemoveTile(id tile.ID) {
if _, exists := u.tiles[id]; !exists {
return
}
delete(u.tiles, id)
u.hasChanges = true
}
func (u *tileContainer) EndUpdate() {
hasChanges := u.hasChanges
u.mutex.Unlock()
if hasChanges {
u.manager.notifyChanged(u.provider)
}
}
func (u *tileContainer) Invalidate() {
u.hasChanges = true
}
func (u *tileContainer) getJSON(arr []tileJSON) []tileJSON {
u.mutex.Lock()
defer u.mutex.Unlock()
for _, tile := range u.tiles {
jsonObject := tile.getJSON()
arr = append(arr, jsonObject)
}
return arr
}