forked from yuval-k/harbor
-
Notifications
You must be signed in to change notification settings - Fork 4
/
task_list.go
55 lines (42 loc) · 1.08 KB
/
task_list.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
package task
import (
"sync"
)
//Store is designed to keep the tasks.
type Store interface {
//GetTasks return the current existing list in store.
GetTasks() []Task
//AddTasks is used to append tasks to the list.
AddTasks(tasks ...Task)
}
//DefaultStore is the default implemetation of Store interface.
type DefaultStore struct {
//To sync the related operations.
*sync.RWMutex
//The space to keep the tasks.
tasks []Task
}
//NewDefaultStore is constructor method for DefaultStore.
func NewDefaultStore() *DefaultStore {
return &DefaultStore{new(sync.RWMutex), []Task{}}
}
//GetTasks implements the same method in Store interface.
func (ds *DefaultStore) GetTasks() []Task {
copyList := []Task{}
ds.RLock()
defer ds.RUnlock()
if ds.tasks != nil && len(ds.tasks) > 0 {
copyList = append(copyList, ds.tasks...)
}
return copyList
}
//AddTasks implements the same method in Store interface.
func (ds *DefaultStore) AddTasks(tasks ...Task) {
//Double confirm.
if ds.tasks == nil {
ds.tasks = []Task{}
}
ds.Lock()
defer ds.Unlock()
ds.tasks = append(ds.tasks, tasks...)
}