Procházet zdrojové kódy

refactor: new func openSession creates and opens a new session

ayn2op před 3 roky
rodič
revize
bd2720daae
3 změnil soubory, kde provedl 10 přidání a 8 odebrání
  1. 1 2
      login_form.go
  2. 1 3
      main.go
  3. 8 3
      state.go

+ 1 - 2
login_form.go

@@ -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)
 	}

+ 1 - 3
main.go

@@ -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)
 		}

+ 8 - 3
state.go

@@ -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) {