fix(remote-control): re-check idle state inside delayed sync callback
This commit is contained in:
parent
aacabde7dc
commit
b77c2a57b0
|
|
@ -30,11 +30,16 @@ const QRCode = _require("qrcode") as { toString: (text: string, opts: any) => Pr
|
||||||
export default function remoteControl(pi: ExtensionAPI) {
|
export default function remoteControl(pi: ExtensionAPI) {
|
||||||
let server: RemoteServer | undefined;
|
let server: RemoteServer | undefined;
|
||||||
let pendingSyncTimer: ReturnType<typeof setTimeout> | undefined;
|
let pendingSyncTimer: ReturnType<typeof setTimeout> | undefined;
|
||||||
|
let needsSyncOnIdle = false;
|
||||||
|
|
||||||
function scheduleSync(ctx: ExtensionContext): void {
|
function scheduleSync(ctx: ExtensionContext): void {
|
||||||
if (pendingSyncTimer) clearTimeout(pendingSyncTimer);
|
if (pendingSyncTimer) clearTimeout(pendingSyncTimer);
|
||||||
pendingSyncTimer = setTimeout(() => {
|
pendingSyncTimer = setTimeout(() => {
|
||||||
pendingSyncTimer = undefined;
|
pendingSyncTimer = undefined;
|
||||||
|
if (!ctx.isIdle()) {
|
||||||
|
needsSyncOnIdle = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
server?.sync(ctx);
|
server?.sync(ctx);
|
||||||
updateStatus(ctx);
|
updateStatus(ctx);
|
||||||
}, 0);
|
}, 0);
|
||||||
|
|
@ -113,6 +118,10 @@ export default function remoteControl(pi: ExtensionAPI) {
|
||||||
|
|
||||||
pi.on("agent_end", async (_event, ctx) => {
|
pi.on("agent_end", async (_event, ctx) => {
|
||||||
server?.broadcast({ type: "agent_end" });
|
server?.broadcast({ type: "agent_end" });
|
||||||
|
if (needsSyncOnIdle) {
|
||||||
|
needsSyncOnIdle = false;
|
||||||
|
server?.sync(ctx);
|
||||||
|
}
|
||||||
updateStatus(ctx);
|
updateStatus(ctx);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue