Skip to content

Commit 22fefe4

Browse files
tbowmoThomas Bowman Mørch
andauthored
Ensure that repeat interval is a sane number (#33)
* Ensure that repeat interval is a sane number * Simplify fallback logic * Rearrange lines a bit * Default repeatInterval when repeat not enabled --------- Co-authored-by: Thomas Bowman Mørch <thomas.git@bowmo.dk>
1 parent 047fc72 commit 22fefe4

File tree

3 files changed

+25
-17
lines changed

3 files changed

+25
-17
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"author": {
44
"name": "Thomas Bowman Mørch"
55
},
6-
"version": "0.15.0",
6+
"version": "0.15.1",
77
"engines": {
88
"node": ">=14.0.0"
99
},

src/lib/small-timer-runner.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,9 +304,11 @@ describe('lib/small-timer-runner', () => {
304304
stubs.stubbedTimeCalc.getOnState.returns(false)
305305

306306
new SmallTimerRunner(stubs.position, stubs.configuration, stubs.node)
307+
const initialCallCount = stubs.status.callCount
307308
sinon.clock.tick(60000)
308309

309310
expect(stubs.send.callCount).to.equal(0)
311+
expect(stubs.status.callCount).to.be.greaterThan(initialCallCount)
310312
})
311313

312314
it('should repeat twice in 60 seconds when configured to 30 second repeat interval', () => {

src/lib/small-timer-runner.ts

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ type Position = {
2525

2626
const pad = (n: number) => n < 10 ? `0${n.toFixed(0)}` : `${n.toFixed(0)}`
2727

28+
const SecondsTick = 1000
29+
2830
export class SmallTimerRunner {
2931

3032
private startupTock: ReturnType<typeof setTimeout> | undefined = undefined
@@ -54,7 +56,7 @@ export class SmallTimerRunner {
5456
private sendEmptyPayload: boolean
5557

5658
// Default to 20 seconds between ticks (update of state / node)
57-
private defaultTickTimer = 20000
59+
private defaultTickTimer = SecondsTick * 20
5860

5961
constructor(
6062
position: Position,
@@ -79,19 +81,25 @@ export class SmallTimerRunner {
7981
this.offMsgType = configuration.offMsgType
8082
this.rules = configuration.rules
8183
this.repeat = configuration.repeat
82-
this.repeatInterval = Number(configuration.repeatInterval)
84+
85+
this.repeatInterval = this.repeat
86+
? Number(configuration.repeatInterval || 60)
87+
: 60
88+
89+
// default tick timer is 3 times as frequent as repeat timer, but never below 1 second
90+
this.defaultTickTimer = this.repeatInterval * SecondsTick / 3
91+
if (this.defaultTickTimer < SecondsTick) {
92+
this.defaultTickTimer = SecondsTick
93+
}
94+
8395
this.debugMode = configuration.debugEnable
8496

8597
this.onTimeout = Number(configuration.onTimeout)
8698
this.offTimeout = Number(configuration.offTimeout)
8799
this.sendEmptyPayload = configuration.sendEmptyPayload ?? true
88100

89-
// default tick timer is 3 times as frequent as repeat timer, but never below 1 second
90-
this.defaultTickTimer = this.repeatInterval * 1000 / 3
91-
if (this.defaultTickTimer < 1000) { this.defaultTickTimer = 1000 }
92-
93101
if (configuration.injectOnStartup) {
94-
this.startupTock = setTimeout(this.forceSend.bind(this), 2000)
102+
this.startupTock = setTimeout(this.forceSend.bind(this), 2 * SecondsTick)
95103
} else {
96104
this.calcState()
97105
this.updateNodeStatus()
@@ -194,7 +202,7 @@ export class SmallTimerRunner {
194202
}
195203

196204
private shouldRepeatPublish(): boolean {
197-
const seconds = Date.now() / 1000
205+
const seconds = Date.now() / SecondsTick
198206
if (this.repeat && (seconds - this.lastPublish >= this.repeatInterval)) {
199207
this.lastPublish = seconds
200208
return true
@@ -211,7 +219,7 @@ export class SmallTimerRunner {
211219
const nextChange = this.updateNodeStatus()
212220

213221
if (nextChange < 60) {
214-
this.startTickTimer(1000)
222+
this.startTickTimer(SecondsTick)
215223
} else {
216224
this.startTickTimer(this.defaultTickTimer)
217225
}
@@ -412,16 +420,14 @@ export class SmallTimerRunner {
412420
}
413421
}
414422

415-
private startTickTimer(interval: number): void {
416-
if (this.tickTimer && (interval === this.tickTimerInterval)) {
423+
private startTickTimer(newInterval: number): void {
424+
if (this.tickTimer && (newInterval === this.tickTimerInterval)) {
417425
// No need in (re) starting the tick timer, if it running with desired interval already
418426
return
419427
}
420428

421-
if (this.tickTimer) {
422-
this.stopTickTimer()
423-
}
424-
this.tickTimerInterval = interval
425-
this.tickTimer = setInterval(this.timerEvent.bind(this), interval)
429+
this.stopTickTimer()
430+
this.tickTimerInterval = newInterval
431+
this.tickTimer = setInterval(this.timerEvent.bind(this), newInterval)
426432
}
427433
}

0 commit comments

Comments
 (0)