Skip to content

Commit 851aaf7

Browse files
committed
feat: expose poll interval as config #30
1 parent db0d396 commit 851aaf7

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

Diff for: src/config.ts

+31-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { SomeCompanionConfigField } from '@companion-module/base'
1+
import type { SomeCompanionConfigField } from '@companion-module/base'
2+
3+
export const DEFAULT_CONFIG: DeviceConfig = {
4+
pollInterval: 3,
5+
}
26

37
export interface DeviceConfig {
48
clientId?: string
@@ -8,6 +12,7 @@ export interface DeviceConfig {
812
refreshToken?: string
913
deviceId?: string
1014
authURL?: string
15+
pollInterval: number
1116
}
1217

1318
export function GetConfigFields(): SomeCompanionConfigField[] {
@@ -61,5 +66,30 @@ export function GetConfigFields(): SomeCompanionConfigField[] {
6166
width: 12,
6267
label: 'Auth URL',
6368
},
69+
{
70+
type: 'textinput',
71+
id: 'pollInterval',
72+
width: 6,
73+
label: 'API Poll Interval (seconds)',
74+
},
75+
{
76+
type: 'static-text',
77+
id: '_poll_info_',
78+
width: 6,
79+
label: '',
80+
value:
81+
'This is how often the module will poll the Spotify API for updates. Default is 3 seconds. This may need to be increased if reaching the api rate limit.',
82+
},
6483
]
6584
}
85+
86+
export function ensureRequiredConfigIsDefined(config: DeviceConfig): boolean {
87+
let changed = false
88+
89+
if (!config.pollInterval || config.pollInterval < 0) {
90+
config.pollInterval = DEFAULT_CONFIG.pollInterval
91+
changed = true
92+
}
93+
94+
return changed
95+
}

Diff for: src/index.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
SomeCompanionConfigField,
99
} from '@companion-module/base'
1010
import PQueue from 'p-queue'
11-
import { GetConfigFields, DeviceConfig } from './config.js'
11+
import { GetConfigFields, DeviceConfig, DEFAULT_CONFIG, ensureRequiredConfigIsDefined } from './config.js'
1212
import { FeedbackId, GetFeedbacksList } from './feedback.js'
1313
import { DoAction, GetActionsList } from './actions.js'
1414
import { SpotifyPlaybackState, SpotifyState } from './state.js'
@@ -48,7 +48,7 @@ class SpotifyInstance extends InstanceBase<DeviceConfig> implements SpotifyInsta
4848
playbackState: null,
4949
}
5050

51-
this.config = {}
51+
this.config = { ...DEFAULT_CONFIG }
5252
}
5353

5454
public async checkIfApiErrorShouldRetry(err: any): Promise<boolean> {
@@ -106,6 +106,9 @@ class SpotifyInstance extends InstanceBase<DeviceConfig> implements SpotifyInsta
106106

107107
async configUpdated(config: DeviceConfig): Promise<void> {
108108
this.config = config
109+
if (ensureRequiredConfigIsDefined(this.config)) {
110+
this.saveConfig(this.config)
111+
}
109112

110113
this.setupOrRefreshAuthentication()
111114

@@ -191,13 +194,16 @@ class SpotifyInstance extends InstanceBase<DeviceConfig> implements SpotifyInsta
191194

192195
async init(config: DeviceConfig): Promise<void> {
193196
this.config = config
197+
if (ensureRequiredConfigIsDefined(this.config)) {
198+
this.saveConfig(this.config)
199+
}
194200

195201
this.updateStatus(InstanceStatus.Connecting)
196202

197203
this.setupOrRefreshAuthentication()
198204

199-
if (!this.pollTimer) {
200-
this.pollTimer = setInterval(() => this.queuePoll(), 3000) // Check every 3 seconds. This leaves a bit of headroom before we hit the daily api limit
205+
if (!this.pollTimer && this.config.pollInterval) {
206+
this.pollTimer = setInterval(() => this.queuePoll(), this.config.pollInterval * 1000) // Check every 3 seconds. This leaves a bit of headroom before we hit the daily api limit
201207
}
202208

203209
this.initActions()

0 commit comments

Comments
 (0)