@@ -12,6 +12,7 @@ Module.register("compliments", {
12
12
} ,
13
13
updateInterval : 30000 ,
14
14
remoteFile : null ,
15
+ remoteFileRefreshInterval : 0 ,
15
16
fadeSpeed : 4000 ,
16
17
morningStartTime : 3 ,
17
18
morningEndTime : 12 ,
@@ -20,6 +21,9 @@ Module.register("compliments", {
20
21
random : true ,
21
22
specialDayUnique : false
22
23
} ,
24
+ urlSuffix : "" ,
25
+ compliments_new : null ,
26
+ refreshMinimumDelay : 15 * 60 * 60 * 1000 , // 15 minutes
23
27
lastIndexUsed : - 1 ,
24
28
// Set currentweather from module
25
29
currentWeatherType : "" ,
@@ -41,6 +45,17 @@ Module.register("compliments", {
41
45
const response = await this . loadComplimentFile ( ) ;
42
46
this . config . compliments = JSON . parse ( response ) ;
43
47
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
+ }
44
59
}
45
60
let minute_sync_delay = 1 ;
46
61
// loop thru all the configured when events
@@ -185,7 +200,13 @@ Module.register("compliments", {
185
200
async loadComplimentFile ( ) {
186
201
const isRemote = this . config . remoteFile . indexOf ( "http://" ) === 0 || this . config . remoteFile . indexOf ( "https://" ) === 0 ,
187
202
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 ) ;
189
210
return await response . text ( ) ;
190
211
} ,
191
212
@@ -236,6 +257,27 @@ Module.register("compliments", {
236
257
compliment . lastElementChild . remove ( ) ;
237
258
wrapper . appendChild ( compliment ) ;
238
259
}
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
+ }
239
281
return wrapper ;
240
282
} ,
241
283
0 commit comments