summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/utils/nowPlaying.ts23
-rw-r--r--tests/nowPlaying.test.ts10
2 files changed, 28 insertions, 5 deletions
diff --git a/src/utils/nowPlaying.ts b/src/utils/nowPlaying.ts
index 2e0a876..380c9f5 100644
--- a/src/utils/nowPlaying.ts
+++ b/src/utils/nowPlaying.ts
@@ -20,11 +20,26 @@ function numericPid(value: string | undefined): number {
return value && /^\d+$/.test(value) ? Number(value) : -1;
}
+function compareRunningProcesses(a: RunningFlatpakApp, b: RunningFlatpakApp): number {
+ if (a.active !== b.active) return a.active ? -1 : 1;
+ const startDifference = numericValue(b.start_time) - numericValue(a.start_time);
+ if (startDifference !== 0) return startDifference;
+ return numericPid(b.pid) - numericPid(a.pid);
+}
+
export function selectMostRecentRunningFlatpak(
apps: FlatpakApp[],
runningApps: RunningFlatpakApp[],
): FlatpakApp | null {
- const candidates = runningApps
+ const newestProcessByApp = new Map<string, RunningFlatpakApp>();
+ for (const running of runningApps) {
+ const current = newestProcessByApp.get(running.app_id);
+ if (!current || compareRunningProcesses(running, current) < 0) {
+ newestProcessByApp.set(running.app_id, running);
+ }
+ }
+
+ const candidates = Array.from(newestProcessByApp.values())
.map((running) => ({
running,
app: apps.find((app) => app.app_id === running.app_id) || null,
@@ -38,10 +53,8 @@ export function selectMostRecentRunningFlatpak(
: [];
eligibleCandidates.sort((a, b) => {
- const startDifference = numericValue(b.running.start_time) - numericValue(a.running.start_time);
- if (startDifference !== 0) return startDifference;
- const pidDifference = numericPid(b.running.pid) - numericPid(a.running.pid);
- if (pidDifference !== 0) return pidDifference;
+ const processDifference = compareRunningProcesses(a.running, b.running);
+ if (processDifference !== 0) return processDifference;
return a.running.app_id.localeCompare(b.running.app_id);
});
diff --git a/tests/nowPlaying.test.ts b/tests/nowPlaying.test.ts
index 8a52b67..c99ffc4 100644
--- a/tests/nowPlaying.test.ts
+++ b/tests/nowPlaying.test.ts
@@ -47,6 +47,16 @@ test("prefers active Flatpak status before process age", () => {
assert.equal(selectMostRecentRunningFlatpak(apps, running)?.app_id, "org.example.active");
});
+test("deduplicates multiple process rows for one managed Flatpak", () => {
+ const apps = [flatpak("com.heroicgameslauncher.hgl")];
+ const running = [
+ { app_id: "com.heroicgameslauncher.hgl", active: false, pid: "228081", start_time: null },
+ { app_id: "com.heroicgameslauncher.hgl", active: false, pid: "228116", start_time: null },
+ ];
+
+ assert.equal(selectMostRecentRunningFlatpak(apps, running)?.app_id, "com.heroicgameslauncher.hgl");
+});
+
test("Flatpak runtime wins while a Steam shortcut is running", () => {
const target = resolveNowPlayingTarget(game(true), flatpak("org.libretro.RetroArch", "RetroArch"));