context passing
This commit is contained in:
+8
-3
@@ -1,6 +1,7 @@
|
|||||||
package client
|
package client
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"image"
|
"image"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
@@ -21,6 +22,8 @@ type client struct {
|
|||||||
app fyne.App
|
app fyne.App
|
||||||
window fyne.Window
|
window fyne.Window
|
||||||
|
|
||||||
|
loadReportFunc func(uint)
|
||||||
|
|
||||||
world *world.World
|
world *world.World
|
||||||
drawer *world.GGDrawer
|
drawer *world.GGDrawer
|
||||||
raster *canvas.Raster
|
raster *canvas.Raster
|
||||||
@@ -63,7 +66,7 @@ type client struct {
|
|||||||
hits []world.Hit
|
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{
|
e := &client{
|
||||||
conn: conn,
|
conn: conn,
|
||||||
app: app,
|
app: app,
|
||||||
@@ -77,6 +80,8 @@ func NewClient(conn connector.UIConnector, app fyne.App, settings mc.Settings) (
|
|||||||
hits: make([]world.Hit, 5),
|
hits: make([]world.Hit, 5),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
e.loadReportFunc = func(t uint) { e.loadReport(ctx, t) }
|
||||||
|
|
||||||
e.drawer = &world.GGDrawer{DC: nil}
|
e.drawer = &world.GGDrawer{DC: nil}
|
||||||
|
|
||||||
e.raster = canvas.NewRaster(func(wPx, hPx int) image.Image {
|
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
|
return e, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *client) loadReport(t uint) {
|
func (e *client) loadReport(ctx context.Context, t uint) {
|
||||||
e.conn.FetchReport("GAME_ID", t, func(r report.Report, err error) {
|
e.conn.FetchReport(ctx, "GAME_ID", t, func(r report.Report, err error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
e.handlerError(err)
|
e.handlerError(err)
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -1,11 +1,13 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"galaxy/client"
|
"galaxy/client"
|
||||||
mc "galaxy/model/client"
|
mc "galaxy/model/client"
|
||||||
"os"
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
|
||||||
"fyne.io/fyne/v2/app"
|
"fyne.io/fyne/v2/app"
|
||||||
)
|
)
|
||||||
@@ -27,7 +29,11 @@ func main() {
|
|||||||
settings := mc.Settings{
|
settings := mc.Settings{
|
||||||
StoragePath: ".",
|
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 {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package connector
|
package connector
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
model "galaxy/model/client"
|
model "galaxy/model/client"
|
||||||
"galaxy/model/report"
|
"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.
|
// 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,
|
// 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].
|
// 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 {
|
type VersionInfo struct {
|
||||||
|
|||||||
+1
-1
@@ -29,7 +29,7 @@ func main() {
|
|||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
app := app.New()
|
app := app.New()
|
||||||
l, err := loader.NewLoader(app, nil)
|
l, err := loader.NewLoader(ctx, app, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
+5
-5
@@ -12,17 +12,17 @@ import (
|
|||||||
"fyne.io/fyne/v2"
|
"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 {
|
type loader struct {
|
||||||
conn connector.Connector
|
conn connector.Connector
|
||||||
cli mc.Client
|
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()
|
app.Storage().List()
|
||||||
settings := mc.Settings{}
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
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.
|
// 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.
|
// 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 == "" {
|
if path == "" {
|
||||||
return nil, errors.New("no plugin path given")
|
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 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