Skip to content

Commit

Permalink
[PM-4401] Re-add manual ngZone.run execution (#6647)
Browse files Browse the repository at this point in the history
* [PM-4401] feat: add browser messaging api service

* [PM-4401] feat: use new service

* [PM-4401] chore: rename to `ZonedMessageListenerService`

* [PM-4401] chore: remove polyfill
  • Loading branch information
coroiu authored Oct 20, 2023
1 parent 8dc81b6 commit c1494b8
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 51 deletions.
21 changes: 21 additions & 0 deletions apps/browser/src/platform/browser/run-inside-angular.operator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { NgZone } from "@angular/core";
import { MonoTypeOperatorFunction, Observable } from "rxjs";

export function runInsideAngular<T>(ngZone: NgZone): MonoTypeOperatorFunction<T> {
return (source: Observable<T>) =>
new Observable<T>((subscriber) => {
const subscription = source.subscribe({
next(value) {
ngZone.run(() => subscriber.next(value));
},
error(error: unknown) {
ngZone.run(() => subscriber.error(error));
},
complete() {
ngZone.run(() => subscriber.complete());
},
});

return () => subscription.unsubscribe();
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Injectable, NgZone } from "@angular/core";
import { Observable } from "rxjs";

import { BrowserApi } from "./browser-api";
import { runInsideAngular } from "./run-inside-angular.operator";

/**
* This service is used for listening to messages from the background script.
* It automatically runs all callbacks inside the Angular zone.
* This should be used instead of `BrowserApi.messageListener` in all popup-components.
* Not needed for services running in the background script.
*/
@Injectable({ providedIn: "root" })
export class ZonedMessageListenerService {
constructor(private ngZone: NgZone) {}

messageListener(
name: string,
callback: (
message: any,
sender: chrome.runtime.MessageSender,
sendResponse: any
) => boolean | void
) {
BrowserApi.messageListener(name, (message, sender, sendResponse) => {
return this.ngZone.run(() => callback(message, sender, sendResponse));
});
}

messageListener$(): Observable<unknown> {
return BrowserApi.messageListener$().pipe(runInsideAngular(this.ngZone));
}
}
44 changes: 0 additions & 44 deletions apps/browser/src/platform/polyfills/zone-patch-chrome-runtime.ts

This file was deleted.

6 changes: 4 additions & 2 deletions apps/browser/src/popup/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/pl
import { DialogService, SimpleDialogOptions } from "@bitwarden/components";

import { BrowserApi } from "../platform/browser/browser-api";
import { ZonedMessageListenerService } from "../platform/browser/zoned-message-listener.service";
import { BrowserStateService } from "../platform/services/abstractions/browser-state.service";

import { routerTransition } from "./app-routing.animations";
Expand Down Expand Up @@ -50,7 +51,8 @@ export class AppComponent implements OnInit, OnDestroy {
private ngZone: NgZone,
private sanitizer: DomSanitizer,
private platformUtilsService: PlatformUtilsService,
private dialogService: DialogService
private dialogService: DialogService,
private browserMessagingApi: ZonedMessageListenerService
) {}

async ngOnInit() {
Expand Down Expand Up @@ -128,7 +130,7 @@ export class AppComponent implements OnInit, OnDestroy {
};

(window as any).bitwardenPopupMainMessageListener = bitwardenPopupMainMessageListener;
BrowserApi.messageListener("app.component", bitwardenPopupMainMessageListener);
this.browserMessagingApi.messageListener("app.component", bitwardenPopupMainMessageListener);

// eslint-disable-next-line rxjs/no-async-subscribe
this.router.events.pipe(takeUntil(this.destroy$)).subscribe(async (event) => {
Expand Down
2 changes: 0 additions & 2 deletions apps/browser/src/popup/polyfills.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
import "core-js/stable";
import "zone.js";

import "../platform/polyfills/zone-patch-chrome-runtime";
10 changes: 7 additions & 3 deletions apps/browser/src/vault/popup/components/fido2/fido2.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { SecureNoteView } from "@bitwarden/common/vault/models/view/secure-note.
import { DialogService } from "@bitwarden/components";
import { PasswordRepromptService } from "@bitwarden/vault";

import { BrowserApi } from "../../../../platform/browser/browser-api";
import { ZonedMessageListenerService } from "../../../../platform/browser/zoned-message-listener.service";
import {
BrowserFido2Message,
BrowserFido2UserInterfaceSession,
Expand Down Expand Up @@ -78,7 +78,8 @@ export class Fido2Component implements OnInit, OnDestroy {
private settingsService: SettingsService,
private searchService: SearchService,
private logService: LogService,
private dialogService: DialogService
private dialogService: DialogService,
private browserMessagingApi: ZonedMessageListenerService
) {}

ngOnInit() {
Expand All @@ -93,7 +94,10 @@ export class Fido2Component implements OnInit, OnDestroy {
}))
);

combineLatest([queryParams$, BrowserApi.messageListener$() as Observable<BrowserFido2Message>])
combineLatest([
queryParams$,
this.browserMessagingApi.messageListener$() as Observable<BrowserFido2Message>,
])
.pipe(
concatMap(async ([queryParams, message]) => {
this.sessionId = queryParams.sessionId;
Expand Down

0 comments on commit c1494b8

Please sign in to comment.