mirror of
https://github.com/diamondburned/arikawa.git
synced 2024-11-14 02:42:51 +00:00
efde3f4ea6
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.
101 lines
1.9 KiB
Go
101 lines
1.9 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 Member struct {
|
|
guilds moreatomic.Map // discord.GuildID -> *guildMembers
|
|
}
|
|
|
|
type guildMembers struct {
|
|
mut sync.RWMutex
|
|
members map[discord.UserID]discord.Member
|
|
}
|
|
|
|
var _ store.MemberStore = (*Member)(nil)
|
|
|
|
func NewMember() *Member {
|
|
return &Member{
|
|
guilds: *moreatomic.NewMap(func() interface{} {
|
|
return &guildMembers{
|
|
members: make(map[discord.UserID]discord.Member, 1),
|
|
}
|
|
}),
|
|
}
|
|
}
|
|
|
|
func (s *Member) Reset() error {
|
|
return s.guilds.Reset()
|
|
}
|
|
|
|
func (s *Member) Member(guildID discord.GuildID, userID discord.UserID) (*discord.Member, error) {
|
|
iv, ok := s.guilds.Load(guildID)
|
|
if !ok {
|
|
return nil, store.ErrNotFound
|
|
}
|
|
|
|
gm := iv.(*guildMembers)
|
|
|
|
gm.mut.RLock()
|
|
defer gm.mut.RUnlock()
|
|
|
|
m, ok := gm.members[userID]
|
|
if ok {
|
|
return &m, nil
|
|
}
|
|
|
|
return nil, store.ErrNotFound
|
|
}
|
|
|
|
func (s *Member) Members(guildID discord.GuildID) ([]discord.Member, error) {
|
|
iv, ok := s.guilds.Load(guildID)
|
|
if !ok {
|
|
return nil, store.ErrNotFound
|
|
}
|
|
|
|
gm := iv.(*guildMembers)
|
|
|
|
gm.mut.RLock()
|
|
defer gm.mut.RUnlock()
|
|
|
|
var members = make([]discord.Member, 0, len(gm.members))
|
|
for _, m := range gm.members {
|
|
members = append(members, m)
|
|
}
|
|
|
|
return members, nil
|
|
}
|
|
|
|
func (s *Member) MemberSet(guildID discord.GuildID, m *discord.Member, update bool) error {
|
|
iv, _ := s.guilds.LoadOrStore(guildID)
|
|
gm := iv.(*guildMembers)
|
|
|
|
gm.mut.Lock()
|
|
if _, ok := gm.members[m.User.ID]; !ok || update {
|
|
gm.members[m.User.ID] = *m
|
|
}
|
|
gm.mut.Unlock()
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *Member) MemberRemove(guildID discord.GuildID, userID discord.UserID) error {
|
|
iv, ok := s.guilds.Load(guildID)
|
|
if !ok {
|
|
return nil
|
|
}
|
|
|
|
gm := iv.(*guildMembers)
|
|
|
|
gm.mut.Lock()
|
|
delete(gm.members, userID)
|
|
gm.mut.Unlock()
|
|
|
|
return nil
|
|
}
|