add configurable time entry poller

This commit is contained in:
2025-01-15 14:18:33 +01:00
parent faadb1a4d3
commit 8cd5a65542
2 changed files with 41 additions and 1 deletions

View File

@@ -3,7 +3,8 @@ import { type SomeCompanionConfigField } from '@companion-module/base'
export interface ModuleConfig { export interface ModuleConfig {
apiToken?: string apiToken?: string
workspaceName: string workspaceName: string
alwaysStart?: boolean alwaysStart: boolean
startTimerPoller: boolean
} }
export function GetConfigFields(): SomeCompanionConfigField[] { export function GetConfigFields(): SomeCompanionConfigField[] {
@@ -32,5 +33,12 @@ export function GetConfigFields(): SomeCompanionConfigField[] {
width: 12, width: 12,
default: false, default: false,
}, },
{
type: 'checkbox',
id: 'startTimerPoller',
label: 'Poll for current time entry every 30 seconds',
width: 12,
default: false,
},
] ]
} }

View File

@@ -18,6 +18,7 @@ export class TogglTrack extends InstanceBase<ModuleConfig> {
workspaceId?: number // current active workspace id workspaceId?: number // current active workspace id
workspaceName: string = '' // name of workspace workspaceName: string = '' // name of workspace
projects?: { id: number; label: string }[] projects?: { id: number; label: string }[]
intervalId?: NodeJS.Timeout
constructor(internal: unknown) { constructor(internal: unknown) {
super(internal) super(internal)
@@ -29,6 +30,9 @@ export class TogglTrack extends InstanceBase<ModuleConfig> {
async destroy(): Promise<void> { async destroy(): Promise<void> {
this.log('info', 'destroy ' + this.id) this.log('info', 'destroy ' + this.id)
if (this.config.startTimerPoller) {
this.stopTimeEntryPoller()
}
} }
async init(config: ModuleConfig): Promise<void> { async init(config: ModuleConfig): Promise<void> {
@@ -56,6 +60,10 @@ export class TogglTrack extends InstanceBase<ModuleConfig> {
if (this.toggl && this.workspaceId) { if (this.toggl && this.workspaceId) {
this.updateStatus(InstanceStatus.Ok) this.updateStatus(InstanceStatus.Ok)
} }
if (this.config.startTimerPoller) {
this.startTimeEntryPoller()
}
} }
async configUpdated(config: ModuleConfig): Promise<void> { async configUpdated(config: ModuleConfig): Promise<void> {
@@ -63,6 +71,7 @@ export class TogglTrack extends InstanceBase<ModuleConfig> {
const apiTokenChanged: boolean = this.config.apiToken != config.apiToken const apiTokenChanged: boolean = this.config.apiToken != config.apiToken
const workSpaceDefaultChanged: boolean = this.config.workspaceName != config.workspaceName const workSpaceDefaultChanged: boolean = this.config.workspaceName != config.workspaceName
const timeEntryPollerChanged: boolean = this.config.startTimerPoller != config.startTimerPoller
this.config = config this.config = config
@@ -75,6 +84,14 @@ export class TogglTrack extends InstanceBase<ModuleConfig> {
await this.getWorkspace() await this.getWorkspace()
} }
if (timeEntryPollerChanged) {
if (this.config.startTimerPoller) {
this.startTimeEntryPoller()
} else {
this.stopTimeEntryPoller()
}
}
this.updateActions() this.updateActions()
//this.updateVariables() //this.updateVariables()
if (this.toggl && this.workspaceId) { if (this.toggl && this.workspaceId) {
@@ -117,6 +134,21 @@ export class TogglTrack extends InstanceBase<ModuleConfig> {
} }
} }
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<number | null> { async getCurrentTimer(): Promise<number | null> {
this.log('debug', 'function: getCurrentTimer') this.log('debug', 'function: getCurrentTimer')