Skip to content

Commit ae2306e

Browse files
committed
restore generator state when navigating back
1 parent 53f4f93 commit ae2306e

File tree

7 files changed

+66
-20
lines changed

7 files changed

+66
-20
lines changed

frontend/src/components/app-sidebar.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import {
1313
SettingsIcon,
1414
SparkleIcon,
1515
Sun,
16-
SwordIcon,
1716
TrainIcon,
1817
TrashIcon,
1918
WavesIcon,

frontend/src/screens/GameScreen.tsx

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { syncCharacters, SyncType } from "../data/sync";
44
import { cn, useStateProducer } from "../lib/utils";
55
import { type Pair } from "@/lib/tsutils";
66
import { types } from "wailsjs/go/models";
7-
import { Reload } from "../../wailsjs/go/core/Generator";
7+
import * as Generator from "../../wailsjs/go/core/Generator";
88
import * as Downloader from "../../wailsjs/go/core/Downloader";
99
import {
1010
DisableAllModsByGame,
@@ -79,13 +79,8 @@ function GameScreen(props: { dataApi: DataApi; game: number }) {
7979
useMemo(() => getElementPref(props.game), [props.game])
8080
);
8181
const running = useDownloadStore(useShallow((state) => state.running));
82-
8382
const refreshCharacters = () => setRefreshTrigger((prev) => prev + 1);
8483

85-
useEffect(() => {
86-
refreshCharacters();
87-
}, [running, updates]);
88-
8984
const elements = useStateProducer<string[]>(
9085
[],
9186
async (update) => {
@@ -99,7 +94,7 @@ function GameScreen(props: { dataApi: DataApi; game: number }) {
9994
async (update) => {
10095
update(await props.dataApi.charactersWithModsAndTags());
10196
},
102-
[props.dataApi, refreshTrigger]
97+
[props.dataApi, running, updates, refreshTrigger]
10398
);
10499

105100
const filteredCharacters = useMemo(() => {
@@ -323,11 +318,25 @@ function OverlayOptions({
323318
}: {
324319
dataApi: DataApi;
325320
dialog: GameDialog | undefined;
326-
setDialog: (dialgo: GameDialog | undefined) => void;
321+
setDialog: (dialog: GameDialog | undefined) => void;
327322
refreshCharacters: () => void;
328323
}) {
329324
const [reloading, setReloading] = useState(false);
330325
const [syncing, setSyncing] = useState(false);
326+
// lazy way to restore the state of generating job TODO: maybe use events
327+
useEffect(() => {
328+
dataApi.game().then((game) => {
329+
Generator.IsRunning(game)
330+
.then((reloading) => {
331+
setReloading(reloading)
332+
if (reloading) {
333+
Generator
334+
.AwaitCurrentJob(game)
335+
.finally(() => setReloading(false))
336+
}
337+
})
338+
})
339+
}, [])
331340

332341
const sync = (type: SyncType) => {
333342
setSyncing(true);
@@ -338,7 +347,7 @@ function OverlayOptions({
338347

339348
const reload = async () => {
340349
setReloading(true);
341-
Reload(await dataApi.game())
350+
Generator.Reload(await dataApi.game())
342351
.finally(() => setReloading(false))
343352
.catch();
344353
};

frontend/wailsjs/go/core/Generator.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,8 @@
22
// This file is automatically generated. DO NOT EDIT
33
import {types} from '../models';
44

5+
export function AwaitCurrentJob(arg1:types.Game):Promise<void>;
6+
7+
export function IsRunning(arg1:types.Game):Promise<boolean>;
8+
59
export function Reload(arg1:types.Game):Promise<void>;

frontend/wailsjs/go/core/Generator.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22
// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL
33
// This file is automatically generated. DO NOT EDIT
44

5+
export function AwaitCurrentJob(arg1) {
6+
return window['go']['core']['Generator']['AwaitCurrentJob'](arg1);
7+
}
8+
9+
export function IsRunning(arg1) {
10+
return window['go']['core']['Generator']['IsRunning'](arg1);
11+
}
12+
513
export function Reload(arg1) {
614
return window['go']['core']['Generator']['Reload'](arg1);
715
}

pkg/api/dapi.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func CleanCache() {
5959

6060
log.LogDebug("keeping in httpcache: " + info.Name() + " " + info.ModTime().String())
6161
remaining -= info.Size()
62-
cf = append(cf, types.Pair[string, time.Time]{X: filePath, Y: info.ModTime()})
62+
cf = append(cf, types.PairOf(filePath, info.ModTime()))
6363

6464
return nil
6565
})

pkg/core/generator.go

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
type Generator struct {
2424
db *DbHelper
2525
poolSize int
26-
cancelFns map[types.Game]context.CancelFunc
26+
cancelFns map[types.Game]types.Pair[context.Context, context.CancelFunc]
2727
wgMap map[types.Game]*sync.WaitGroup
2828
mutexMap map[types.Game]*sync.Mutex
2929
outputDirs map[types.Game]pref.Preference[string]
@@ -41,11 +41,11 @@ func NewGenerator(
4141
return &Generator{
4242
db: db,
4343
poolSize: 4,
44-
cancelFns: map[types.Game]context.CancelFunc{
45-
types.Genshin: func() {},
46-
types.StarRail: func() {},
47-
types.ZZZ: func() {},
48-
types.WuWa: func() {},
44+
cancelFns: map[types.Game]types.Pair[context.Context, context.CancelFunc]{
45+
types.Genshin: {},
46+
types.StarRail: {},
47+
types.ZZZ: {},
48+
types.WuWa: {},
4949
},
5050
wgMap: map[types.Game]*sync.WaitGroup{
5151
types.Genshin: {},
@@ -76,6 +76,22 @@ func areModsSame(parts []string) func(m types.Mod) bool {
7676
}
7777
}
7878

79+
func (g *Generator) AwaitCurrentJob(game types.Game) {
80+
wg, ok := g.wgMap[game]
81+
if !ok {
82+
return
83+
}
84+
wg.Wait()
85+
}
86+
87+
func (g *Generator) IsRunning(game types.Game) bool {
88+
ctx, cancel := g.cancelFns[game].Pair()
89+
if ctx == nil || cancel == nil {
90+
return false
91+
}
92+
return ctx.Err() == nil
93+
}
94+
7995
// unzips mod folders into export dir for given game deleting any
8096
// previous mod files
8197
func (g *Generator) Reload(game types.Game) error {
@@ -89,14 +105,16 @@ func (g *Generator) Reload(game types.Game) error {
89105
g.mutexMap[game].Lock()
90106
defer g.mutexMap[game].Unlock()
91107

92-
if g.cancelFns[game] != nil {
93-
g.cancelFns[game]()
108+
_, cancel := g.cancelFns[game].Pair()
109+
110+
if cancel != nil {
111+
cancel()
94112
g.wgMap[game].Wait()
95113
}
96114

97115
ctx, cancel := context.WithCancel(context.Background())
98116

99-
g.cancelFns[game] = cancel
117+
g.cancelFns[game] = types.PairOf(ctx, cancel)
100118
errCh := make(chan error, 1)
101119

102120
g.wgMap[game].Add(1)

pkg/types/types.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ type Pair[X any, Y any] struct {
2121
Y Y
2222
}
2323

24+
func PairOf[X any, Y any](x X, y Y) Pair[X, Y] {
25+
return Pair[X, Y]{X: x, Y: y}
26+
}
27+
28+
func (p Pair[X, Y]) Pair() (X, Y) {
29+
return p.X, p.Y
30+
}
31+
2432
func (g Game) Int64() int64 {
2533
switch g {
2634
case Genshin:

0 commit comments

Comments
 (0)