Skip to content

Commit 5b7257c

Browse files
authored
fix: handle power monitor events to manage scheduler lifecycle (#207)
* fix: handle power monitor events to manage scheduler lifecycle * Build plugin
1 parent ba36ca0 commit 5b7257c

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

resources/js/electron-plugin/dist/index.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
77
step((generator = generator.apply(thisArg, _arguments || [])).next());
88
});
99
};
10-
import { app, session } from "electron";
10+
import { app, session, powerMonitor } from "electron";
1111
import { initialize } from "@electron/remote/main/index.js";
1212
import state from "./server/state.js";
1313
import { electronApp, optimizer } from "@electron-toolkit/utils";
@@ -80,6 +80,13 @@ class NativePHP {
8080
state.phpIni = yield this.loadPhpIni();
8181
yield this.startPhpApp();
8282
this.startScheduler();
83+
powerMonitor.on("suspend", () => {
84+
this.stopScheduler();
85+
});
86+
powerMonitor.on("resume", () => {
87+
this.stopScheduler();
88+
this.startScheduler();
89+
});
8390
const filter = {
8491
urls: [`http://127.0.0.1:${state.phpPort}/*`]
8592
};
@@ -181,6 +188,12 @@ class NativePHP {
181188
this.processes.push(yield startPhpApp());
182189
});
183190
}
191+
stopScheduler() {
192+
if (this.schedulerInterval) {
193+
clearInterval(this.schedulerInterval);
194+
this.schedulerInterval = null;
195+
}
196+
}
184197
startScheduler() {
185198
const now = new Date();
186199
const delay = (60 - now.getSeconds()) * 1000 + (1000 - now.getMilliseconds());

resources/js/electron-plugin/src/index.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import type CrossProcessExports from "electron";
2-
import { app, session } from "electron";
1+
import CrossProcessExports from "electron";
2+
import { app, session, powerMonitor } from "electron";
33
import { initialize } from "@electron/remote/main/index.js";
44
import state from "./server/state.js";
55
import { electronApp, optimizer } from "@electron-toolkit/utils";
@@ -109,6 +109,15 @@ class NativePHP {
109109
await this.startPhpApp();
110110
this.startScheduler();
111111

112+
powerMonitor.on("suspend", () => {
113+
this.stopScheduler();
114+
});
115+
116+
powerMonitor.on("resume", () => {
117+
this.stopScheduler();
118+
this.startScheduler();
119+
});
120+
112121
const filter = {
113122
urls: [`http://127.0.0.1:${state.phpPort}/*`]
114123
};
@@ -235,6 +244,14 @@ class NativePHP {
235244
this.processes.push(await startPhpApp());
236245
}
237246

247+
248+
private stopScheduler() {
249+
if (this.schedulerInterval) {
250+
clearInterval(this.schedulerInterval);
251+
this.schedulerInterval = null;
252+
}
253+
}
254+
238255
private startScheduler() {
239256
const now = new Date();
240257
const delay =

0 commit comments

Comments
 (0)