v1.0.15: fix cross-folder timer conflict + new icons

- Stop previously running timer globally using persisted runningEntryId,
  works across nested folders and plugin restarts
- In-memory cache prevents onWillAppear flash of stale running state
- Replace SVG button icons with Aurora timer PNG icons (on/off)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
pdmarf
2026-04-21 18:28:30 +01:00
parent f69eb8e841
commit c1384e942b
9 changed files with 96 additions and 51 deletions

View File

@@ -1,4 +1,4 @@
const CURRENT_VERSION = "1.0.14";
const CURRENT_VERSION = "1.0.15";
const GITEA_BASE = "https://gitea.pdmarf.co.uk/pdm/stream_deck_notion_timer/raw/branch/master";
const SIGNING_PUBLIC_KEY = `-----BEGIN PUBLIC KEY-----
MCowBQYDK2VwAyEAN7ko8TUpuPzPAJuKAZCRjV0c4ZSlou5d9pUAF6o12b4=
@@ -66,6 +66,7 @@ interface GlobalSettings {
timingDbId: string;
projectsDbId: string;
userId: string;
runningEntryId?: string | null;
}
interface TimerSettings {
@@ -84,6 +85,23 @@ async function getGlobal(): Promise<GlobalSettings> {
return { ...stored, ...HARDCODED };
}
// In-memory cache so onWillAppear can check running state without an async round-trip
let memRunningEntryId: string | null | undefined = undefined; // undefined = not yet loaded
async function getRunningEntryId(): Promise<string | null> {
if (memRunningEntryId === undefined) {
const stored = await streamDeck.settings.getGlobalSettings<GlobalSettings>();
memRunningEntryId = stored.runningEntryId ?? null;
}
return memRunningEntryId;
}
async function setRunningEntry(entryId: string | null): Promise<void> {
memRunningEntryId = entryId;
const stored = await streamDeck.settings.getGlobalSettings<GlobalSettings>();
await streamDeck.settings.setGlobalSettings({ ...stored, runningEntryId: entryId });
}
function isConfigured(g: GlobalSettings): boolean {
return !!(g.notionToken && g.userId);
}
@@ -98,13 +116,26 @@ class TimerToggle extends SingletonAction<TimerSettings> {
private settingsCache = new Map<string, TimerSettings>();
async onWillAppear(ev: WillAppearEvent<TimerSettings>): Promise<void> {
this.settingsCache.set(ev.action.id, ev.payload.settings);
const { activeEntryId, projectName } = ev.payload.settings;
const title = buttonTitle(projectName || "");
if (activeEntryId) {
await Promise.all([ev.action.setState(1), ev.action.setTitle(`${title}`)]);
} else {
// Use in-memory cache to determine correct state before rendering — no flash
const running = await getRunningEntryId();
const isRunning = !!activeEntryId && activeEntryId === running;
if (activeEntryId && !isRunning) {
// Self-heal: this button thinks it's running but it's not — clear it
const cleared = { ...ev.payload.settings, activeEntryId: null };
await ev.action.setSettings(cleared);
this.settingsCache.set(ev.action.id, cleared);
await Promise.all([ev.action.setState(0), ev.action.setTitle(title)]);
} else {
this.settingsCache.set(ev.action.id, ev.payload.settings);
if (isRunning) {
await Promise.all([ev.action.setState(1), ev.action.setTitle(`${title}`)]);
} else {
await Promise.all([ev.action.setState(0), ev.action.setTitle(title)]);
}
}
}
@@ -153,19 +184,23 @@ class TimerToggle extends SingletonAction<TimerSettings> {
this.settingsCache.set(ev.action.id, stopped);
await ev.action.setState(0);
await ev.action.setTitle(title);
await ev.action.showOk();
await setRunningEntry(null);
} else {
// Stop any other running timer first
for (const other of this.actions) {
if (other.id === ev.action.id) continue;
const otherSettings = this.settingsCache.get(other.id);
if (otherSettings?.activeEntryId) {
await stopTimer(global.notionToken, otherSettings.activeEntryId);
const stopped = { ...otherSettings, activeEntryId: null };
await other.setSettings(stopped);
this.settingsCache.set(other.id, stopped);
await other.setState(0);
await other.setTitle(buttonTitle(otherSettings.projectName || ""));
const prevEntryId = await getRunningEntryId();
// Stop previous timer
if (prevEntryId) {
await stopTimer(global.notionToken, prevEntryId);
for (const other of this.actions) {
if (other.id === ev.action.id) continue;
const otherSettings = this.settingsCache.get(other.id);
if (otherSettings?.activeEntryId === prevEntryId) {
const stopped = { ...otherSettings, activeEntryId: null };
await other.setSettings(stopped);
this.settingsCache.set(other.id, stopped);
await other.setState(0);
await other.setTitle(buttonTitle(otherSettings.projectName || ""));
}
}
}
@@ -179,9 +214,9 @@ class TimerToggle extends SingletonAction<TimerSettings> {
const started = { ...ev.payload.settings, activeEntryId: entryId };
await ev.action.setSettings(started);
this.settingsCache.set(ev.action.id, started);
await setRunningEntry(entryId);
await ev.action.setState(1);
await ev.action.setTitle(`${title}`);
await ev.action.showOk();
}
} catch (err) {
streamDeck.logger.error("Timer toggle failed:", err);