Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(background/storage): allow extension be in multiple states #393

Merged
merged 5 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 18 additions & 18 deletions src/background/services/storage.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import type {
AmountValue,
ExtensionState,
GrantDetails,
Storage,
StorageKey,
WalletAmount
} from '@/shared/types'
import { type Browser } from 'webextension-polyfill'
import { EventsService } from './events'
import { bigIntMax, ThrottleBatch } from '@/shared/helpers'
import { bigIntMax, objectEquals, ThrottleBatch } from '@/shared/helpers'
import { computeBalance } from '../utils'

const defaultStorage = {
Expand All @@ -18,8 +19,8 @@ const defaultStorage = {
* structural changes would need migrations for keeping compatibility with
* existing installations.
*/
version: 2,
state: null,
version: 3,
state: {},
connected: false,
enabled: true,
exceptionList: {},
Expand Down Expand Up @@ -126,23 +127,14 @@ export class StorageService {
return false
}

// TODO: ensure correct transitions between states, while also considering
// race conditions.
async setState(
state: null | Record<Exclude<Storage['state'], null>, boolean>
): Promise<boolean> {
async setState(state: Storage['state']): Promise<boolean> {
const { state: prevState } = await this.get(['state'])

let newState: Storage['state'] = null
if (state !== null) {
if (typeof state.missing_host_permissions === 'boolean') {
if (state.missing_host_permissions) {
newState = 'missing_host_permissions'
}
}
const newState: Storage['state'] = { ...prevState }
for (const key of Object.keys(state) as ExtensionState[]) {
newState[key] = state[key]
}

if (prevState === newState) {
if (prevState && objectEquals(prevState, newState)) {
return false
}

Expand Down Expand Up @@ -253,9 +245,17 @@ const MIGRATIONS: Record<Storage['version'], Migration> = {
}

if (data.hasHostPermissions === false) {
data.state = 'missing_host_permissions' satisfies Storage['state']
data.state = 'missing_host_permissions'
}

return [data, deleteKeys]
},
3: (data) => {
const newState =
data.state && typeof data.state === 'string'
? { [data.state as ExtensionState]: true }
: {}
data.state = newState satisfies Storage['state']
return [data]
}
}
2 changes: 1 addition & 1 deletion src/popup/components/ProtectedRoute.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { ROUTES_PATH } from '../Popup'
export const ProtectedRoute = () => {
const { state } = React.useContext(PopupStateContext)

if (state.state === 'missing_host_permissions') {
if (state.state.missing_host_permissions) {
return <Navigate to={ROUTES_PATH.MISSING_HOST_PERMISSION} />
}
if (state.connected === false) {
Expand Down
15 changes: 15 additions & 0 deletions src/shared/helpers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { objectEquals } from './helpers'

describe('objectEquals', () => {
it('should return true if objects are equal', () => {
expect(objectEquals({}, {})).toBe(true)
expect(objectEquals({ a: 1 }, { a: 1 })).toBe(true)
expect(objectEquals({ b: 2, a: 1 }, { a: 1, b: 2 })).toBe(true)
expect(objectEquals({ a: 1 }, { a: 1, b: undefined })).toBe(true)
})

it('should return false if objects are not equal', () => {
expect(objectEquals({ a: 1 }, { a: 2 })).toBe(false)
expect(objectEquals({ a: 1 }, { b: 1 })).toBe(false)
})
})
9 changes: 9 additions & 0 deletions src/shared/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,12 @@ export function convert(value: bigint, source: number, target: number) {
export function bigIntMax(a: string, b: string) {
return BigInt(a) > BigInt(b) ? a : b
}

type Primitive = string | number | boolean | null | undefined

// Warn: Not a nested object equals or a deepEquals function
sidvishnoi marked this conversation as resolved.
Show resolved Hide resolved
export function objectEquals<T extends Record<string, Primitive>>(a: T, b: T) {
const keysA = Object.keys(a)
const keysB = Object.keys(b)
return JSON.stringify(a, keysA.sort()) === JSON.stringify(b, keysB.sort())
}
12 changes: 6 additions & 6 deletions src/shared/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ export interface RecurringGrant extends GrantDetailsBase {
}
export type GrantDetails = OneTimeGrant | RecurringGrant

export type ExtensionState =
| never // just added for code formatting
/** Extension can't inject scripts and fetch resources from all hosts */
| 'missing_host_permissions'

export interface Storage {
/**
* Storage structure version. Used in migrations. Numbers are sequential.
Expand All @@ -48,12 +53,7 @@ export interface Storage {
/** If a wallet is connected or not */
connected: boolean
/** Extension state */
state:
| never // just added for code formatting
/** Normal */
| null
/** Extension can't inject scripts and fetch resources from all hosts */
| 'missing_host_permissions'
state: Partial<Record<ExtensionState, boolean>>

rateOfPay?: string | undefined | null
minRateOfPay?: string | undefined | null
Expand Down
Loading