Skip to content

Commit

Permalink
Improve naming on auto lock interval
Browse files Browse the repository at this point in the history
  • Loading branch information
hyphenized committed Jun 16, 2023
1 parent e0f0251 commit abce01a
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 46 deletions.
6 changes: 3 additions & 3 deletions background/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1796,9 +1796,9 @@ export default class Main extends BaseService<never> {
}
})

uiSliceEmitter.on("updateAutoLockTimer", async (newTimerValue) => {
await this.preferenceService.updateAutoLockTimer(newTimerValue)
await this.internalSignerService.updateAutoLockTimer()
uiSliceEmitter.on("updateAutoLockInterval", async (newTimerValue) => {
await this.preferenceService.updateAutoLockInterval(newTimerValue)
await this.internalSignerService.updateAutoLockInterval()
this.store.dispatch(setAutoLockTimer(newTimerValue))
})
}
Expand Down
6 changes: 3 additions & 3 deletions background/redux-slices/migrations/to-32.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { MINUTE } from "../../constants"

const DEFAULT_AUTOLOCK_TIMER = 60 * MINUTE
const DEFAULT_AUTOLOCK_INTERVAL = 60 * MINUTE

type OldState = {
ui: {
Expand All @@ -16,7 +16,7 @@ type NewState = {
ui: {
settings: {
[settingsKey: string]: unknown
autoLockTimer: number
autoLockInterval: number
}
[sliceKey: string]: unknown
}
Expand All @@ -32,7 +32,7 @@ export default (prevState: Record<string, unknown>): NewState => {
...typedPrevState.ui,
settings: {
...typedPrevState.ui.settings,
autoLockTimer: DEFAULT_AUTOLOCK_TIMER,
autoLockInterval: DEFAULT_AUTOLOCK_INTERVAL,
},
},
}
Expand Down
16 changes: 8 additions & 8 deletions background/redux-slices/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { AccountSignerSettings } from "../ui"
import { AccountState, addAddressNetwork } from "./accounts"
import { createBackgroundAsyncThunk } from "./utils"
import { UNIXTime } from "../types"
import { DEFAULT_AUTOLOCK_TIMER } from "../services/preferences/defaults"
import { DEFAULT_AUTOLOCK_INTERVAL } from "../services/preferences/defaults"

export const defaultSettings = {
hideDust: false,
Expand All @@ -20,7 +20,7 @@ export const defaultSettings = {
showAnalyticsNotification: false,
showUnverifiedAssets: false,
hideBanners: false,
autoLockTimer: DEFAULT_AUTOLOCK_TIMER,
autoLockInterval: DEFAULT_AUTOLOCK_INTERVAL,
}

export type UIState = {
Expand All @@ -36,7 +36,7 @@ export type UIState = {
showAnalyticsNotification: boolean
showUnverifiedAssets: boolean
hideBanners: boolean
autoLockTimer: UNIXTime
autoLockInterval: UNIXTime
}
snackbarMessage: string
routeHistoryEntries?: Partial<Location>[]
Expand All @@ -56,7 +56,7 @@ export type Events = {
newSelectedNetwork: EVMNetwork
updateAnalyticsPreferences: Partial<AnalyticsPreferences>
addCustomNetworkResponse: [string, boolean]
updateAutoLockTimer: number
updateAutoLockInterval: number
}

export const emitter = new Emittery<Events>()
Expand Down Expand Up @@ -279,16 +279,16 @@ export const addNetworkUserResponse = createBackgroundAsyncThunk(
}
)

export const updateAutoLockTimer = createBackgroundAsyncThunk(
"ui/updateAutoLockTimer",
export const updateAutoLockInterval = createBackgroundAsyncThunk(
"ui/updateAutoLockInterval",
async (newValue: string) => {
const parsedValue = parseInt(newValue, 10)

if (Number.isNaN(parsedValue) || parsedValue <= 1) {
throw new Error("Invalid value for auto lock timer")
}

emitter.emit("updateAutoLockTimer", parsedValue)
emitter.emit("updateAutoLockInterval", parsedValue)
}
)

Expand Down Expand Up @@ -347,7 +347,7 @@ export const selectHideDust = createSelector(

export const selectAutoLockTimer = createSelector(
selectSettings,
(settings) => settings.autoLockTimer
(settings) => settings.autoLockInterval
)

export const selectSnackbarMessage = createSelector(
Expand Down
18 changes: 9 additions & 9 deletions background/services/internal-signer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { FeatureFlags, isEnabled } from "../../features"
import { AddressOnNetwork } from "../../accounts"
import logger from "../../lib/logger"
import PreferenceService from "../preferences"
import { DEFAULT_AUTOLOCK_TIMER } from "../preferences/defaults"
import { DEFAULT_AUTOLOCK_INTERVAL } from "../preferences/defaults"

export enum SignerInternalTypes {
mnemonicBIP39S128 = "mnemonic#bip39:128",
Expand Down Expand Up @@ -169,7 +169,7 @@ export default class InternalSignerService extends BaseService<Events> {
*/
lastOutsideActivity: UNIXTime | undefined

#internalAutoLockTimer: UNIXTime = DEFAULT_AUTOLOCK_TIMER
#internalAutoLockInterval: UNIXTime = DEFAULT_AUTOLOCK_INTERVAL

static create: ServiceCreatorFunction<
Events,
Expand Down Expand Up @@ -198,8 +198,8 @@ export default class InternalSignerService extends BaseService<Events> {
// it is. Don't emit if there are no vaults to unlock.
await super.internalStartService()

this.#internalAutoLockTimer =
await this.preferenceService.getAutoLockTimer()
this.#internalAutoLockInterval =
await this.preferenceService.getAutoLockInterval()

if ((await getEncryptedVaults()).vaults.length > 0) {
this.emitter.emit("locked", this.locked())
Expand All @@ -212,9 +212,9 @@ export default class InternalSignerService extends BaseService<Events> {
await super.internalStopService()
}

async updateAutoLockTimer(): Promise<void> {
this.#internalAutoLockTimer =
await this.preferenceService.getAutoLockTimer()
async updateAutoLockInterval(): Promise<void> {
this.#internalAutoLockInterval =
await this.preferenceService.getAutoLockInterval()

await this.autolockIfNeeded()
}
Expand Down Expand Up @@ -366,9 +366,9 @@ export default class InternalSignerService extends BaseService<Events> {
const timeSinceLastActivity = now - this.lastActivity
const timeSinceLastOutsideActivity = now - this.lastOutsideActivity

if (timeSinceLastActivity >= this.#internalAutoLockTimer) {
if (timeSinceLastActivity >= this.#internalAutoLockInterval) {
this.lock()
} else if (timeSinceLastOutsideActivity >= this.#internalAutoLockTimer) {
} else if (timeSinceLastOutsideActivity >= this.#internalAutoLockInterval) {
this.lock()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ describe("InternalSignerService when autolocking", () => {

it("will autolock after the keyring idle time but not sooner", async () => {
// eslint-disable-next-line @typescript-eslint/dot-notation
const maxIdleTime = await service["preferenceService"].getAutoLockTimer()
const maxIdleTime = await service["preferenceService"].getAutoLockInterval()

expect(service.locked()).toEqual(false)

Expand All @@ -419,7 +419,7 @@ describe("InternalSignerService when autolocking", () => {

it("will autolock after the outside activity idle time but not sooner", async () => {
// eslint-disable-next-line @typescript-eslint/dot-notation
const maxIdleTime = await service["preferenceService"].getAutoLockTimer()
const maxIdleTime = await service["preferenceService"].getAutoLockInterval()

expect(service.locked()).toEqual(false)

Expand Down Expand Up @@ -460,7 +460,7 @@ describe("InternalSignerService when autolocking", () => {
},
])("will bump keyring activity idle time when $action", async ({ call }) => {
// eslint-disable-next-line @typescript-eslint/dot-notation
const maxIdleTime = await service["preferenceService"].getAutoLockTimer()
const maxIdleTime = await service["preferenceService"].getAutoLockInterval()

jest.spyOn(Date, "now").mockReturnValue(dateNowValue + maxIdleTime - 1)

Expand All @@ -483,7 +483,7 @@ describe("InternalSignerService when autolocking", () => {

it("will bump the outside activity idle time when outside activity is marked", async () => {
// eslint-disable-next-line @typescript-eslint/dot-notation
const maxIdleTime = await service["preferenceService"].getAutoLockTimer()
const maxIdleTime = await service["preferenceService"].getAutoLockInterval()

jest.spyOn(Date, "now").mockReturnValue(dateNowValue + maxIdleTime - 1)

Expand All @@ -508,17 +508,17 @@ describe("InternalSignerService when autolocking", () => {
// eslint-disable-next-line @typescript-eslint/dot-notation, prefer-destructuring
const preferenceService = service["preferenceService"]

const maxIdleTime = await preferenceService.getAutoLockTimer()
const maxIdleTime = await preferenceService.getAutoLockInterval()

await service.generateNewKeyring(SignerInternalTypes.mnemonicBIP39S256)

expect(service.locked()).toBe(false)

callAutolockHandler(maxIdleTime / 2)

await preferenceService.updateAutoLockTimer(maxIdleTime / 2)
await preferenceService.updateAutoLockInterval(maxIdleTime / 2)

await service.updateAutoLockTimer()
await service.updateAutoLockInterval()

expect(service.locked()).toEqual(true)
})
Expand Down
10 changes: 5 additions & 5 deletions background/services/preferences/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Dexie, { Transaction } from "dexie"
import { FiatCurrency } from "../../assets"
import { AddressOnNetwork } from "../../accounts"

import DEFAULT_PREFERENCES, { DEFAULT_AUTOLOCK_TIMER } from "./defaults"
import DEFAULT_PREFERENCES, { DEFAULT_AUTOLOCK_INTERVAL } from "./defaults"
import { AccountSignerSettings } from "../../ui"
import { AccountSignerWithId } from "../../signing"
import { AnalyticsPreferences } from "./types"
Expand Down Expand Up @@ -42,7 +42,7 @@ export interface Preferences {
isEnabled: boolean
hasDefaultOnBeenTurnedOn: boolean
}
autoLockTimer: UNIXTime
autoLockInterval: UNIXTime
}

export class PreferenceDatabase extends Dexie {
Expand Down Expand Up @@ -335,7 +335,7 @@ export class PreferenceDatabase extends Dexie {
.toCollection()
.modify((storedPreferences: Preferences) => {
const update: Partial<Preferences> = {
autoLockTimer: DEFAULT_AUTOLOCK_TIMER,
autoLockInterval: DEFAULT_AUTOLOCK_INTERVAL,
}

Object.assign(storedPreferences, update)
Expand All @@ -358,11 +358,11 @@ export class PreferenceDatabase extends Dexie {
return this.preferences.reverse().first() as Promise<Preferences>
}

async setAutoLockTimer(newValue: number): Promise<void> {
async setAutoLockInterval(newValue: number): Promise<void> {
await this.preferences
.toCollection()
.modify((storedPreferences: Preferences) => {
const update: Partial<Preferences> = { autoLockTimer: newValue }
const update: Partial<Preferences> = { autoLockInterval: newValue }

Object.assign(storedPreferences, update)
})
Expand Down
4 changes: 2 additions & 2 deletions background/services/preferences/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ETHEREUM, MINUTE, USD } from "../../constants"
import { storageGatewayURL } from "../../lib/storage-gateway"
import { Preferences } from "./types"

export const DEFAULT_AUTOLOCK_TIMER = 60 * MINUTE
export const DEFAULT_AUTOLOCK_INTERVAL = 60 * MINUTE

const defaultPreferences: Preferences = {
tokenLists: {
Expand Down Expand Up @@ -35,7 +35,7 @@ const defaultPreferences: Preferences = {
isEnabled: false,
hasDefaultOnBeenTurnedOn: false,
},
autoLockTimer: DEFAULT_AUTOLOCK_TIMER,
autoLockTimer: DEFAULT_AUTOLOCK_INTERVAL,
}

export default defaultPreferences
8 changes: 4 additions & 4 deletions background/services/preferences/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,12 +253,12 @@ export default class PreferenceService extends BaseService<Events> {
return (await this.db.getPreferences())?.defaultWallet
}

async getAutoLockTimer(): Promise<number> {
return (await this.db.getPreferences()).autoLockTimer
async getAutoLockInterval(): Promise<number> {
return (await this.db.getPreferences()).autoLockInterval
}

async updateAutoLockTimer(newValue: number): Promise<void> {
return this.db.setAutoLockTimer(newValue)
async updateAutoLockInterval(newValue: number): Promise<void> {
return this.db.setAutoLockInterval(newValue)
}

async setDefaultWalletValue(newDefaultWalletValue: boolean): Promise<void> {
Expand Down
10 changes: 5 additions & 5 deletions ui/pages/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import {
selectHideBanners,
selectShowUnverifiedAssets,
toggleShowUnverifiedAssets,
selectAutoLockTimer,
updateAutoLockTimer,
selectAutoLockTimer as selectAutoLockInterval,
updateAutoLockInterval,
} from "@tallyho/tally-background/redux-slices/ui"
import { useHistory } from "react-router-dom"
import { selectMainCurrencySign } from "@tallyho/tally-background/redux-slices/selectors"
Expand Down Expand Up @@ -325,7 +325,7 @@ export default function Settings(): ReactElement {
{ label: "60", value: String(60 * MINUTE) },
]

const autoLockTimer = useBackgroundSelector(selectAutoLockTimer)
const autoLockInterval = useBackgroundSelector(selectAutoLockInterval)

const autoLockSettings = {
title: "",
Expand All @@ -343,10 +343,10 @@ export default function Settings(): ReactElement {
<SharedSelect
options={autoLockOptions}
defaultIndex={autoLockOptions.findIndex(
({ value }) => value === String(autoLockTimer)
({ value }) => value === String(autoLockInterval)
)}
width="100%"
onChange={(newValue) => dispatch(updateAutoLockTimer(newValue))}
onChange={(newValue) => dispatch(updateAutoLockInterval(newValue))}
/>
</div>
<style jsx>
Expand Down

0 comments on commit abce01a

Please sign in to comment.