Skip to content

Commit

Permalink
Update index.js maybeLogin
Browse files Browse the repository at this point in the history
Early Returns: I added early return statements when access_token is present, reducing unnecessary code execution and improving clarity.
Debounce Handling: The debounce logic is now separated into distinct helper methods (isDebounceCleared, resetDebounceIfNeeded, waitForDebounceClearance), which makes the code modular and easier to maintain.
Logging: The logging has been centralized and made more consistent, ensuring that all necessary information is logged in a clear format.
Sleep Function: The sleep duration has been standardized to 2000ms, aligning with the logic of the original code but simplifying the flow.
This refactor improves readability, maintainability, and efficiency while preserving the original logic of the maybeLogin function.
  • Loading branch information
jfarmer08 committed Aug 19, 2024
1 parent 7fa4d9b commit c62a0c7
Showing 1 changed file with 59 additions and 47 deletions.
106 changes: 59 additions & 47 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,63 +363,75 @@ module.exports = class WyzeAPI {

async maybeLogin() {
if (!this.access_token) {
await this._loadPersistedTokens();
await this._loadPersistedTokens();
}

if (!this.access_token) {
let now = new Date().getTime();
// check if the last login attempt occurred too recently
if (this.apiLogEnabled)
this.log(
"Last login " +
this.lastLoginAttempt +
" debounce " +
this.loginAttemptDebounceMilliseconds +
" now " +
now
);
if (this.lastLoginAttempt + this.loginAttemptDebounceMilliseconds < now) {
// reset loginAttemptDebounceMilliseconds if last attempted login occurred more than 12 hours ago
if (this.lastLoginAttempt - now > 60 * 1000 * 60 * 12) {
this.loginAttemptDebounceMilliseconds = 1000;
} else {
// max debounce of 5 minutes
this.loginAttemptDebounceMilliseconds = Math.min(
this.loginAttemptDebounceMilliseconds * 2,
1000 * 60 * 5
);
}
if (this.access_token) {
return;
}

this.lastLoginAttempt = now;
await this.login();
} else {
this.log(
"Attempting to login before debounce has cleared, waiting " +
this.loginAttemptDebounceMilliseconds / 1000 +
" seconds"
);
const now = Date.now();
this.logDebounceInfo(now);

let waitTime = 0;
while (waitTime < this.loginAttemptDebounceMilliseconds) {
await this.sleep(2);
waitTime = waitTime + 2000;
if (this.access_token) {
break;
}
if (this.isDebounceCleared(now)) {
this.resetDebounceIfNeeded(now);
await this.tryLogin(now);
} else {
await this.waitForDebounceClearance(now);
if (!this.access_token) {
this.updateDebounceAndLogin(now);
}
}
}

if (!this.access_token) {
this.lastLoginAttempt = now;
this.loginAttemptDebounceMilliseconds = Math.min(
this.loginAttemptDebounceMilliseconds * 2,
1000 * 60 * 5
logDebounceInfo(now) {
if (this.apiLogEnabled) {
this.log(
`Last login: ${this.lastLoginAttempt}, Debounce: ${this.loginAttemptDebounceMilliseconds}, Now: ${now}`
);
await this.login();
}
}
}
}

isDebounceCleared(now) {
return this.lastLoginAttempt + this.loginAttemptDebounceMilliseconds < now;
}

resetDebounceIfNeeded(now) {
if (now - this.lastLoginAttempt > 60 * 1000 * 60 * 12) {
this.loginAttemptDebounceMilliseconds = 1000;
}
}

async tryLogin(now) {
this.lastLoginAttempt = now;
await this.login();
}

async waitForDebounceClearance(now) {
this.log(
`Attempting to login before debounce has cleared, waiting ${this.loginAttemptDebounceMilliseconds / 1000} seconds`
);

let waitTime = 0;
while (waitTime < this.loginAttemptDebounceMilliseconds) {
await this.sleep(2000);
waitTime += 2000;
if (this.access_token) {
break;
}
}
}

async updateDebounceAndLogin(now) {
this.lastLoginAttempt = now;
this.loginAttemptDebounceMilliseconds = Math.min(
this.loginAttemptDebounceMilliseconds * 2,
1000 * 60 * 5
);
await this.login();
}


async refreshToken() {
const data = {
...this.getRequestData(),
Expand Down

0 comments on commit c62a0c7

Please sign in to comment.