Skip to content

Commit

Permalink
fix: remove everyTwoMinutes$
Browse files Browse the repository at this point in the history
Move offscreen initialization messages to separate file
  • Loading branch information
dawidsowardx committed May 16, 2024
1 parent c03ee92 commit f574fa6
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 43 deletions.
43 changes: 43 additions & 0 deletions src/chrome/offscreen/helpers/initialize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { createMessage } from 'chrome/messages/create-message'
import { MessageClient } from 'chrome/messages/message-client'
import { ConnectorExtensionOptions } from 'options'
import { SessionId, WalletPublicKey } from '../session-router'
import { Connections } from 'pairing/state/connections'

export const initialize = (messageClient: MessageClient) => {
return {
options: () => {
messageClient
.sendMessageAndWaitForConfirmation<{
options: ConnectorExtensionOptions
}>(createMessage.getExtensionOptions('offScreen'))
.andThen(({ options }) =>
messageClient.handleMessage(
createMessage.setConnectorExtensionOptions('offScreen', options),
),
)
},
sessionRouterData: () => {
messageClient
.sendMessageAndWaitForConfirmation<Record<SessionId, WalletPublicKey>>(
createMessage.getSessionRouterData(),
)
.andThen((data) =>
messageClient.handleMessage(
createMessage.setSessionRouterData(data, 'offScreen'),
),
)
},
connections: () => {
messageClient
.sendMessageAndWaitForConfirmation<Connections>(
createMessage.getConnections('offScreen'),
)
.andThen((connections) =>
messageClient.handleMessage(
createMessage.setConnections('offScreen', connections),
),
)
},
}
}
48 changes: 5 additions & 43 deletions src/chrome/offscreen/offscreen.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import { logger as utilsLogger } from 'utils/logger'
import { createMessage } from 'chrome/messages/create-message'
import { OffscreenMessageHandler } from 'chrome/offscreen/message-handler'
import { MessageClient } from 'chrome/messages/message-client'
import { Message } from 'chrome/messages/_types'
import { switchMap, timer } from 'rxjs'
import { ConnectorExtensionOptions } from 'options'
import { LogsClient } from './logs-client'
import { Connections } from 'pairing/state/connections'
import { WalletConnectionClient } from './wallet-connection/wallet-connection-client'
import { walletConnectionClientFactory } from './wallet-connection/factory'
import { WalletPublicKey, SessionId } from './session-router'
import { initialize } from './helpers/initialize'

const logsClient = LogsClient()

Expand All @@ -36,47 +32,13 @@ chrome.runtime.onMessage.addListener((message: Message, sender) => {
messageClient.onMessage(message, sender.tab?.id)
})

messageClient
.sendMessageAndWaitForConfirmation<{ options: ConnectorExtensionOptions }>(
createMessage.getExtensionOptions('offScreen'),
)
.andThen(({ options }) =>
messageClient.handleMessage(
createMessage.setConnectorExtensionOptions('offScreen', options),
),
)
const init = initialize(messageClient)

messageClient
.sendMessageAndWaitForConfirmation<Record<SessionId, WalletPublicKey>>(
createMessage.getSessionRouterData(),
)
.andThen((data) =>
messageClient.handleMessage(
createMessage.setSessionRouterData(data, 'offScreen'),
),
)

const TWO_MINUTES = 120_000
const everyTwoMinute$ = timer(0, TWO_MINUTES)

everyTwoMinute$
.pipe(
switchMap(() =>
messageClient
.sendMessageAndWaitForConfirmation<Connections>(
createMessage.getConnections('offScreen'),
)
.andThen((connections) =>
messageClient.handleMessage(
createMessage.setConnections('offScreen', connections),
),
),
),
)
.subscribe()
init.options()
init.sessionRouterData()
init.connections()

declare global {
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
interface Window {
radix: {
messageClient: MessageClient
Expand Down

0 comments on commit f574fa6

Please sign in to comment.