Skip to content

Commit bd620e0

Browse files
authored
Enhance compliments remote file with refresh support (#3630)
add support to refresh the compliments remotefile add testcases for both without refresh (testcase missing) and with refresh doc to be updated
1 parent f1522da commit bd620e0

File tree

6 files changed

+101
-1
lines changed

6 files changed

+101
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ _This release is scheduled to be released on 2025-01-01._
1616
- [docs] Added step for npm publishing in release process (#3595)
1717
- [core] Add GitHub workflow to run spellcheck a few days before each release.
1818
- [core] Add intest flag to index.html to pass to module js for test mode detection (needed by #3630)
19+
- [compliments] add support for refreshing remote compliments file, and testcases
1920

2021
### Removed
2122

modules/default/compliments/compliments.js

+43-1
Original file line numberDiff line numberDiff line change
@@ -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

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
let config = {
2+
address: "0.0.0.0",
3+
ipWhitelist: [],
4+
modules: [
5+
{
6+
module: "compliments",
7+
position: "bottom_bar",
8+
config: {
9+
updateInterval: 3000,
10+
remoteFile: "http://localhost:8080/tests/mocks/compliments_test.json"
11+
}
12+
}
13+
]
14+
};
15+
16+
/*************** DO NOT EDIT THE LINE BELOW ***************/
17+
if (typeof module !== "undefined") { module.exports = config; }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
let config = {
2+
address: "0.0.0.0",
3+
ipWhitelist: [],
4+
modules: [
5+
{
6+
module: "compliments",
7+
position: "bottom_bar",
8+
config: {
9+
updateInterval: 3000,
10+
remoteFileRefreshInterval: 1500,
11+
remoteFile: "http://localhost:8080/tests/mocks/compliments_test.json",
12+
remoteFile2: "http://localhost:8080/tests/mocks/compliments_file.json"
13+
}
14+
}
15+
]
16+
};
17+
18+
/*************** DO NOT EDIT THE LINE BELOW ***************/
19+
if (typeof module !== "undefined") { module.exports = config; }

tests/electron/modules/compliments_spec.js

+16
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,22 @@ describe("Compliments module", () => {
7878
await expect(doTest(["just a test"])).resolves.toBe(true);
7979
});
8080
});
81+
});
8182

83+
describe("Feature remote compliments file", () => {
84+
describe("get list from remote file", () => {
85+
it("shows 'Remote compliment file works!' as only anytime list set", async () => {
86+
await helpers.startApplication("tests/configs/modules/compliments/compliments_file.js", "01 Jan 2022 10:00:00 GMT");
87+
await expect(doTest(["Remote compliment file works!"])).resolves.toBe(true);
88+
});
89+
});
90+
describe("get updated list from remote file", () => {
91+
it("shows 'test in morning' as test time set to 10am", async () => {
92+
await helpers.startApplication("tests/configs/modules/compliments/compliments_file_change.js", "01 Jan 2022 10:00:00 GMT");
93+
await expect(doTest(["Remote compliment file works!"])).resolves.toBe(true);
94+
await new Promise((r) => setTimeout(r, 10000));
95+
await expect(doTest(["test in morning"])).resolves.toBe(true);
96+
});
97+
});
8298
});
8399
});

tests/mocks/compliments_file.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"morning": ["test in morning"],
3+
"afternoon": ["test in afternoon"],
4+
"evening": ["test in evening"]
5+
}

0 commit comments

Comments
 (0)