From f574fa6c29c890506d6a7228e3d3f9474179c203 Mon Sep 17 00:00:00 2001 From: Dawid Sowa Date: Thu, 16 May 2024 19:00:52 +0200 Subject: [PATCH] fix: remove `everyTwoMinutes$` Move offscreen initialization messages to separate file --- src/chrome/offscreen/helpers/initialize.ts | 43 +++++++++++++++++++ src/chrome/offscreen/offscreen.ts | 48 +++------------------- 2 files changed, 48 insertions(+), 43 deletions(-) create mode 100644 src/chrome/offscreen/helpers/initialize.ts diff --git a/src/chrome/offscreen/helpers/initialize.ts b/src/chrome/offscreen/helpers/initialize.ts new file mode 100644 index 00000000..2a7fcda0 --- /dev/null +++ b/src/chrome/offscreen/helpers/initialize.ts @@ -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>( + createMessage.getSessionRouterData(), + ) + .andThen((data) => + messageClient.handleMessage( + createMessage.setSessionRouterData(data, 'offScreen'), + ), + ) + }, + connections: () => { + messageClient + .sendMessageAndWaitForConfirmation( + createMessage.getConnections('offScreen'), + ) + .andThen((connections) => + messageClient.handleMessage( + createMessage.setConnections('offScreen', connections), + ), + ) + }, + } +} diff --git a/src/chrome/offscreen/offscreen.ts b/src/chrome/offscreen/offscreen.ts index 063df64c..342444e3 100644 --- a/src/chrome/offscreen/offscreen.ts +++ b/src/chrome/offscreen/offscreen.ts @@ -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() @@ -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>( - 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( - 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