Skip to content

Commit

Permalink
refactor: move some API to infra/.../context and move some others to …
Browse files Browse the repository at this point in the history
…web3-provider (#10775)
  • Loading branch information
Jack-Works committed Sep 16, 2023
1 parent dd4efff commit 5dcfe20
Show file tree
Hide file tree
Showing 74 changed files with 261 additions and 209 deletions.
5 changes: 0 additions & 5 deletions packages/app/src/helpers/createSharedContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ export function createSharedContext(): Omit<Plugin.SiteAdaptor.SiteAdaptorContex
connectPersona: reject,
createPersona: reject,
currentPersonaIdentifier: emptyValueRef,
currentVisitingProfile: UNDEFINED,
getPostURL,
getPostPayload,
getNextIDPlatform: () => undefined,
Expand All @@ -32,7 +31,6 @@ export function createSharedContext(): Omit<Plugin.SiteAdaptor.SiteAdaptorContex
getThemeSettings: () => ({ color: '', mode: ThemeMode.Light, size: FontSize.Normal, isDim: false }),
getWallets: reject,
hasPaymentPassword: reject,
lastRecognizedProfile: UNDEFINED,
openDashboard: reject,
openPopupWindow: reject,
fetchJSON: reject,
Expand All @@ -47,15 +45,12 @@ export function createSharedContext(): Omit<Plugin.SiteAdaptor.SiteAdaptorContex
queryPersonaByProfile: reject,
grantEIP2255Permission: reject,
disconnectAllWalletsFromOrigin: reject,
removeWallet: reject,
resetAllWallets: reject,
selectMaskWalletAccount: reject,
setMinimalMode: reject,
signWithPersona: reject,
signWithWallet: reject,
updateWallet: reject,
send: reject,
themeSettings: UNDEFINED,
allPersonas: EMPTY_ARRAY,
}
}
16 changes: 15 additions & 1 deletion packages/app/src/plugin-host/enable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,24 @@ import './register.js'

import { Emitter } from '@servie/events'
import { startPluginSiteAdaptor } from '@masknet/plugin-infra/content-script'
import { BooleanPreference, EnhanceableSite, createI18NBundle, i18NextInstance } from '@masknet/shared-base'
import {
BooleanPreference,
EMPTY_ARRAY,
EnhanceableSite,
UNDEFINED,
createI18NBundle,
i18NextInstance,
} from '@masknet/shared-base'
import { setupReactShadowRootEnvironment } from '@masknet/theme'
import { inMemoryStorage, indexedDBStorage } from '../setup/storage.js'
import { createSharedContext } from '../helpers/createSharedContext.js'
import { __setSiteAdaptorContext__ } from '@masknet/plugin-infra/content-script/context'

__setSiteAdaptorContext__({
lastRecognizedProfile: UNDEFINED,
currentVisitingProfile: UNDEFINED,
allPersonas: EMPTY_ARRAY,
})

startPluginSiteAdaptor(EnhanceableSite.App, {
minimalMode: {
Expand Down
22 changes: 7 additions & 15 deletions packages/mask/src/extension/dashboard/load-dashboard.tsx
Original file line number Diff line number Diff line change
@@ -1,31 +1,23 @@
import { IntegratedDashboard } from '../../../dashboard/entry.js'
import { startPluginDashboard } from '@masknet/plugin-infra/dashboard'
import { DashboardRoutes, MaskMessages, createSubscriptionFromAsync } from '@masknet/shared-base'
import { DashboardRoutes } from '@masknet/shared-base'
import { createPartialSharedUIContext, createPluginHost } from '../../../shared/plugin-infra/host.js'
import { createNormalReactRoot } from '../../utils/createNormalReactRoot.js'
import { RestPartOfPluginUIContextShared } from '../../utils/plugin-context-shared-ui.js'
import { NextSharedUIContext, RestPartOfPluginUIContextShared } from '../../utils/plugin-context-shared-ui.js'
import Services from '#services'
import { Modals } from '@masknet/shared'
import { __setUIContext__ } from '@masknet/plugin-infra/dom/context'

__setUIContext__({
allPersonas: NextSharedUIContext.allPersonas,
})
startPluginDashboard(
createPluginHost(
undefined,
(id, def, signal) => ({
...createPartialSharedUIContext(id, def, signal),
...RestPartOfPluginUIContextShared,
allPersonas: createSubscriptionFromAsync(
() => Services.Identity.queryOwnedPersonaInformation(true),
[],
(x) => {
const clearCurrentPersonaIdentifier = MaskMessages.events.currentPersonaIdentifier.on(x)
const clearPersonasChanged = MaskMessages.events.personasChanged.on(x)

return () => {
clearCurrentPersonaIdentifier()
clearPersonasChanged()
}
},
),
allPersonas: NextSharedUIContext.allPersonas,
}),
Services.Settings.getPluginMinimalModeEnabled,
Services.Helper.hasHostPermission,
Expand Down
6 changes: 5 additions & 1 deletion packages/mask/src/extension/popups/normal-client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@ import { createNormalReactRoot, hydrateNormalReactRoot } from '../../utils/index
import { createPluginHost, createPartialSharedUIContext } from '../../../shared/plugin-infra/host.js'
import Services from '#services'
import Popups from './UI.js'
import { RestPartOfPluginUIContextShared } from '../../utils/plugin-context-shared-ui.js'
import { NextSharedUIContext, RestPartOfPluginUIContextShared } from '../../utils/plugin-context-shared-ui.js'
import {
MaskMessages,
createSubscriptionFromAsync,
currentPersonaIdentifier,
pluginIDsSettings,
} from '@masknet/shared-base'
import { initialPersonaInformation } from '@masknet/shared'
import { __setUIContext__ } from '@masknet/plugin-infra/dom/context'

__setUIContext__({
allPersonas: NextSharedUIContext.allPersonas,
})
if (
location.hash === '#/personas' ||
(location.hash.includes('#/personas') && location.hash.includes('tab=Connected+Wallets'))
Expand Down
6 changes: 0 additions & 6 deletions packages/mask/src/initialization/WalletRef.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/mask/src/initialization/post-async-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import './storage.js'
import './telemetry-update.js'
import '../../shared-ui/locales_legacy/init.js'
import '../site-adaptors/index.js'
import './WalletRef.js'
import './walletSetup.js'

export { activateSiteAdaptorUI } from '../site-adaptor-infra/define.js'
11 changes: 11 additions & 0 deletions packages/mask/src/initialization/walletSetup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Services from '#services'
import { Providers } from '@masknet/web3-providers'
import { ProviderType } from '@masknet/web3-shared-evm'
import { NextSharedUIContext } from '../utils/plugin-context-shared-ui.js'

Providers[ProviderType.MaskWallet].setIOContext({
allPersonas: NextSharedUIContext.allPersonas,
resetAllWallets: Services.Wallet.resetAllWallets,
removeWallet: Services.Wallet.removeWallet,
renameWallet: Services.Wallet.renameWallet,
})
28 changes: 7 additions & 21 deletions packages/mask/src/site-adaptor-infra/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ import type { SiteAdaptorUI } from '@masknet/types'
import { type Plugin, startPluginSiteAdaptor, SiteAdaptorContextRef } from '@masknet/plugin-infra/content-script'
import { Modals, sharedUIComponentOverwrite, sharedUINetworkIdentifier, type ModalProps } from '@masknet/shared'
import {
createSubscriptionFromAsync,
createSubscriptionFromValueRef,
currentPersonaIdentifier,
currentSetupGuideStatus,
DashboardRoutes,
ECKeyIdentifier,
i18NextInstance,
MaskMessages,
queryRemoteI18NBundle,
type SetupGuideContext,
SetupGuideStep,
Expand All @@ -28,8 +26,9 @@ import { getCurrentIdentifier } from '../site-adaptors/utils.js'
import { attachReactTreeWithoutContainer, setupReactShadowRootEnvironment } from '../utils/index.js'
import '../utils/debug/general.js'
import { configureSelectorMissReporter } from '../utils/startWatch.js'
import { RestPartOfPluginUIContextShared } from '../utils/plugin-context-shared-ui.js'
import { NextSharedUIContext, RestPartOfPluginUIContextShared } from '../utils/plugin-context-shared-ui.js'
import { definedSiteAdaptorsUI } from './define.js'
import { __setSiteAdaptorContext__ } from '@masknet/plugin-infra/content-script/context'

const definedSiteAdaptorsResolved = new Map<string, SiteAdaptorUI.Definition>()

Expand Down Expand Up @@ -129,21 +128,6 @@ export async function activateSiteAdaptorUIInner(ui_deferred: SiteAdaptorUI.Defe

signal.addEventListener('abort', queryRemoteI18NBundle(Services.Helper.queryRemoteI18NBundle))

const allPersonaSub = createSubscriptionFromAsync(
() => Services.Identity.queryOwnedPersonaInformation(true),
[],
(x) => {
const clearCurrentPersonaIdentifier = MaskMessages.events.currentPersonaIdentifier.on(x)
const clearOwnPersonaChanged = MaskMessages.events.ownPersonaChanged.on(x)

return () => {
clearCurrentPersonaIdentifier()
clearOwnPersonaChanged()
}
},
signal,
)

const lastRecognizedSub = createSubscriptionFromValueRef(ui.collecting.identityProvider.recognized, signal)
const currentVisitingSub = createSubscriptionFromValueRef(
ui.collecting.currentVisitingIdentityProvider.recognized,
Expand All @@ -159,12 +143,14 @@ export async function activateSiteAdaptorUIInner(ui_deferred: SiteAdaptorUI.Defe
})
}

SiteAdaptorContextRef.value = {
...RestPartOfPluginUIContextShared,
__setSiteAdaptorContext__({
lastRecognizedProfile: lastRecognizedSub,
currentVisitingProfile: currentVisitingSub,
allPersonas: NextSharedUIContext.allPersonas,
})
SiteAdaptorContextRef.value = {
...RestPartOfPluginUIContextShared,
currentPersonaIdentifier,
allPersonas: allPersonaSub,
themeSettings: themeSettingsSub,
getThemeSettings: () => ui.configuration.themeSettings,
getNextIDPlatform: () => ui.configuration.nextIDConfig?.platform,
Expand Down
18 changes: 15 additions & 3 deletions packages/mask/src/utils/plugin-context-shared-ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,21 @@ import { WalletConnectQRCodeModal } from '@masknet/shared'
import Services from '#services'
import type { PartialSharedUIContext } from '../../shared/plugin-infra/host.js'

export const NextSharedUIContext = {
allPersonas: createSubscriptionFromAsync(
() => Services.Identity.queryOwnedPersonaInformation(true),
[],
(x) => {
const clearCurrentPersonaIdentifier = MaskMessages.events.currentPersonaIdentifier.on(x)
const clearPersonasChanged = MaskMessages.events.personasChanged.on(x)

return () => {
clearCurrentPersonaIdentifier()
clearPersonasChanged()
}
},
),
}
export const RestPartOfPluginUIContextShared: Omit<
Plugin.SiteAdaptor.SiteAdaptorContext,
| keyof PartialSharedUIContext
Expand All @@ -28,7 +43,6 @@ export const RestPartOfPluginUIContextShared: Omit<
| 'connectPersona'
| 'createPersona'
| 'currentPersonaIdentifier'
| 'allPersonas'
| 'getSearchedKeyword'
| 'setWeb3State'
> = {
Expand Down Expand Up @@ -88,8 +102,6 @@ export const RestPartOfPluginUIContextShared: Omit<
getWallets: Services.Wallet.getWallets,
addWallet: Services.Wallet.addWallet,
updateWallet: Services.Wallet.updateWallet,
removeWallet: Services.Wallet.removeWallet,
resetAllWallets: Services.Wallet.resetAllWallets,

hasPaymentPassword: Services.Wallet.hasPassword,
}
10 changes: 10 additions & 0 deletions packages/plugin-infra/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
"mask-src": "./src/entry-dom.ts",
"default": "./dist/entry-dom.js"
},
"./dom/context": {
"types": "./dist/dom/context.d.ts",
"mask-src": "./src/dom/context.ts",
"default": "./dist/dom/context.js"
},
"./background-worker": {
"types": "./dist/entry-background-worker.d.ts",
"mask-src": "./src/entry-background-worker.ts",
Expand All @@ -28,6 +33,11 @@
"types": "./dist/entry-content-script.d.ts",
"mask-src": "./src/entry-content-script.ts",
"default": "./dist/entry-content-script.js"
},
"./content-script/context": {
"types": "./dist/site-adaptor/context.d.ts",
"mask-src": "./src/site-adaptor/context.ts",
"default": "./dist/site-adaptor/context.js"
}
},
"types": "./dist/entry.d.ts",
Expand Down
13 changes: 13 additions & 0 deletions packages/plugin-infra/src/dom/context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// This file will be virtualized in the future.
// Currently all plugins access the same value, but we can virtualize them in the future.

import type { PersonaInformation } from '@masknet/shared-base'
import type { Subscription } from 'use-subscription'

export interface __UIContext__ {
allPersonas: Subscription<readonly PersonaInformation[]>
}
export let allPersonas: __UIContext__['allPersonas']
export function __setUIContext__(value: __UIContext__) {
allPersonas = value.allPersonas
}
1 change: 0 additions & 1 deletion packages/plugin-infra/src/dom/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ export { useActivatedPluginWeb3State } from './useActivatedPluginWeb3State.js'
export { useActivatedPluginWeb3UI } from './useActivatedPluginWeb3UI.js'
export { useAllPluginsWeb3State } from './useAllPluginsWeb3State.js'
export { useSiteAdaptorContext, SiteAdaptorContextRef } from './useSiteAdaptorContext.js'
export { WalletServiceRef, type WalletBackupProvider } from './useWalletService.js'
export { PluginI18NFieldRender, type PluginI18NFieldRenderProps, usePluginI18NField } from './useI18N.js'

export { type PluginWrapperMethods, type PluginWrapperComponent, usePluginWrapper } from './usePluginWrapper.js'
7 changes: 0 additions & 7 deletions packages/plugin-infra/src/dom/useWalletService.ts

This file was deleted.

19 changes: 19 additions & 0 deletions packages/plugin-infra/src/site-adaptor/context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// This file will be virtualized in the future.
// Currently all plugins access the same value, but we can virtualize them in the future.

import type { Subscription } from 'use-subscription'
import type { IdentityResolved } from '../types.js'
import { __setUIContext__, type __UIContext__ } from '../dom/context.js'

export * from '../dom/context.js'
export interface __SiteAdaptorContext__ extends __UIContext__ {
lastRecognizedProfile: Subscription<IdentityResolved | undefined>
currentVisitingProfile: Subscription<IdentityResolved | undefined>
}
export let lastRecognizedProfile: __SiteAdaptorContext__['lastRecognizedProfile']
export let currentVisitingProfile: __SiteAdaptorContext__['currentVisitingProfile']
export function __setSiteAdaptorContext__(value: __SiteAdaptorContext__) {
__setUIContext__(value)
lastRecognizedProfile = value.lastRecognizedProfile
currentVisitingProfile = value.currentVisitingProfile
}
6 changes: 2 additions & 4 deletions packages/plugin-infra/src/site-adaptor/useAllPersonas.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { useSubscription } from 'use-subscription'
import { EMPTY_ARRAY } from '@masknet/shared-base'
import { useSiteAdaptorContext } from '../dom/useSiteAdaptorContext.js'
import { allPersonas } from '../dom/context.js'

export function useAllPersonas() {
const { allPersonas } = useSiteAdaptorContext()
return useSubscription(allPersonas ?? EMPTY_ARRAY)
return useSubscription(allPersonas)
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { useSubscription } from 'use-subscription'
import { UNDEFINED } from '@masknet/shared-base'
import { useSiteAdaptorContext } from '../dom/useSiteAdaptorContext.js'
import { allPersonas } from '../dom/context.js'

export function useCurrentPersonaInformation() {
const { currentPersona, allPersonas } = useSiteAdaptorContext()
const { currentPersona } = useSiteAdaptorContext()
const current = useSubscription(currentPersona ?? UNDEFINED)
const personas = useSubscription(allPersonas ?? UNDEFINED)
const personas = useSubscription(allPersonas)
return personas?.find((x) => x.identifier.rawPublicKey === current?.rawPublicKey)
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { useSubscription } from 'use-subscription'
import { UNDEFINED } from '@masknet/shared-base'
import { useSiteAdaptorContext } from '../dom/useSiteAdaptorContext.js'
import { currentVisitingProfile } from './context.js'

export function useCurrentVisitingIdentity() {
const { currentVisitingProfile } = useSiteAdaptorContext()
return useSubscription(currentVisitingProfile ?? UNDEFINED)
return useSubscription(currentVisitingProfile)
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { useSubscription } from 'use-subscription'
import { UNDEFINED } from '@masknet/shared-base'
import { useSiteAdaptorContext } from '../dom/useSiteAdaptorContext.js'
import { lastRecognizedProfile } from './context.js'

export function useLastRecognizedIdentity() {
const { lastRecognizedProfile } = useSiteAdaptorContext()
return useSubscription(lastRecognizedProfile ?? UNDEFINED)
}
9 changes: 0 additions & 9 deletions packages/plugin-infra/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ export namespace Plugin.Shared {

export interface SharedUIContext extends SharedContext {
setWeb3State(state: Web3State<any, any, any, any, any, any, any, any>): void
allPersonas?: Subscription<PersonaInformation[]>
/** The selected persona */
currentPersona: Subscription<PersonaIdentifier | undefined>
/** Get all wallets */
Expand Down Expand Up @@ -182,12 +181,6 @@ export namespace Plugin.Shared {
},
): Promise<void>

/** Remove a old wallet */
removeWallet(id: string, password?: string): Promise<void>

/** Reset all wallets */
resetAllWallets(): Promise<void>

/** Send request to native API, for a risky request will be added into the waiting queue. */
send(payload: JsonRpcPayload, options?: TransactionOptions): Promise<JsonRpcResponse>
hasPaymentPassword(): Promise<boolean>
Expand Down Expand Up @@ -336,8 +329,6 @@ export namespace Plugin.Shared {
/** This part runs in the Site Adaptor */
export namespace Plugin.SiteAdaptor {
export interface SiteAdaptorContext extends Shared.SharedUIContext {
lastRecognizedProfile: Subscription<IdentityResolved | undefined>
currentVisitingProfile: Subscription<IdentityResolved | undefined>
themeSettings: Subscription<ThemeSettings | undefined>
/** The default theme settings. */
getThemeSettings: () => ThemeSettings | undefined
Expand Down

0 comments on commit 5dcfe20

Please sign in to comment.