From b1ee65dca80c625a506beb11537d74ec6c95f58f Mon Sep 17 00:00:00 2001 From: Justin Baur <19896123+justindbaur@users.noreply.github.com> Date: Thu, 22 Dec 2022 13:29:02 -0500 Subject: [PATCH] Use Multi-Messaging Service (#4304) Co-authored-by: Daniel James Smith Co-authored-by: Matt Gibson --- .../browser/src/background/main.background.ts | 33 +++++++++++-------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/apps/browser/src/background/main.background.ts b/apps/browser/src/background/main.background.ts index bd70ef57c9..1182124e01 100644 --- a/apps/browser/src/background/main.background.ts +++ b/apps/browser/src/background/main.background.ts @@ -211,9 +211,25 @@ export default class MainBackground { const logoutCallback = async (expired: boolean, userId?: string) => await this.logout(expired, userId); - this.messagingService = this.popupOnlyContext - ? new BrowserMessagingPrivateModeBackgroundService() - : new BrowserMessagingService(); + const messagingServices: MessagingServiceAbstraction[] = []; + if (!isPrivateMode) { + // Sent to detached background + messagingServices.push(new BrowserMessagingService()); + } + + if (this.popupOnlyContext) { + // Sent to popup + messagingServices.push(new BrowserMessagingPrivateModeBackgroundService()); + } + + this.messagingService = new (class extends MessagingServiceAbstraction { + // AuthService should send the messages to the background not popup. + send = (subscriber: string, arg: any = {}) => { + for (const messagingService of messagingServices) { + messagingService.send(subscriber, arg); + } + }; + })(); this.logService = new ConsoleLogService(false); this.cryptoFunctionService = new WebCryptoFunctionService(window); this.storageService = new BrowserLocalStorageService(); @@ -349,22 +365,13 @@ export default class MainBackground { this.twoFactorService = new TwoFactorService(this.i18nService, this.platformUtilsService); - // eslint-disable-next-line - const that = this; - const backgroundMessagingService = new (class extends MessagingServiceAbstraction { - // AuthService should send the messages to the background not popup. - send = (subscriber: string, arg: any = {}) => { - const message = Object.assign({}, { command: subscriber }, arg); - that.runtimeBackground.processMessage(message, that, null); - }; - })(); this.authService = new AuthService( this.cryptoService, this.apiService, this.tokenService, this.appIdService, this.platformUtilsService, - backgroundMessagingService, + this.messagingService, this.logService, this.keyConnectorService, this.environmentService,