Skip to content

Commit

Permalink
Fixed Webhook Update event
Browse files Browse the repository at this point in the history
  • Loading branch information
diamondburned committed Jan 25, 2020
1 parent b45c048 commit ea9f2c2
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 16 deletions.
2 changes: 1 addition & 1 deletion gateway/events_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) },
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 22 additions & 3 deletions handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type Handler struct {
Synchronous bool

handlers map[uint64]handler
horders []uint64
hserial uint64
hmutex sync.Mutex
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}

Expand Down
4 changes: 1 addition & 3 deletions state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
17 changes: 8 additions & 9 deletions state/store_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down

0 comments on commit ea9f2c2

Please sign in to comment.