1
0
Fork 0

[sleeplog] Change .onChange from array to object

master
storm64 2022-11-10 17:04:54 +01:00
parent 81aece08ee
commit 58330df11c
3 changed files with 71 additions and 70 deletions

View File

@ -7,4 +7,4 @@
0.10: Complete rework off this app! 0.10: Complete rework off this app!
0.10beta: Add interface.html to view saved log data, add "View log" function for debugging log, send data for gadgetbridge, change caching for global getStats 0.10beta: Add interface.html to view saved log data, add "View log" function for debugging log, send data for gadgetbridge, change caching for global getStats
0.11: Prevent module not found error 0.11: Prevent module not found error
0.12: Execute functions in `sleeplog.onChange[fn({timestamp, status, consecutive})]` on a status change 0.12: Execute functions in `sleeplog.onChange` object on a status change

View File

@ -154,10 +154,10 @@ Available through the App Loader when your watch is connected.
#### Add functions triggered by status changes #### Add functions triggered by status changes
With the following code it is possible to add functions that will be called on status changes. With the following code it is possible to add functions that will be called on status changes.
``` ```
// first ensure that the sleeplog object is available // first ensure that the sleeplog onChange object is available
if (typeof (global.sleeplog || {}).onChange === "object") { if (typeof (global.sleeplog || {}).onChange === "object") {
// then add your function to the onChange-array // then add your function to the onChange object
sleeplog.onChange.push( function(data) { print(data); } ); sleeplog.onChange["my app name"] = function(data) { print(data); };
} }
``` ```
The passed data object has the following properties: The passed data object has the following properties:

View File

@ -258,19 +258,20 @@ if (sleeplog.conf.enabled) {
// check if the status has changed // check if the status has changed
if (data.status !== this.status || data.consecutive !== this.consecutive) { if (data.status !== this.status || data.consecutive !== this.consecutive) {
// check for onChange functions // read and check for onChange functions
if ((this.onChange || []).length) { var onChange = Object.keys(this.onChange) || [];
this.onChange.forEach(fn => { if (onChange.length) onChange.forEach(key => {
// setup timeouts to start onChange functions if fn is a function // read function to key
if (typeof fn === "function") setTimeout(fn, 100, { var fn = this.onChange[key];
timestamp: new Date(data.timestamp), // setup timeouts to start onChange functions if fn is a function
status: data.status === this.status ? undefined : data.status, if (typeof fn === "function") setTimeout(fn, 100, {
consecutive: data.consecutive === this.consecutive ? undefined : data.consecutive, timestamp: new Date(data.timestamp),
prevStatus: this.status, status: data.status === this.status ? undefined : data.status,
prevConsecutive: this.consecutive consecutive: data.consecutive === this.consecutive ? undefined : data.consecutive,
}); prevStatus: this.status,
prevConsecutive: this.consecutive
}); });
} });
// append status // append status
this.appendStatus(data.timestamp, data.status, data.consecutive); this.appendStatus(data.timestamp, data.status, data.consecutive);
@ -335,7 +336,7 @@ if (sleeplog.conf.enabled) {
return this.statsCache; return this.statsCache;
}, },
// define array for functions to execute after a status change (changes had hapened 10min earlier) // define object for functions to execute after a status change (changes had hapened 10min earlier)
// changed values will be passed as object with the following properties: // changed values will be passed as object with the following properties:
// timestamp: as date object, // timestamp: as date object,
// status: if changed 0-4 else undefined, // status: if changed 0-4 else undefined,