From 1822724df2e13fdf6616e64145ad5e83b5ade374 Mon Sep 17 00:00:00 2001 From: Zamitto Date: Tue, 23 Apr 2024 21:10:15 -0300 Subject: [PATCH] feat: use psList on windows (~25ms vs ~320ms) --- src/main/helpers/ps.ts | 7 ------- src/main/services/process-watcher.ts | 17 +++++++++-------- yarn.lock | 2 +- 3 files changed, 10 insertions(+), 16 deletions(-) diff --git a/src/main/helpers/ps.ts b/src/main/helpers/ps.ts index f7ce3391..5a30a9ed 100644 --- a/src/main/helpers/ps.ts +++ b/src/main/helpers/ps.ts @@ -1,12 +1,5 @@ import psList from "ps-list"; -import { tasklist } from "tasklist"; export const getProcesses = async () => { - if (process.platform === "win32") { - return tasklist().then((tasks) => - tasks.map((task) => ({ ...task, name: task.imageName })) - ); - } - return psList(); }; diff --git a/src/main/services/process-watcher.ts b/src/main/services/process-watcher.ts index 96244a60..c67f6815 100644 --- a/src/main/services/process-watcher.ts +++ b/src/main/services/process-watcher.ts @@ -1,7 +1,7 @@ import path from "node:path"; import { IsNull, Not } from "typeorm"; - +import { exec } from "child_process" import { gameRepository } from "@main/repository"; import { getProcesses } from "@main/helpers"; import { WindowManager } from "./window-manager"; @@ -14,13 +14,21 @@ export const startProcessWatcher = async () => { // eslint-disable-next-line no-constant-condition while (true) { + await sleep(sleepTime); + const games = await gameRepository.find({ where: { executablePath: Not(IsNull()), }, }); + if (games.length == 0) { + continue; + } + + console.time("getProcesses") const processes = await getProcesses(); + console.timeEnd("getProcesses") for (const game of games) { const gameProcess = processes.find((runningProcess) => { @@ -55,15 +63,10 @@ export const startProcessWatcher = async () => { gameRepository.update(game.id, { lastTimePlayed: new Date().toUTCString(), }); - - gamesPlaytime.set(game.id, performance.now()); - await sleep(sleepTime); - continue; } gamesPlaytime.set(game.id, performance.now()); - await sleep(sleepTime); continue; } @@ -73,8 +76,6 @@ export const startProcessWatcher = async () => { WindowManager.mainWindow.webContents.send("on-game-close", game.id); } } - - await sleep(sleepTime); } } }; diff --git a/yarn.lock b/yarn.lock index 8b36bcdb..c6450c09 100644 --- a/yarn.lock +++ b/yarn.lock @@ -8866,7 +8866,7 @@ proxy-from-env@^1.1.0: ps-list@^8.1.1: version "8.1.1" - resolved "https://registry.npmjs.org/ps-list/-/ps-list-8.1.1.tgz" + resolved "https://registry.yarnpkg.com/ps-list/-/ps-list-8.1.1.tgz#9ff1952b26a9a07fcc05270407e60544237ae581" integrity sha512-OPS9kEJYVmiO48u/B9qneqhkMvgCxT+Tm28VCEJpheTpl8cJ0ffZRRNgS5mrQRTrX5yRTpaJ+hRDeefXYmmorQ== psl@^1.1.33: