2019-02-17 18:12:06 +03:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
2022-08-19 00:34:05 +03:00
|
|
|
"github.com/MichaelMure/git-bug/entities/identity"
|
2020-11-08 21:18:44 +03:00
|
|
|
"github.com/MichaelMure/git-bug/repository"
|
2019-02-17 18:12:06 +03:00
|
|
|
)
|
|
|
|
|
2020-09-16 17:22:02 +03:00
|
|
|
var _ identity.Interface = &IdentityCache{}
|
|
|
|
|
2019-02-18 16:11:37 +03:00
|
|
|
// IdentityCache is a wrapper around an Identity for caching.
|
2019-02-17 18:12:06 +03:00
|
|
|
type IdentityCache struct {
|
|
|
|
*identity.Identity
|
|
|
|
repoCache *RepoCache
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewIdentityCache(repoCache *RepoCache, id *identity.Identity) *IdentityCache {
|
|
|
|
return &IdentityCache{
|
|
|
|
Identity: id,
|
|
|
|
repoCache: repoCache,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-18 16:11:37 +03:00
|
|
|
func (i *IdentityCache) notifyUpdated() error {
|
|
|
|
return i.repoCache.identityUpdated(i.Identity.Id())
|
|
|
|
}
|
|
|
|
|
2020-11-08 21:18:44 +03:00
|
|
|
func (i *IdentityCache) Mutate(repo repository.RepoClock, f func(*identity.Mutator)) error {
|
|
|
|
err := i.Identity.Mutate(repo, f)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-19 01:16:47 +03:00
|
|
|
return i.notifyUpdated()
|
|
|
|
}
|
|
|
|
|
2019-02-17 18:12:06 +03:00
|
|
|
func (i *IdentityCache) Commit() error {
|
2019-02-19 01:16:47 +03:00
|
|
|
err := i.Identity.Commit(i.repoCache.repo)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return i.notifyUpdated()
|
2019-02-17 18:12:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *IdentityCache) CommitAsNeeded() error {
|
2019-02-19 01:16:47 +03:00
|
|
|
err := i.Identity.CommitAsNeeded(i.repoCache.repo)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return i.notifyUpdated()
|
2019-02-17 18:12:06 +03:00
|
|
|
}
|