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