@@ -12,6 +12,7 @@ Module.register("compliments", {
1212 } ,
1313 updateInterval : 30000 ,
1414 remoteFile : null ,
15+ remoteFileRefreshInterval : 0 ,
1516 fadeSpeed : 4000 ,
1617 morningStartTime : 3 ,
1718 morningEndTime : 12 ,
@@ -20,6 +21,9 @@ Module.register("compliments", {
2021 random : true ,
2122 specialDayUnique : false
2223 } ,
24+ urlSuffix : "" ,
25+ compliments_new : null ,
26+ refreshMinimumDelay : 15 * 60 * 60 * 1000 , // 15 minutes
2327 lastIndexUsed : - 1 ,
2428 // Set currentweather from module
2529 currentWeatherType : "" ,
@@ -41,6 +45,17 @@ Module.register("compliments", {
4145 const response = await this . loadComplimentFile ( ) ;
4246 this . config . compliments = JSON . parse ( response ) ;
4347 this . updateDom ( ) ;
48+ if ( this . config . remoteFileRefreshInterval !== 0 ) {
49+ if ( ( this . config . remoteFileRefreshInterval >= this . refreshMinimumDelay ) || window . mmTestMode === "true" ) {
50+ setInterval ( async ( ) => {
51+ const response = await this . loadComplimentFile ( ) ;
52+ this . compliments_new = JSON . parse ( response ) ;
53+ } ,
54+ this . config . remoteFileRefreshInterval ) ;
55+ } else {
56+ Log . error ( `${ this . name } remoteFileRefreshInterval less than minimum` ) ;
57+ }
58+ }
4459 }
4560 let minute_sync_delay = 1 ;
4661 // loop thru all the configured when events
@@ -185,7 +200,13 @@ Module.register("compliments", {
185200 async loadComplimentFile ( ) {
186201 const isRemote = this . config . remoteFile . indexOf ( "http://" ) === 0 || this . config . remoteFile . indexOf ( "https://" ) === 0 ,
187202 url = isRemote ? this . config . remoteFile : this . file ( this . config . remoteFile ) ;
188- const response = await fetch ( url ) ;
203+ // because we may be fetching the same url,
204+ // we need to force the server to not give us the cached result
205+ // create an extra property (ignored by the server handler) just so the url string is different
206+ // that will never be the same, using the ms value of date
207+ if ( this . config . remoteFileRefreshInterval !== 0 ) this . urlSuffix = `?dummy=${ Date . now ( ) } ` ;
208+ //
209+ const response = await fetch ( url + this . urlSuffix ) ;
189210 return await response . text ( ) ;
190211 } ,
191212
@@ -236,6 +257,27 @@ Module.register("compliments", {
236257 compliment . lastElementChild . remove ( ) ;
237258 wrapper . appendChild ( compliment ) ;
238259 }
260+ // if a new set of compliments was loaded from the refresh task
261+ // we do this here to make sure no other function is using the compliments list
262+ if ( this . compliments_new ) {
263+ // use them
264+ if ( JSON . stringify ( this . config . compliments ) !== JSON . stringify ( this . compliments_new ) ) {
265+ // only reset if the contents changes
266+ this . config . compliments = this . compliments_new ;
267+ // reset the index
268+ this . lastIndexUsed = - 1 ;
269+ }
270+ // clear new file list so we don't waste cycles comparing between refreshes
271+ this . compliments_new = null ;
272+ }
273+ // only in test mode
274+ if ( window . mmTestMode === "true" ) {
275+ // check for (undocumented) remoteFile2 to test new file load
276+ if ( this . config . remoteFile2 !== null && this . config . remoteFileRefreshInterval !== 0 ) {
277+ // switch the file so that next time it will be loaded from a changed file
278+ this . config . remoteFile = this . config . remoteFile2 ;
279+ }
280+ }
239281 return wrapper ;
240282 } ,
241283
0 commit comments