Skip to content

Commit

Permalink
Recurring refresh with timeout instead of interval
Browse files Browse the repository at this point in the history
  • Loading branch information
DEV2DEV-DE committed Nov 29, 2023
1 parent 08313ce commit 4e30eed
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Jablotron extends utils.Adapter {

this.connected = false;
this.sessionId = '';
this.refreshInterval = undefined;
this.timeout = null

Check failure on line 35 in main.js

View workflow job for this annotation

GitHub Actions / check-and-lint

Missing semicolon
this.states = [];

axios.defaults.withCredentials = true; // force axios to use cookies
Expand Down Expand Up @@ -77,16 +77,8 @@ class Jablotron extends utils.Adapter {

// create interval for recurring tasks
if (this.connected) {
this.log.debug('Setting up polling interval');
this.refreshInterval = this.setInterval(() => {
try {
this.log.debug('Fetch data from jablonet.net');
this.getExtendedData(headers, this.sessionId);
} catch (error) {
this.log.error('Error in polling interval: ' + error);
this.connected = false;
}
}, this.config.pollInterval * 1000);
this.log.debug('Setting up recurring refresh');
this.recurringRefresh();
// subscribe to all state changes
// this.subscribeStates('status.alarm');
} else {
Expand Down Expand Up @@ -157,6 +149,18 @@ class Jablotron extends utils.Adapter {
}
}

/**
* Recursively refreshes data at a specified interval.
* @returns {void}
*/
async recurringRefresh() {
this.timeout = setTimeout(() => {
this.log.debug('Fetch data from jablonet.net');
this.getExtendedData(headers, this.sessionId);
this.recurringRefresh();
}, this.config.pollInterval * 1000);
}

/**
* read all services related to the account
* @param {object} headers
Expand Down Expand Up @@ -346,7 +350,7 @@ class Jablotron extends utils.Adapter {
*/
onUnload(callback) {
try {
this.clearInterval(this.refreshInterval);
this.clearTimeout(this.timeout);
this.connected = false;
callback();
} catch (e) {
Expand Down

0 comments on commit 4e30eed

Please sign in to comment.