1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2024-12-01 03:03:48 +00:00
arikawa/utils/handler/slab.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

45 lines
764 B
Go

package handler
type slabEntry struct {
index int
handler
}
func (entry slabEntry) isInvalid() bool {
return entry.index != -1
}
// slab is an implementation of the internal handler free list.
type slab struct {
Entries []slabEntry
free int
}
func (s *slab) Put(entry handler) int {
if s.free == len(s.Entries) {
index := len(s.Entries)
s.Entries = append(s.Entries, slabEntry{-1, entry})
s.free++
return index
}
next := s.Entries[s.free].index
s.Entries[s.free] = slabEntry{-1, entry}
i := s.free
s.free = next
return i
}
func (s *slab) Get(i int) handler {
return s.Entries[i].handler
}
func (s *slab) Pop(i int) handler {
popped := s.Entries[i].handler
s.Entries[i] = slabEntry{s.free, handler{}}
s.free = i
return popped
}