angular/devtools/projects/shell-browser/src/app/zone-aware-chrome-message-bus.ts
Joey Perrott 711cb41626 refactor(devtools): migrate devtools to prettier formatting (#53945)
Migrate formatting to prettier for devtools from clang-format

PR Close #53945
2024-01-19 19:09:54 +01:00

50 lines
1.3 KiB
TypeScript

/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {NgZone} from '@angular/core';
import {Events, MessageBus, Parameters} from 'protocol';
import {ChromeMessageBus} from './chrome-message-bus';
export class ZoneAwareChromeMessageBus extends MessageBus<Events> {
private _bus: ChromeMessageBus;
constructor(
port: chrome.runtime.Port,
private _ngZone: NgZone,
) {
super();
this._bus = new ChromeMessageBus(port);
}
override on<E extends keyof Events>(topic: E, cb: Events[E]): void {
this._bus.on(
topic,
function (this: ZoneAwareChromeMessageBus): void {
this._ngZone.run(() => (cb as any).apply(null, arguments));
}.bind(this),
);
}
override once<E extends keyof Events>(topic: E, cb: Events[E]): void {
this._bus.once(
topic,
function (this: ZoneAwareChromeMessageBus): void {
this._ngZone.run(() => (cb as any).apply(null, arguments));
}.bind(this),
);
}
override emit<E extends keyof Events>(topic: E, args?: Parameters<Events[E]>): boolean {
this._ngZone.run(() => this._bus.emit(topic, args));
return true;
}
override destroy(): void {
this._bus.destroy();
}
}