fix(remote-control): re-check idle state inside delayed sync callback

This commit is contained in:
Yejun Su 2026-03-20 18:51:11 +08:00
parent aacabde7dc
commit b77c2a57b0
No known key found for this signature in database
GPG Key ID: AD03A563F321CA44
1 changed files with 9 additions and 0 deletions

View File

@ -30,11 +30,16 @@ const QRCode = _require("qrcode") as { toString: (text: string, opts: any) => Pr
export default function remoteControl(pi: ExtensionAPI) {
let server: RemoteServer | undefined;
let pendingSyncTimer: ReturnType<typeof setTimeout> | undefined;
let needsSyncOnIdle = false;
function scheduleSync(ctx: ExtensionContext): void {
if (pendingSyncTimer) clearTimeout(pendingSyncTimer);
pendingSyncTimer = setTimeout(() => {
pendingSyncTimer = undefined;
if (!ctx.isIdle()) {
needsSyncOnIdle = true;
return;
}
server?.sync(ctx);
updateStatus(ctx);
}, 0);
@ -113,6 +118,10 @@ export default function remoteControl(pi: ExtensionAPI) {
pi.on("agent_end", async (_event, ctx) => {
server?.broadcast({ type: "agent_end" });
if (needsSyncOnIdle) {
needsSyncOnIdle = false;
server?.sync(ctx);
}
updateStatus(ctx);
});