diff --git a/gateway/events_map.go b/gateway/events_map.go index 9060ed3a..a71f698e 100644 --- a/gateway/events_map.go +++ b/gateway/events_map.go @@ -57,5 +57,5 @@ var EventCreator = map[string]func() Event{ "VOICE_STATE_UPDATE": func() Event { return new(VoiceStateUpdateEvent) }, "VOICE_SERVER_UPDATE": func() Event { return new(VoiceServerUpdateEvent) }, - "WEBHOOKS_UDPATE": func() Event { return new(WebhooksUpdateEvent) }, + "WEBHOOKS_UPDATE": func() Event { return new(WebhooksUpdateEvent) }, } diff --git a/go.mod b/go.mod index 1ca46d2c..b613640b 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/diamondburned/arikawa go 1.13 require ( + github.com/davecgh/go-spew v1.1.1 github.com/gorilla/schema v1.1.0 github.com/pkg/errors v0.8.1 github.com/sasha-s/go-csync v0.0.0-20160729053059-3bc6c8bdb3fa diff --git a/handler/handler.go b/handler/handler.go index a871f1fe..29004b05 100644 --- a/handler/handler.go +++ b/handler/handler.go @@ -38,6 +38,7 @@ type Handler struct { Synchronous bool handlers map[uint64]handler + horders []uint64 hserial uint64 hmutex sync.Mutex } @@ -55,7 +56,13 @@ func (h *Handler) Call(ev interface{}) { h.hmutex.Lock() defer h.hmutex.Unlock() - for _, handler := range h.handlers { + for _, order := range h.horders { + handler, ok := h.handlers[order] + if !ok { + // This shouldn't ever happen, but we're adding this just in case. + continue + } + if handler.not(evT) { continue } @@ -123,18 +130,30 @@ func (h *Handler) addHandler(handler interface{}) (rm func(), err error) { h.hmutex.Lock() defer h.hmutex.Unlock() - // Get the current counter value and increment the counter + // Get the current counter value and increment the counter: serial := h.hserial h.hserial++ - // Use the serial for the map + // Use the serial for the map: h.handlers[serial] = *r + // Append the serial into the list of keys: + h.horders = append(h.horders, serial) + return func() { h.hmutex.Lock() defer h.hmutex.Unlock() + // Delete the handler from the map: delete(h.handlers, serial) + + // Delete the key from the orders slice: + for i, order := range h.horders { + if order == serial { + h.horders = append(h.horders[:i], h.horders[i+1:]...) + break + } + } }, nil } diff --git a/state/state.go b/state/state.go index 1ad05837..5eb64d1a 100644 --- a/state/state.go +++ b/state/state.go @@ -57,9 +57,7 @@ func NewFromSession(s *session.Session, store Store) (*State, error) { } func New(token string) (*State, error) { - return NewWithStore(token, NewDefaultStore(&DefaultStoreOptions{ - MaxMessages: 50, - })) + return NewWithStore(token, NewDefaultStore(nil)) } func NewWithStore(token string, store Store) (*State, error) { diff --git a/state/store_default.go b/state/store_default.go index 27430b91..7d96ee6f 100644 --- a/state/store_default.go +++ b/state/store_default.go @@ -39,17 +39,12 @@ func NewDefaultStore(opts *DefaultStoreOptions) *DefaultStore { } } - return &DefaultStore{ + ds := &DefaultStore{ DefaultStoreOptions: opts, - - privates: map[discord.Snowflake]*discord.Channel{}, - guilds: map[discord.Snowflake]*discord.Guild{}, - - channels: map[discord.Snowflake][]discord.Channel{}, - members: map[discord.Snowflake][]discord.Member{}, - presences: map[discord.Snowflake][]discord.Presence{}, - messages: map[discord.Snowflake][]discord.Message{}, } + ds.Reset() + + return ds } func (s *DefaultStore) Reset() error { @@ -60,6 +55,10 @@ func (s *DefaultStore) Reset() error { s.privates = map[discord.Snowflake]*discord.Channel{} s.guilds = map[discord.Snowflake]*discord.Guild{} + + s.channels = map[discord.Snowflake][]discord.Channel{} + s.members = map[discord.Snowflake][]discord.Member{} + s.presences = map[discord.Snowflake][]discord.Presence{} s.messages = map[discord.Snowflake][]discord.Message{} return nil