context passing
This commit is contained in:
+8
-3
@@ -1,6 +1,7 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"context"
|
||||
"image"
|
||||
"sync"
|
||||
|
||||
@@ -21,6 +22,8 @@ type client struct {
|
||||
app fyne.App
|
||||
window fyne.Window
|
||||
|
||||
loadReportFunc func(uint)
|
||||
|
||||
world *world.World
|
||||
drawer *world.GGDrawer
|
||||
raster *canvas.Raster
|
||||
@@ -63,7 +66,7 @@ type client struct {
|
||||
hits []world.Hit
|
||||
}
|
||||
|
||||
func NewClient(conn connector.UIConnector, app fyne.App, settings mc.Settings) (mc.Client, error) {
|
||||
func NewClient(ctx context.Context, conn connector.UIConnector, app fyne.App, settings mc.Settings) (mc.Client, error) {
|
||||
e := &client{
|
||||
conn: conn,
|
||||
app: app,
|
||||
@@ -77,6 +80,8 @@ func NewClient(conn connector.UIConnector, app fyne.App, settings mc.Settings) (
|
||||
hits: make([]world.Hit, 5),
|
||||
}
|
||||
|
||||
e.loadReportFunc = func(t uint) { e.loadReport(ctx, t) }
|
||||
|
||||
e.drawer = &world.GGDrawer{DC: nil}
|
||||
|
||||
e.raster = canvas.NewRaster(func(wPx, hPx int) image.Image {
|
||||
@@ -96,8 +101,8 @@ func NewClient(conn connector.UIConnector, app fyne.App, settings mc.Settings) (
|
||||
return e, nil
|
||||
}
|
||||
|
||||
func (e *client) loadReport(t uint) {
|
||||
e.conn.FetchReport("GAME_ID", t, func(r report.Report, err error) {
|
||||
func (e *client) loadReport(ctx context.Context, t uint) {
|
||||
e.conn.FetchReport(ctx, "GAME_ID", t, func(r report.Report, err error) {
|
||||
if err != nil {
|
||||
e.handlerError(err)
|
||||
} else {
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"galaxy/client"
|
||||
mc "galaxy/model/client"
|
||||
"os"
|
||||
"os/signal"
|
||||
|
||||
"fyne.io/fyne/v2/app"
|
||||
)
|
||||
@@ -27,7 +29,11 @@ func main() {
|
||||
settings := mc.Settings{
|
||||
StoragePath: ".",
|
||||
}
|
||||
c, err := client.NewClient(nil, app, settings)
|
||||
|
||||
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
|
||||
defer cancel()
|
||||
|
||||
c, err := client.NewClient(ctx, nil, app, settings)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package connector
|
||||
|
||||
import (
|
||||
"context"
|
||||
model "galaxy/model/client"
|
||||
"galaxy/model/report"
|
||||
)
|
||||
@@ -25,7 +26,7 @@ type UIConnector interface {
|
||||
// FetchReport asynchronously requests from backend server a [report.Report] for a given [model.GameID] and turn number.
|
||||
// Passed callback func will will accept non-nil error in case of I/O or decoding errors occuried,
|
||||
// otherwise callback func accepts loaded [report.Report].
|
||||
FetchReport(model.GameID, uint, func(report.Report, error))
|
||||
FetchReport(context.Context, model.GameID, uint, func(report.Report, error))
|
||||
}
|
||||
|
||||
type VersionInfo struct {
|
||||
|
||||
+1
-1
@@ -29,7 +29,7 @@ func main() {
|
||||
defer cancel()
|
||||
|
||||
app := app.New()
|
||||
l, err := loader.NewLoader(app, nil)
|
||||
l, err := loader.NewLoader(ctx, app, nil)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
+5
-5
@@ -12,17 +12,17 @@ import (
|
||||
"fyne.io/fyne/v2"
|
||||
)
|
||||
|
||||
type ClientInit func(connector.UIConnector, fyne.App, mc.Settings) (mc.Client, error)
|
||||
type ClientInit func(context.Context, connector.UIConnector, fyne.App, mc.Settings) (mc.Client, error)
|
||||
|
||||
type loader struct {
|
||||
conn connector.Connector
|
||||
cli mc.Client
|
||||
}
|
||||
|
||||
func NewLoader(app fyne.App, conn connector.Connector) (*loader, error) {
|
||||
func NewLoader(ctx context.Context, app fyne.App, conn connector.Connector) (*loader, error) {
|
||||
app.Storage().List()
|
||||
settings := mc.Settings{}
|
||||
cli, err := loadClientPlugin(conn, app, settings, "./client.so", "NewClient")
|
||||
cli, err := loadClientPlugin(ctx, conn, app, settings, "./client.so", "NewClient")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -61,7 +61,7 @@ func (l *loader) backgroundLoop(ctx context.Context, final <-chan struct{}) {
|
||||
|
||||
// loadClientPlugin loads a Client implementation from a shared object (.so) file at the specified path.
|
||||
// It calls the constructor function by name, passing the necessary dependencies, and returns the initialized Client.
|
||||
func loadClientPlugin(conn connector.UIConnector, app fyne.App, s mc.Settings, path, name string) (mc.Client, error) {
|
||||
func loadClientPlugin(ctx context.Context, conn connector.UIConnector, app fyne.App, s mc.Settings, path, name string) (mc.Client, error) {
|
||||
if path == "" {
|
||||
return nil, errors.New("no plugin path given")
|
||||
}
|
||||
@@ -81,5 +81,5 @@ func loadClientPlugin(conn connector.UIConnector, app fyne.App, s mc.Settings, p
|
||||
return nil, fmt.Errorf("unexpected type %T; want %T", sym, initializerPtr)
|
||||
}
|
||||
|
||||
return (*initializerPtr)(conn, app, s)
|
||||
return (*initializerPtr)(ctx, conn, app, s)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user