-
Notifications
You must be signed in to change notification settings - Fork 2
/
notifies.go
50 lines (38 loc) · 801 Bytes
/
notifies.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
package main
import (
"fmt"
"sync"
"github.com/onodera-punpun/oshirase"
)
type notifies struct {
Notifies []oshirase.Notify
NMU sync.RWMutex
}
func newNotifies() *notifies {
return ¬ifies{}
}
func (ns *notifies) add(n *oshirase.Notify) {
ns.NMU.Lock()
defer ns.NMU.Unlock()
ns.Notifies = append(ns.Notifies, *n)
}
func (ns *notifies) delete(id uint32) error {
idx, err := ns.findByID(id)
if err != nil {
return err
}
ns.NMU.Lock()
defer ns.NMU.Unlock()
ns.Notifies = append(ns.Notifies[:idx], ns.Notifies[idx+1:]...)
return nil
}
func (ns *notifies) findByID(id uint32) (index int, err error) {
ns.NMU.RLock()
defer ns.NMU.RUnlock()
for i, n := range ns.Notifies {
if n.ID == id {
return i, nil
}
}
return -1, fmt.Errorf("id %d dosesn't exist", id)
}