Skip to content

Commit

Permalink
feat: Added reload logic
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelKostroma committed Nov 1, 2024
1 parent bcc7f4c commit 5a1e995
Showing 1 changed file with 38 additions and 2 deletions.
40 changes: 38 additions & 2 deletions background/services/keyring/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
SignerImportSource,
} from "./types"
import { isSignerPrivateKeyType } from "./utils"
import { browser } from "../../index"

export const MAX_KEYRING_IDLE_TIME = 10 * MINUTE
export const MAX_OUTSIDE_IDLE_TIME = 10 * MINUTE
Expand Down Expand Up @@ -59,6 +60,13 @@ export default class KeyringService extends BaseService<KeyringServiceEvents> {
this.serviceAutoLockHandler()
},
},
autoReload: {
schedule: {
periodInMinutes: 720,
},
handler: () => this.dailyReload(),
runAtStart: false,
},
})

this.vaultManager = new VaultManager()
Expand Down Expand Up @@ -93,6 +101,8 @@ export default class KeyringService extends BaseService<KeyringServiceEvents> {
this.lastInternalWalletActivity = null

await this.notifyUIWithUpdates()

await this.shouldReloadHandler()
}

public async unlock(password: string): Promise<boolean> {
Expand All @@ -112,10 +122,34 @@ export default class KeyringService extends BaseService<KeyringServiceEvents> {
}
}

/**
* Restarts the wallet extension background worker.
* This function is triggered every 12 hours.
*/
private async dailyReload() {
if (this.isLocked()) {
chrome.runtime.reload()
return
}

await browser.storage.local.set({ shouldReload: true })
}

private async shouldReloadHandler() {
const { shouldReload } = await browser.storage.local.get("shouldReload")

if (!shouldReload) return

await browser.storage.local.set({ shouldReload: false })
chrome.runtime.reload()
}

// Locks the keyring if the time since last keyring or outside activity exceeds preset levels.
private serviceAutoLockHandler(): void {
if (!this.lastInternalWalletActivity || !this.lastExternalWalletActivity) {
this.notifyUIWithUpdates().then(() => this.walletManager.clearState())
this.notifyUIWithUpdates()
.then(() => this.walletManager.clearState())
.then(() => this.shouldReloadHandler())
return
}

Expand All @@ -127,7 +161,9 @@ export default class KeyringService extends BaseService<KeyringServiceEvents> {
timeSinceLastKeyringActivity >= MAX_KEYRING_IDLE_TIME ||
timeSinceLastOutsideActivity >= MAX_OUTSIDE_IDLE_TIME
) {
this.notifyUIWithUpdates().then(() => this.walletManager.clearState())
this.notifyUIWithUpdates()
.then(() => this.walletManager.clearState())
.then(() => this.shouldReloadHandler())
}
}

Expand Down

0 comments on commit 5a1e995

Please sign in to comment.