refactor: new func openSession creates and opens a new session

This commit is contained in:
ayn2op 2023-01-21 12:01:27 +05:30
parent 8920daf0d0
commit bd2720daae
3 changed files with 10 additions and 8 deletions

View file

@ -63,8 +63,7 @@ func (lf *LoginForm) onLoginButtonSelected() {
}
// We got the token, return with a new Session.
discordState = newState(l.Token)
err = discordState.Open(context.Background())
discordState, err = openState(l.Token)
if err != nil {
log.Fatal(err)
}

View file

@ -1,7 +1,6 @@
package main
import (
"context"
"flag"
"log"
"os"
@ -77,8 +76,7 @@ func main() {
if token == "" {
app.SetRoot(newLoginForm(), true)
} else {
discordState = newState(token)
err = discordState.Open(context.Background())
discordState, err = openState(token)
if err != nil {
log.Fatal(err)
}

View file

@ -1,6 +1,7 @@
package main
import (
"context"
"fmt"
"log"
"runtime"
@ -26,18 +27,22 @@ type State struct {
*state.State
}
func newState(token string) *State {
func openState(token string) (*State, error) {
s := &State{
State: state.New(token),
}
// Handlers
s.AddHandler(s.onReady)
s.AddHandler(s.onMessageCreate)
s.StateLog = s.onLog
s.OnRequest = append(s.Client.OnRequest, s.onRequest)
return s
if err := s.Open(context.TODO()); err != nil {
return nil, err
}
return s, nil
}
func (s *State) onLog(err error) {