1package cache
2
3import (
4 "sync"
5
6 "github.com/MichaelMure/git-bug/entities/identity"
7 "github.com/MichaelMure/git-bug/entity"
8 "github.com/MichaelMure/git-bug/repository"
9)
10
11var _ entity.Interface = &IdentityCache{}
12var _ CacheEntity = &IdentityCache{}
13
14// IdentityCache is a wrapper around an Identity for caching.
15type IdentityCache struct {
16 repo repository.ClockedRepo
17 entityUpdated func(id entity.Id) error
18
19 mu sync.Mutex
20 *identity.Identity
21}
22
23func NewIdentityCache(i *identity.Identity, repo repository.ClockedRepo, entityUpdated func(id entity.Id) error) *IdentityCache {
24 return &IdentityCache{
25 repo: repo,
26 entityUpdated: entityUpdated,
27 Identity: i,
28 }
29}
30
31func (i *IdentityCache) notifyUpdated() error {
32 return i.entityUpdated(i.Identity.Id())
33}
34
35func (i *IdentityCache) Mutate(repo repository.RepoClock, f func(*identity.Mutator)) error {
36 i.mu.Lock()
37 err := i.Identity.Mutate(repo, f)
38 i.mu.Unlock()
39 if err != nil {
40 return err
41 }
42 return i.notifyUpdated()
43}
44
45func (i *IdentityCache) Commit() error {
46 i.mu.Lock()
47 err := i.Identity.Commit(i.repo)
48 i.mu.Unlock()
49 if err != nil {
50 return err
51 }
52 return i.notifyUpdated()
53}
54
55func (i *IdentityCache) CommitAsNeeded() error {
56 i.mu.Lock()
57 err := i.Identity.CommitAsNeeded(i.repo)
58 i.mu.Unlock()
59 if err != nil {
60 return err
61 }
62 return i.notifyUpdated()
63}
64
65func (i *IdentityCache) Lock() {
66 i.mu.Lock()
67}