Compare commits

3 Commits

Author SHA1 Message Date
pdmarf
ce1a17975d v1.0.41: add 1-hour elapsed check with continue/stop dialog, auto-stop on no response 2026-07-13 13:21:34 +01:00
pdmarf
f76c5ac33b v1.0.40: bump version.json so auto-updater delivers update to staff
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-15 17:07:17 +01:00
pdmarf
6e0976a284 v1.0.40: fix duplicate timer race condition on rapid double-press
onKeyDown is async and calls await startTimer (~1s network). A second
press before that resolves saw the same state (isRunning=false,
memRunningEntryId=null) and created a second Notion entry. Only the
last startTimer call's ID was tracked, orphaning the first entry
running indefinitely in Notion.

pendingKeyDown Set acts as a per-action mutex: a second press while the
first is in-flight is dropped. try/finally guarantees the lock is always
released.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-13 18:18:52 +01:00
5 changed files with 174 additions and 47 deletions

View File

@@ -6438,7 +6438,7 @@ async function stopTimer(token, entryId) {
} }
// src/plugin.ts // src/plugin.ts
var CURRENT_VERSION = "1.0.39"; var CURRENT_VERSION = "1.0.41";
var GITEA_BASE = "https://gitea.pdmarf.co.uk/pdm/stream_deck_notion_timer/raw/branch/stable-rebuild"; var GITEA_BASE = "https://gitea.pdmarf.co.uk/pdm/stream_deck_notion_timer/raw/branch/stable-rebuild";
var SIGNING_PUBLIC_KEY = `-----BEGIN PUBLIC KEY----- var SIGNING_PUBLIC_KEY = `-----BEGIN PUBLIC KEY-----
MCowBQYDK2VwAyEAN7ko8TUpuPzPAJuKAZCRjV0c4ZSlou5d9pUAF6o12b4= MCowBQYDK2VwAyEAN7ko8TUpuPzPAJuKAZCRjV0c4ZSlou5d9pUAF6o12b4=
@@ -6567,8 +6567,61 @@ function isConfigured(g) {
function buttonTitle(projectName) { function buttonTitle(projectName) {
return projectName.replace(/^[\p{Extended_Pictographic}\uFE0F\s]+/u, "").trim(); return projectName.replace(/^[\p{Extended_Pictographic}\uFE0F\s]+/u, "").trim();
} }
var ELAPSED_CHECK_MS = 60 * 60 * 1e3;
var DIALOG_TIMEOUT_SECONDS = 300;
async function promptContinue(title) {
const { execFile: execFile3 } = await import("node:child_process");
const safeTitle = title.replace(/[\\"]/g, "");
const script = `display dialog "Timer for ${safeTitle} has been running for over an hour.
Should it continue?" buttons {"Stop", "Continue"} default button "Continue" giving up after ${DIALOG_TIMEOUT_SECONDS}`;
return new Promise((resolve) => {
execFile3("osascript", ["-e", script], (err, stdout) => {
if (err || /gave up:true/.test(String(stdout))) {
resolve("timeout");
return;
}
resolve(/button returned:Stop/.test(String(stdout)) ? "stop" : "continue");
});
});
}
var TimerToggle = class extends SingletonAction { var TimerToggle = class extends SingletonAction {
projectCache = /* @__PURE__ */ new Map(); projectCache = /* @__PURE__ */ new Map();
pendingKeyDown = /* @__PURE__ */ new Set();
elapsedCheckTimer = null;
clearElapsedCheck() {
if (this.elapsedCheckTimer) clearTimeout(this.elapsedCheckTimer);
this.elapsedCheckTimer = null;
}
scheduleElapsedCheck(entryId, actionId, title) {
this.clearElapsedCheck();
this.elapsedCheckTimer = setTimeout(() => {
this.checkElapsed(entryId, actionId, title);
}, ELAPSED_CHECK_MS);
}
async checkElapsed(entryId, actionId, title) {
if (memRunningEntryId !== entryId) return;
const answer = await promptContinue(title);
if (memRunningEntryId !== entryId) return;
if (answer === "continue") {
this.scheduleElapsedCheck(entryId, actionId, title);
return;
}
try {
const global = await getGlobal();
await stopTimer(global.notionToken, entryId);
} catch (err) {
plugin_default.logger.error("Auto-stop after elapsed-check failed:", err);
}
setRunningEntry(null, null);
this.clearElapsedCheck();
for (const action2 of this.actions) {
if (action2.id === actionId) {
await Promise.all([action2.setState(0), action2.setTitle(title)]);
break;
}
}
}
async onWillAppear(ev) { async onWillAppear(ev) {
this.projectCache.set(ev.action.id, ev.payload.settings); this.projectCache.set(ev.action.id, ev.payload.settings);
const title = buttonTitle(ev.payload.settings.projectName || ""); const title = buttonTitle(ev.payload.settings.projectName || "");
@@ -6584,52 +6637,61 @@ var TimerToggle = class extends SingletonAction {
await sendProjectsToPI(); await sendProjectsToPI();
} }
async onKeyDown(ev) { async onKeyDown(ev) {
const { projectId, projectName } = ev.payload.settings; if (this.pendingKeyDown.has(ev.action.id)) return;
const title = buttonTitle(projectName || ""); this.pendingKeyDown.add(ev.action.id);
const isRunning = memRunningActionId === ev.action.id;
if (projectId) {
if (isRunning) {
await Promise.all([ev.action.setState(0), ev.action.setTitle(title)]);
} else {
for (const other of this.actions) {
if (other.id === ev.action.id) continue;
if (memRunningActionId === other.id) {
const s = this.projectCache.get(other.id);
await Promise.all([other.setState(0), other.setTitle(buttonTitle(s?.projectName || ""))]);
}
}
await Promise.all([ev.action.setState(1), ev.action.setTitle(`\u23F1 ${title}`)]);
}
}
const global = await getGlobal();
if (!isConfigured(global)) {
await ev.action.showAlert();
return;
}
if (!projectId) {
await ev.action.showAlert();
return;
}
try { try {
if (isRunning) { const { projectId, projectName } = ev.payload.settings;
await stopTimer(global.notionToken, memRunningEntryId); const title = buttonTitle(projectName || "");
setRunningEntry(null, null); const isRunning = memRunningActionId === ev.action.id;
await Promise.all([ev.action.setState(0), ev.action.setTitle(title)]); if (projectId) {
} else { if (isRunning) {
if (memRunningEntryId) { await Promise.all([ev.action.setState(0), ev.action.setTitle(title)]);
await stopTimer(global.notionToken, memRunningEntryId); } else {
for (const other of this.actions) {
if (other.id === ev.action.id) continue;
if (memRunningActionId === other.id) {
const s = this.projectCache.get(other.id);
await Promise.all([other.setState(0), other.setTitle(buttonTitle(s?.projectName || ""))]);
}
}
await Promise.all([ev.action.setState(1), ev.action.setTitle(`\u23F1 ${title}`)]);
} }
const entryId = await startTimer(global.notionToken, global.timingDbId, projectId, projectName, global.userId);
setRunningEntry(entryId, ev.action.id);
await Promise.all([ev.action.setState(1), ev.action.setTitle(`\u23F1 ${title}`)]);
} }
} catch (err) { const global = await getGlobal();
await Promise.all([ if (!isConfigured(global)) {
ev.action.setState(isRunning ? 1 : 0), await ev.action.showAlert();
ev.action.setTitle(isRunning ? `\u23F1 ${title}` : title) return;
]); }
plugin_default.logger.error("Timer toggle failed:", err); if (!projectId) {
await ev.action.showAlert(); await ev.action.showAlert();
return;
}
try {
if (isRunning) {
await stopTimer(global.notionToken, memRunningEntryId);
setRunningEntry(null, null);
this.clearElapsedCheck();
await Promise.all([ev.action.setState(0), ev.action.setTitle(title)]);
} else {
if (memRunningEntryId) {
await stopTimer(global.notionToken, memRunningEntryId);
this.clearElapsedCheck();
}
const entryId = await startTimer(global.notionToken, global.timingDbId, projectId, projectName, global.userId);
setRunningEntry(entryId, ev.action.id);
this.scheduleElapsedCheck(entryId, ev.action.id, title);
await Promise.all([ev.action.setState(1), ev.action.setTitle(`\u23F1 ${title}`)]);
}
} catch (err) {
await Promise.all([
ev.action.setState(isRunning ? 1 : 0),
ev.action.setTitle(isRunning ? `\u23F1 ${title}` : title)
]);
plugin_default.logger.error("Timer toggle failed:", err);
await ev.action.showAlert();
}
} finally {
this.pendingKeyDown.delete(ev.action.id);
} }
} }
}; };

View File

@@ -1 +1,2 @@
в<╥!Ъ░xR√з└С;=▀LI" я╓╢HuэF├ы▐Ос─╒┘ sb▌Y╛{йH│╤Н.JщNЦь1╔l ג /}ֿI]<5D>“v¶₪6ִ®[cV”+<2B>הש™>f&6<>$
ת¹ת«b;<3B>׀<EFBFBD>אר»@פִb}©ע&X

Binary file not shown.

View File

@@ -1,4 +1,4 @@
const CURRENT_VERSION = "1.0.39"; const CURRENT_VERSION = "1.0.41";
const GITEA_BASE = "https://gitea.pdmarf.co.uk/pdm/stream_deck_notion_timer/raw/branch/stable-rebuild"; const GITEA_BASE = "https://gitea.pdmarf.co.uk/pdm/stream_deck_notion_timer/raw/branch/stable-rebuild";
const SIGNING_PUBLIC_KEY = `-----BEGIN PUBLIC KEY----- const SIGNING_PUBLIC_KEY = `-----BEGIN PUBLIC KEY-----
MCowBQYDK2VwAyEAN7ko8TUpuPzPAJuKAZCRjV0c4ZSlou5d9pUAF6o12b4= MCowBQYDK2VwAyEAN7ko8TUpuPzPAJuKAZCRjV0c4ZSlou5d9pUAF6o12b4=
@@ -164,9 +164,64 @@ function buttonTitle(projectName: string): string {
return projectName.replace(/^[\p{Extended_Pictographic}\uFE0F\s]+/u, "").trim(); return projectName.replace(/^[\p{Extended_Pictographic}\uFE0F\s]+/u, "").trim();
} }
const ELAPSED_CHECK_MS = 60 * 60 * 1000; // 1 hour
const DIALOG_TIMEOUT_SECONDS = 300; // 5 minutes
async function promptContinue(title: string): Promise<"stop" | "continue" | "timeout"> {
const { execFile } = await import("node:child_process");
const safeTitle = title.replace(/[\\"]/g, "");
const script = `display dialog "Timer for ${safeTitle} has been running for over an hour.\n\nShould it continue?" buttons {"Stop", "Continue"} default button "Continue" giving up after ${DIALOG_TIMEOUT_SECONDS}`;
return new Promise((resolve) => {
execFile("osascript", ["-e", script], (err, stdout) => {
if (err || /gave up:true/.test(String(stdout))) { resolve("timeout"); return; }
resolve(/button returned:Stop/.test(String(stdout)) ? "stop" : "continue");
});
});
}
@action({ UUID: "com.pdma.notion-timer.toggle" }) @action({ UUID: "com.pdma.notion-timer.toggle" })
class TimerToggle extends SingletonAction<TimerSettings> { class TimerToggle extends SingletonAction<TimerSettings> {
private projectCache = new Map<string, TimerSettings>(); private projectCache = new Map<string, TimerSettings>();
private pendingKeyDown = new Set<string>();
private elapsedCheckTimer: NodeJS.Timeout | null = null;
private clearElapsedCheck(): void {
if (this.elapsedCheckTimer) clearTimeout(this.elapsedCheckTimer);
this.elapsedCheckTimer = null;
}
private scheduleElapsedCheck(entryId: string, actionId: string, title: string): void {
this.clearElapsedCheck();
this.elapsedCheckTimer = setTimeout(() => {
this.checkElapsed(entryId, actionId, title);
}, ELAPSED_CHECK_MS);
}
private async checkElapsed(entryId: string, actionId: string, title: string): Promise<void> {
if (memRunningEntryId !== entryId) return;
const answer = await promptContinue(title);
if (memRunningEntryId !== entryId) return;
if (answer === "continue") {
this.scheduleElapsedCheck(entryId, actionId, title);
return;
}
try {
const global = await getGlobal();
await stopTimer(global.notionToken, entryId);
} catch (err) {
streamDeck.logger.error("Auto-stop after elapsed-check failed:", err);
}
setRunningEntry(null, null);
this.clearElapsedCheck();
for (const action of this.actions) {
if (action.id === actionId) {
await Promise.all([action.setState(0), action.setTitle(title)]);
break;
}
}
}
async onWillAppear(ev: WillAppearEvent<TimerSettings>): Promise<void> { async onWillAppear(ev: WillAppearEvent<TimerSettings>): Promise<void> {
this.projectCache.set(ev.action.id, ev.payload.settings); this.projectCache.set(ev.action.id, ev.payload.settings);
@@ -185,6 +240,9 @@ class TimerToggle extends SingletonAction<TimerSettings> {
} }
async onKeyDown(ev: KeyDownEvent<TimerSettings>): Promise<void> { async onKeyDown(ev: KeyDownEvent<TimerSettings>): Promise<void> {
if (this.pendingKeyDown.has(ev.action.id)) return;
this.pendingKeyDown.add(ev.action.id);
try {
const { projectId, projectName } = ev.payload.settings; const { projectId, projectName } = ev.payload.settings;
const title = buttonTitle(projectName || ""); const title = buttonTitle(projectName || "");
const isRunning = memRunningActionId === ev.action.id; const isRunning = memRunningActionId === ev.action.id;
@@ -213,13 +271,16 @@ class TimerToggle extends SingletonAction<TimerSettings> {
if (isRunning) { if (isRunning) {
await stopTimer(global.notionToken, memRunningEntryId!); await stopTimer(global.notionToken, memRunningEntryId!);
setRunningEntry(null, null); setRunningEntry(null, null);
this.clearElapsedCheck();
await Promise.all([ev.action.setState(0), ev.action.setTitle(title)]); await Promise.all([ev.action.setState(0), ev.action.setTitle(title)]);
} else { } else {
if (memRunningEntryId) { if (memRunningEntryId) {
await stopTimer(global.notionToken, memRunningEntryId); await stopTimer(global.notionToken, memRunningEntryId);
this.clearElapsedCheck();
} }
const entryId = await startTimer(global.notionToken, global.timingDbId, projectId, projectName, global.userId); const entryId = await startTimer(global.notionToken, global.timingDbId, projectId, projectName, global.userId);
setRunningEntry(entryId, ev.action.id); setRunningEntry(entryId, ev.action.id);
this.scheduleElapsedCheck(entryId, ev.action.id, title);
await Promise.all([ev.action.setState(1), ev.action.setTitle(`${title}`)]); await Promise.all([ev.action.setState(1), ev.action.setTitle(`${title}`)]);
} }
} catch (err) { } catch (err) {
@@ -231,6 +292,9 @@ class TimerToggle extends SingletonAction<TimerSettings> {
streamDeck.logger.error("Timer toggle failed:", err); streamDeck.logger.error("Timer toggle failed:", err);
await ev.action.showAlert(); await ev.action.showAlert();
} }
} finally {
this.pendingKeyDown.delete(ev.action.id);
}
} }
} }

View File

@@ -1 +1 @@
{ "version": "1.0.39" } { "version": "1.0.41" }