@@ -25,6 +25,8 @@ type Position = {
2525
2626const pad = ( n : number ) => n < 10 ? `0${ n . toFixed ( 0 ) } ` : `${ n . toFixed ( 0 ) } `
2727
28+ const SecondsTick = 1000
29+
2830export 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