From 8cd5a655428f4f3001b6dab336090dcfe6a08240 Mon Sep 17 00:00:00 2001 From: Matthias Kesler Date: Wed, 15 Jan 2025 14:18:33 +0100 Subject: [PATCH] add configurable time entry poller --- src/config.ts | 10 +++++++++- src/main.ts | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/config.ts b/src/config.ts index a39b391..ffea962 100644 --- a/src/config.ts +++ b/src/config.ts @@ -3,7 +3,8 @@ import { type SomeCompanionConfigField } from '@companion-module/base' export interface ModuleConfig { apiToken?: string workspaceName: string - alwaysStart?: boolean + alwaysStart: boolean + startTimerPoller: boolean } export function GetConfigFields(): SomeCompanionConfigField[] { @@ -32,5 +33,12 @@ export function GetConfigFields(): SomeCompanionConfigField[] { width: 12, default: false, }, + { + type: 'checkbox', + id: 'startTimerPoller', + label: 'Poll for current time entry every 30 seconds', + width: 12, + default: false, + }, ] } diff --git a/src/main.ts b/src/main.ts index 0a746ce..1f7ff88 100644 --- a/src/main.ts +++ b/src/main.ts @@ -18,6 +18,7 @@ export class TogglTrack extends InstanceBase { workspaceId?: number // current active workspace id workspaceName: string = '' // name of workspace projects?: { id: number; label: string }[] + intervalId?: NodeJS.Timeout constructor(internal: unknown) { super(internal) @@ -29,6 +30,9 @@ export class TogglTrack extends InstanceBase { async destroy(): Promise { this.log('info', 'destroy ' + this.id) + if (this.config.startTimerPoller) { + this.stopTimeEntryPoller() + } } async init(config: ModuleConfig): Promise { @@ -56,6 +60,10 @@ export class TogglTrack extends InstanceBase { if (this.toggl && this.workspaceId) { this.updateStatus(InstanceStatus.Ok) } + + if (this.config.startTimerPoller) { + this.startTimeEntryPoller() + } } async configUpdated(config: ModuleConfig): Promise { @@ -63,6 +71,7 @@ export class TogglTrack extends InstanceBase { const apiTokenChanged: boolean = this.config.apiToken != config.apiToken const workSpaceDefaultChanged: boolean = this.config.workspaceName != config.workspaceName + const timeEntryPollerChanged: boolean = this.config.startTimerPoller != config.startTimerPoller this.config = config @@ -75,6 +84,14 @@ export class TogglTrack extends InstanceBase { await this.getWorkspace() } + if (timeEntryPollerChanged) { + if (this.config.startTimerPoller) { + this.startTimeEntryPoller() + } else { + this.stopTimeEntryPoller() + } + } + this.updateActions() //this.updateVariables() if (this.toggl && this.workspaceId) { @@ -117,6 +134,21 @@ export class TogglTrack extends InstanceBase { } } + private startTimeEntryPoller(): void { + this.log('info', 'Starting TimeEntry-Poller') + // fetch current timer every 30 seconds + this.intervalId = setInterval(() => { + // this harms the linter (handle unawaited promise in an non-async context) + void (async () => { + await this.getCurrentTimer() + })() + }, 30 * 1000) + } + private stopTimeEntryPoller(): void { + this.log('info', 'Stopping TimeEntry-Poller') + clearInterval(this.intervalId) + } + async getCurrentTimer(): Promise { this.log('debug', 'function: getCurrentTimer')