Skip to content
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
2 changes: 1 addition & 1 deletion packages/mask/src/plugins/Pets/settings.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createGlobalSettings } from '../../settings/createSettings'
import { PetsPluginID } from './constants'

export const petShowSettings = createGlobalSettings<boolean>(`${PetsPluginID}+selectedClosePet`, true)
export const petShowSettings = createGlobalSettings(`${PetsPluginID}+selectedClosePet`, true)
18 changes: 6 additions & 12 deletions packages/mask/src/plugins/Trader/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import { DataProvider } from '@masknet/public-api'
/**
* The slippage tolerance of trader
*/
export const currentSlippageSettings = createGlobalSettings<number>(`${PLUGIN_ID}+slippageTolerance`, SLIPPAGE_DEFAULT)
export const currentSlippageSettings = createGlobalSettings(`${PLUGIN_ID}+slippageTolerance`, SLIPPAGE_DEFAULT)

/**
* Single Hop
*/
export const currentSingleHopOnlySettings = createGlobalSettings<boolean>(`${PLUGIN_ID}+singleHopOnly`, false)
export const currentSingleHopOnlySettings = createGlobalSettings(`${PLUGIN_ID}+singleHopOnly`, false)

/**
* The default data provider
Expand All @@ -28,15 +28,9 @@ export interface TradeProviderSettings {
}

// #region the user preferred coin id
const coinGeckoPreferredCoinId = createInternalSettings<string>(`${PLUGIN_ID}+currentCoinGeckoPreferredCoinId`, '{}')
const coinMarketCapPreferredCoinId = createInternalSettings<string>(
`${PLUGIN_ID}+currentCoinMarketCapPreferredCoinId`,
'{}',
)
const coinUniswapPreferredCoinId = createInternalSettings<string>(
`${PLUGIN_ID}+currentCoinUniswapPreferredCoinId`,
'{}',
)
const coinGeckoPreferredCoinId = createInternalSettings(`${PLUGIN_ID}+currentCoinGeckoPreferredCoinId`, '{}')
const coinMarketCapPreferredCoinId = createInternalSettings(`${PLUGIN_ID}+currentCoinMarketCapPreferredCoinId`, '{}')
const coinUniswapPreferredCoinId = createInternalSettings(`${PLUGIN_ID}+currentCoinUniswapPreferredCoinId`, '{}')

export function getCurrentPreferredCoinIdSettings(dataProvider: DataProvider) {
switch (dataProvider) {
Expand All @@ -55,4 +49,4 @@ export function getCurrentPreferredCoinIdSettings(dataProvider: DataProvider) {
/**
* The approved tokens from uniswap
*/
export const approvedTokensFromUniswap = createInternalSettings<string>(`${PLUGIN_ID}+approvedTokens`, '[]')
export const approvedTokensFromUniswap = createInternalSettings(`${PLUGIN_ID}+approvedTokens`, '[]')
22 changes: 10 additions & 12 deletions packages/mask/src/plugins/Wallet/settings.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isEqual } from 'lodash-unified'
import { createGlobalSettings } from '../../settings/createSettings'
import { createComplexGlobalSettings, createGlobalSettings } from '../../settings/createSettings'
import {
ChainId,
NonFungibleAssetProvider,
Expand All @@ -12,15 +12,9 @@ import {
} from '@masknet/web3-shared-evm'
import { PLUGIN_ID } from './constants'

export const currentMaskWalletAccountSettings = createGlobalSettings<string>(
`${PLUGIN_ID}+selectedMaskWalletAddress`,
'',
)
export const currentMaskWalletAccountSettings = createGlobalSettings(`${PLUGIN_ID}+selectedMaskWalletAddress`, '')

export const currentMaskWalletChainIdSettings = createGlobalSettings<number>(
`${PLUGIN_ID}+maskWalletChainId`,
ChainId.Mainnet,
)
export const currentMaskWalletChainIdSettings = createGlobalSettings(`${PLUGIN_ID}+maskWalletChainId`, ChainId.Mainnet)

export const currentMaskWalletNetworkSettings = createGlobalSettings<NetworkType>(
`${PLUGIN_ID}+selectedMaskWalletNetwork`,
Expand All @@ -32,7 +26,7 @@ export const currentMaskWalletLockStatusSettings = createGlobalSettings<LockStat
LockStatus.INIT,
)

export const currentAccountSettings = createGlobalSettings<string>(`${PLUGIN_ID}+selectedWalletAddress`, '')
export const currentAccountSettings = createGlobalSettings(`${PLUGIN_ID}+selectedWalletAddress`, '')

export const currentChainIdSettings = createGlobalSettings<ChainId>(`${PLUGIN_ID}+chainId`, ChainId.Mainnet)

Expand All @@ -56,10 +50,14 @@ export const currentNonFungibleAssetDataProviderSettings = createGlobalSettings<
NonFungibleAssetProvider.OPENSEA,
)

export const currentGasOptionsSettings = createGlobalSettings<GasOptions | null>(
export const currentGasOptionsSettings = createComplexGlobalSettings<GasOptions | null>(
`${PLUGIN_ID}+gasOptions`,
null,
isEqual,
)

export const currentTokenPricesSettings = createGlobalSettings<CryptoPrice>(`${PLUGIN_ID}+tokenPrices`, {}, isEqual)
export const currentTokenPricesSettings = createComplexGlobalSettings<CryptoPrice>(
`${PLUGIN_ID}+tokenPrices`,
{},
isEqual,
)
42 changes: 32 additions & 10 deletions packages/mask/src/settings/createSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export type InternalSettings<T> = ValueRef<T> & {
readonly resolve: (value: T) => void
readonly reject: (e: Error) => void
}
type ValueComparer<T> = (a: T, b: T) => boolean
export type ValueComparer<T> = (a: T, b: T) => boolean
const defaultValueComparer = (a: any, b: any) => a === b

const cached: Map<string, InternalSettings<any>> = new Map()
Expand Down Expand Up @@ -44,10 +44,10 @@ MaskMessages.events.createInternalSettingsUpdated.on(async (payload) => {
settings.resolve(settings.value)
})

export function createInternalSettings<T extends browser.storage.StorageValue>(
export function createComplexInternalSettings<T extends browser.storage.StorageValue>(
key: string,
value: T,
comparer: ValueComparer<T> = defaultValueComparer,
comparer: ValueComparer<T>,
) {
const settings = new ValueRef(value, comparer) as InternalSettings<T>
const [readyPromise, resolve, reject] = defer<T>()
Expand Down Expand Up @@ -87,34 +87,49 @@ export function createInternalSettings<T extends browser.storage.StorageValue>(
return settings
}

export function createGlobalSettings<T extends browser.storage.StorageValue>(
export function createInternalSettings(key: string, defaultValue: number): InternalSettings<number>
export function createInternalSettings(key: string, defaultValue: string): InternalSettings<string>
export function createInternalSettings(key: string, defaultValue: boolean): InternalSettings<boolean>
export function createInternalSettings<T extends string | number>(key: string, defaultValue: T): InternalSettings<T>
export function createInternalSettings(key: string, defaultValue: any): InternalSettings<any> {
return createComplexInternalSettings(key, defaultValue, defaultValueComparer)
}

export function createComplexGlobalSettings<T extends browser.storage.StorageValue>(
key: string,
value: T,
comparer: ValueComparer<T> = defaultValueComparer,
comparer: ValueComparer<T>,
) {
const settings = createInternalSettings(`settings+${key}`, value, comparer)
const settings = createComplexInternalSettings(`settings+${key}`, value, comparer)
return settings
}
export function createGlobalSettings(key: string, defaultValue: number): InternalSettings<number>
export function createGlobalSettings(key: string, defaultValue: string): InternalSettings<string>
export function createGlobalSettings(key: string, defaultValue: boolean): InternalSettings<boolean>
export function createGlobalSettings<T extends string | number>(key: string, defaultValue: T): InternalSettings<T>
export function createGlobalSettings(key: string, defaultValue: any): InternalSettings<any> {
return createComplexGlobalSettings(key, defaultValue, defaultValueComparer)
}

export interface NetworkSettings<T> {
[networkKey: string]: ValueRef<T> & { ready: boolean; readyPromise: Promise<T> }
}

export function createNetworkSettings<T extends browser.storage.StorageValue>(
export function createComplexNetworkSettings<T extends browser.storage.StorageValue>(
settingsKey: string,
defaultValue: T,
comparer: ValueComparer<T> = defaultValueComparer,
comparer: ValueComparer<T>,
) {
const cached: NetworkSettings<T> = {}
MaskMessages.events.createNetworkSettingsReady.on((networkKey) => {
if (networkKey.startsWith('plugin:') || settingsKey === 'pluginsEnabled') return
if (!(networkKey in cached))
cached[networkKey] = createInternalSettings(`${networkKey}+${settingsKey}`, defaultValue, comparer)
cached[networkKey] = createComplexInternalSettings(`${networkKey}+${settingsKey}`, defaultValue, comparer)
})
return new Proxy(cached, {
get(target, networkKey: string) {
if (!(networkKey in target)) {
const settings = createInternalSettings(`${networkKey}+${settingsKey}`, defaultValue, comparer)
const settings = createComplexInternalSettings(`${networkKey}+${settingsKey}`, defaultValue, comparer)
target[networkKey] = settings
settings.readyPromise.then(() => MaskMessages.events.createNetworkSettingsReady.sendToAll(networkKey))
}
Expand All @@ -127,3 +142,10 @@ export function createNetworkSettings<T extends browser.storage.StorageValue>(
},
})
}
export function createNetworkSettings(key: string, defaultValue: number): NetworkSettings<number>
export function createNetworkSettings(key: string, defaultValue: string): NetworkSettings<string>
export function createNetworkSettings(key: string, defaultValue: boolean): NetworkSettings<boolean>
export function createNetworkSettings<T extends string | number>(key: string, defaultValue: T): NetworkSettings<T>
export function createNetworkSettings(key: string, defaultValue: any): NetworkSettings<any> {
return createComplexNetworkSettings(key, defaultValue, defaultValueComparer)
}
29 changes: 13 additions & 16 deletions packages/mask/src/settings/settings.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
import { createGlobalSettings, createNetworkSettings, NetworkSettings } from './createSettings'
import { createGlobalSettings, createNetworkSettings, createComplexNetworkSettings } from './createSettings'
import { LaunchPage } from './types'
import { Appearance } from '@masknet/theme'
import { LanguageOptions } from '@masknet/public-api'
import { updateLanguage } from '@masknet/shared-base'
import { PLUGIN_ID } from '../plugins/EVM/constants'
import { isEqual } from 'lodash-unified'

export const debugModeSetting = createGlobalSettings<boolean>('debugMode', false)
export const debugModeSetting = createGlobalSettings('debugMode', false)
export const appearanceSettings = createGlobalSettings<Appearance>('appearance', Appearance.default)
export const languageSettings = createGlobalSettings<LanguageOptions>('language', LanguageOptions.__auto__)
languageSettings.addListener(updateLanguage)
export const pluginIDSettings = createGlobalSettings<string>('pluginID', PLUGIN_ID)
export const pluginIDSettings = createGlobalSettings('pluginID', PLUGIN_ID)

export const currentSetupGuideStatus: NetworkSettings<string> = createNetworkSettings('currentSetupGuideStatus', '')
export const userGuideStatus: NetworkSettings<string> = createNetworkSettings('userGuideStatus', '')
export const sayHelloShowed: NetworkSettings<boolean> = createNetworkSettings('sayHelloShowed', false)
export const userPinExtension = createGlobalSettings<boolean>('userPinExtension', false)
export const dismissVerifyNextID: NetworkSettings<{ [key in string]: boolean }> = createNetworkSettings(
export const currentSetupGuideStatus = createNetworkSettings('currentSetupGuideStatus', '')
export const userGuideStatus = createNetworkSettings('userGuideStatus', '')
export const sayHelloShowed = createNetworkSettings('sayHelloShowed', false)
export const userPinExtension = createGlobalSettings('userPinExtension', false)
export const dismissVerifyNextID = createComplexNetworkSettings(
'dismissVerifyNextID',
{},
{} as Record<string, boolean>,
isEqual,
)
export const bioDescription: NetworkSettings<string> = createNetworkSettings('bioDescription', '')
export const personalHomepage: NetworkSettings<string> = createNetworkSettings('personalHomepage', '')
export const bioDescription = createNetworkSettings('bioDescription', '')
export const personalHomepage = createNetworkSettings('personalHomepage', '')
// This is a misuse of concept "NetworkSettings" as "namespaced settings"
// The refactor is tracked in https://github.com/DimensionDev/Maskbook/issues/1884
/**
Expand All @@ -32,12 +32,9 @@ export const personalHomepage: NetworkSettings<string> = createNetworkSettings('
* `useActivatedPluginsDashboard().find((x) => x.ID === PLUGIN_ID)` instead
*/
// This was "currentPluginEnabled" before, but we used it to represent minimal mode now to make the settings be able to migrate.
export const currentPluginMinimalModeNOTEnabled: NetworkSettings<boolean> = createNetworkSettings(
'pluginsEnabled',
true,
)
export const currentPluginMinimalModeNOTEnabled = createNetworkSettings('pluginsEnabled', true)
export const launchPageSettings = createGlobalSettings<LaunchPage>('launchPage', LaunchPage.dashboard)
export const currentPersonaIdentifier = createGlobalSettings<string>('currentPersonaIdentifier', '')
export const currentPersonaIdentifier = createGlobalSettings('currentPersonaIdentifier', '')

try {
// Migrate language settings
Expand Down