1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2024-11-19 05:12:48 +00:00
arikawa/state/store/defaultstore/presence.go
diamondburned efde3f4ea6
state, handler: Refactor state storage and sync handlers
This commit refactors a lot of packages.

It refactors the handler package, removing the Synchronous field and
replacing it the AddSyncHandler API, which allows each handler to
control whether or not it should be ran synchronously independent of
other handlers. This is useful for libraries that need to guarantee the
incoming order of events.

It also refactors the store interfaces to accept more interfaces. This
is to make the API more consistent as well as reducing potential useless
copies. The public-facing state API should still be the same, so this
change will mostly concern users with their own store implementations.

Several miscellaneous functions (such as a few in package gateway) were
modified to be more suitable to other packages, but those functions
should rarely ever be used, anyway.

Several tests are also fixed within this commit, namely fixing state's
intents bug.
2021-11-03 15:16:02 -07:00

108 lines
2 KiB
Go

package defaultstore
import (
"sync"
"github.com/diamondburned/arikawa/v3/discord"
"github.com/diamondburned/arikawa/v3/internal/moreatomic"
"github.com/diamondburned/arikawa/v3/state/store"
)
type Presence struct {
guilds moreatomic.Map
}
type presences struct {
mut sync.RWMutex
presences map[discord.UserID]discord.Presence
}
var _ store.PresenceStore = (*Presence)(nil)
func NewPresence() *Presence {
return &Presence{
guilds: *moreatomic.NewMap(func() interface{} {
return &presences{
presences: make(map[discord.UserID]discord.Presence, 1),
}
}),
}
}
func (s *Presence) Reset() error {
return s.guilds.Reset()
}
func (s *Presence) Presence(gID discord.GuildID, uID discord.UserID) (*discord.Presence, error) {
iv, ok := s.guilds.Load(gID)
if !ok {
return nil, store.ErrNotFound
}
ps := iv.(*presences)
ps.mut.RLock()
defer ps.mut.RUnlock()
p, ok := ps.presences[uID]
if ok {
return &p, nil
}
return nil, store.ErrNotFound
}
func (s *Presence) Presences(guildID discord.GuildID) ([]discord.Presence, error) {
iv, ok := s.guilds.Load(guildID)
if !ok {
return nil, store.ErrNotFound
}
ps := iv.(*presences)
ps.mut.RLock()
defer ps.mut.RUnlock()
var presences = make([]discord.Presence, 0, len(ps.presences))
for _, p := range ps.presences {
presences = append(presences, p)
}
return presences, nil
}
func (s *Presence) PresenceSet(guildID discord.GuildID, p *discord.Presence, update bool) error {
iv, _ := s.guilds.LoadOrStore(guildID)
ps := iv.(*presences)
ps.mut.Lock()
defer ps.mut.Unlock()
// Shitty if check is better than a realloc every time.
if ps.presences == nil {
ps.presences = make(map[discord.UserID]discord.Presence, 1)
}
if _, ok := ps.presences[p.User.ID]; !ok || update {
ps.presences[p.User.ID] = *p
}
return nil
}
func (s *Presence) PresenceRemove(guildID discord.GuildID, userID discord.UserID) error {
iv, ok := s.guilds.Load(guildID)
if !ok {
return nil
}
ps := iv.(*presences)
ps.mut.Lock()
delete(ps.presences, userID)
ps.mut.Unlock()
return nil
}