Skip to content

Commit

Permalink
fix: Improve status refreshing logic
Browse files Browse the repository at this point in the history
  • Loading branch information
KieraDOG committed Aug 12, 2024
1 parent dea0dac commit dc782ef
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/CGDGarageDoor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,23 +145,29 @@ export class CGDGarageDoor {
private refreshStatus = async () => {
this.log.debug(`Refreshing status... ${this.isUpdating}`);
if (this.isUpdating) {
this.log.debug('Skip');
this.log.debug('Skip refreshing status because it is updating');

return;
}

const status = await this.getStatus();
if (!status || this.isStatusEqual(status)) {
if (this.isStatusEqual(status)) {
this.log.debug('Skip updating status because it is equal');
return;
}

if (this.isUpdating) {
this.log.info('Skip updating status because it is updating');
return;
}

this.status = status;
this.statusUpdateListener?.();
};

private isStatusEqual = (data: Status) => {
private isStatusEqual = (data?: Status) => {
const values = ['lamp', 'door', 'vacation'];
return values.every((value) => this.status?.[value] === data[value]);
return values.every((value) => this.status?.[value] === data?.[value]);
};

private poolStatus = async () => {
Expand Down

0 comments on commit dc782ef

Please sign in to comment.