1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2024-11-08 07:54:58 +00:00
arikawa/session/session.go

84 lines
1.5 KiB
Go
Raw Normal View History

2020-01-15 04:43:34 +00:00
package session
import (
"log"
"sync"
"time"
"github.com/diamondburned/arikawa/api"
"github.com/diamondburned/arikawa/gateway"
"github.com/diamondburned/arikawa/json"
)
/*
TODO:
and Session's supposed to handle callbacks too kec
might move all these to Gateway, dunno
could have a lock on Listen()
I can actually see people using gateway channels to handle things
themselves without any callback abstractions, so this is probably the way to go
welp shit
rewrite imminent
*/
type Session struct {
API *api.Client
Gateway *gateway.Conn
gatewayOnce sync.Once
ErrorLog func(err error) // default to log.Println
// Heartrate is the received duration between heartbeats.
Heartrate time.Duration
// LastBeat logs the received heartbeats, with the newest one
// first.
LastBeat [2]time.Time
// Used for Close()
stoppers []chan<- struct{}
closers []func() error
}
func New(token string) (*Session, error) {
// Initialize the session and the API interface
s := &Session{}
s.API = api.NewClient(token)
// Default logger
s.ErrorLog = func(err error) {
log.Println("Arikawa/session error:", err)
}
// Connect to the Gateway
c, err := gateway.NewConn(json.Default{})
if err != nil {
return nil, err
}
s.Gateway = c
return s, nil
}
func (s *Session) Close() error {
for _, stop := range s.stoppers {
close(stop)
}
var err error
for _, closer := range s.closers {
if cerr := closer(); cerr != nil {
err = cerr
}
}
return err
}