Skip to content

Commit

Permalink
refactor: create smaller functions
Browse files Browse the repository at this point in the history
  • Loading branch information
dawidsowardx committed May 16, 2024
1 parent 696ef06 commit b5bdd7b
Showing 1 changed file with 54 additions and 35 deletions.
89 changes: 54 additions & 35 deletions src/chrome/offscreen/message-handler.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { WalletPublicKey } from 'chrome/offscreen/session-router'
import { LogsClient } from './logs-client'
import { errAsync, okAsync } from 'neverthrow'
import { AppLogger, logger as appLogger } from 'utils/logger'
Expand All @@ -9,7 +10,7 @@ import {
} from '../messages/_types'
import { WalletConnectionClient } from './wallet-connection/wallet-connection-client'
import { radixConnectConfig } from 'config'
import { Connections } from 'pairing/state/connections'
import { Connection, Connections } from 'pairing/state/connections'
import {
sessionRouter,
type walletConnectionClientFactory,
Expand All @@ -27,43 +28,61 @@ export const OffscreenMessageHandler = (input: {
const logger = input.logger
const connectionsMap = input.connectionsMap

return (message: Message): MessageHandlerOutput => {
switch (message?.discriminator) {
case messageDiscriminator.setConnections: {
const { connections } = message as { connections: Connections }
Object.entries(connections).forEach(([id, connection]) => {
if (connectionsMap.has(id)) {
const connectionClient = connectionsMap.get(
id,
) as WalletConnectionClient
connectionClient.update(connection)
} else {
logger?.debug(
'Creating WalletConnectionClient',
connection.walletName,
connection.walletPublicKey,
)
const destroyNonExistingConnections = (connections: Connections) => {
// Destroy & remove "WalletConnectionClient"s which do not have corresponding entry in connections configuration
// This happens when user clicks "forget wallet"
Array.from(connectionsMap.entries()).forEach(([id, connection]) => {
if (!connections[id]) {
connection.destroy()
connectionsMap.delete(id)
}
})
}

connectionsMap.set(
id,
input.walletConnectionClientFactory({
connection,
logger: input.logger || appLogger,
radixConnectConfiguration,
}),
)
}
})
const updateWalletConnectionClient = (
id: WalletPublicKey,
connection: Connection,
) => {
const connectionClient = connectionsMap.get(id) as WalletConnectionClient
connectionClient.update(connection)
}

// Destroy & remove "WalletConnectionClient"s which do not have corresponding entry in connections configuration
// This happens when user clicks "forget wallet"
Array.from(connectionsMap.entries()).forEach(([id, connection]) => {
if (!connections[id]) {
connection.destroy()
connectionsMap.delete(id)
}
})
const createWalletConnectionClient = (
id: WalletPublicKey,
connection: Connection,
) => {
logger?.debug(
'Creating WalletConnectionClient',
connection.walletName,
connection.walletPublicKey,
)

connectionsMap.set(
id,
input.walletConnectionClientFactory({
connection,
logger: input.logger || appLogger,
radixConnectConfiguration,
}),
)
}

const updateConnectionsMap = (connections: Connections) => {
Object.entries(connections).forEach(([id, connection]) => {
if (connectionsMap.has(id)) {
updateWalletConnectionClient(id, connection)
} else {
createWalletConnectionClient(id, connection)
}
})
}

return (message: Message): MessageHandlerOutput => {
switch (message?.discriminator) {
case messageDiscriminator.setConnections: {
const { connections } = message
updateConnectionsMap(connections)
destroyNonExistingConnections(connections)
return okAsync({ sendConfirmation: true })
}

Expand Down

0 comments on commit b5bdd7b

Please sign in to comment.