arikawa icon indicating copy to clipboard operation
arikawa copied to clipboard

Callback function for every event before it is json decoded

Open hi117 opened this issue 3 years ago • 1 comments

I'm wanting to migrate from discordgo to arikawa, and one of the features it had was hooking on something like this: func(_ *discordgo.Session, m *discordgo.Event). Is there an equilavent in Arikawa? I tried looking but there didn't seem to be a way to hook on every event before it was decoded. This is how discordgo handled the struct by the way:

type Event struct {
	Operation int             `json:"op"`
	Sequence  int64           `json:"s"`
	Type      string          `json:"t"`
	RawData   json.RawMessage `json:"d"`
	// Struct contains one of the other types in this file.
	Struct interface{} `json:"-"`
}

hi117 avatar Feb 06 '22 15:02 hi117

There is no straightforward way to hook on every event before it's decoded, but there are some roundabout ways to achieve this:

  • You can probably swap out gateway.OpUnmarshalers with something that is a copy of the existing OpUnmarshalers with all functions wrapped, or
  • You can create a wrapper around ws.Conn which will give you control over the <-chan ws.Op channel.

It might be worth it to also route every single ws.Op value through the handler, but that will introduce a lot more work for a case that not a lot of people will care about:

--- a/utils/ws/ophandler/ophandler.go
+++ b/utils/ws/ophandler/ophandler.go
@@ -17,6 +17,7 @@ func Loop(src <-chan ws.Op, dst *handler.Handler) <-chan struct{} {
        go func() {
                for op := range src {
                        dst.Call(op.Data)
+                       dst.Call(op)
                }
                close(done)
        }()

diamondburned avatar Feb 07 '22 03:02 diamondburned