mirror of
https://github.com/bitwarden/browser.git
synced 2024-12-17 15:37:57 +01:00
refactor: create abstract ipc service in libs
This commit is contained in:
parent
0ab9618422
commit
3abc28aeb1
@ -1,17 +1,10 @@
|
||||
import { Injectable, OnDestroy } from "@angular/core";
|
||||
import { Observable, Subject, switchMap, takeUntil } from "rxjs";
|
||||
import { Observable } from "rxjs";
|
||||
|
||||
import { SdkService } from "@bitwarden/common/platform/abstractions/sdk/sdk.service";
|
||||
import { Destination } from "@bitwarden/sdk-internal";
|
||||
import { IpcLink, IpcMessage, IpcService } from "@bitwarden/platform";
|
||||
import { Destination, Manager } from "@bitwarden/sdk-internal";
|
||||
|
||||
import { IpcLink } from "./ipc-link";
|
||||
import { IpcMessage } from "./ipc-message";
|
||||
|
||||
@Injectable()
|
||||
export class IpcForegroundService implements OnDestroy {
|
||||
private destroy$ = new Subject<void>();
|
||||
|
||||
private linkToBackground = new IpcLink(
|
||||
export class IpcForegroundService extends IpcService {
|
||||
private static LinkToBackground = new IpcLink(
|
||||
async (data) => {
|
||||
await chrome.runtime.sendMessage({ payload: data } as IpcMessage);
|
||||
},
|
||||
@ -26,22 +19,7 @@ export class IpcForegroundService implements OnDestroy {
|
||||
[Destination.BrowserBackground],
|
||||
);
|
||||
|
||||
constructor(private sdkService: SdkService) {}
|
||||
|
||||
init() {
|
||||
this.sdkService.client$
|
||||
.pipe(
|
||||
switchMap(async (client) => {
|
||||
const manager = client.ipc().create_manager();
|
||||
await manager.register_link(this.linkToBackground);
|
||||
}),
|
||||
takeUntil(this.destroy$),
|
||||
)
|
||||
.subscribe();
|
||||
}
|
||||
|
||||
ngOnDestroy() {
|
||||
this.destroy$.next();
|
||||
this.destroy$.complete();
|
||||
protected override async registerLinks(manager: Manager): Promise<void> {
|
||||
await manager.register_link(IpcForegroundService.LinkToBackground);
|
||||
}
|
||||
}
|
||||
|
@ -1 +1,2 @@
|
||||
export * from "./services/browser-service";
|
||||
export * from "./ipc";
|
||||
|
3
libs/platform/src/ipc/index.ts
Normal file
3
libs/platform/src/ipc/index.ts
Normal file
@ -0,0 +1,3 @@
|
||||
export * from "./ipc-link";
|
||||
export * from "./ipc-message";
|
||||
export * from "./ipc.service";
|
11
libs/platform/src/ipc/ipc.service.ts
Normal file
11
libs/platform/src/ipc/ipc.service.ts
Normal file
@ -0,0 +1,11 @@
|
||||
import { Manager } from "@bitwarden/sdk-internal";
|
||||
|
||||
export abstract class IpcService {
|
||||
protected manager = new Manager();
|
||||
|
||||
async init() {
|
||||
await this.registerLinks(this.manager);
|
||||
}
|
||||
|
||||
protected abstract registerLinks(manager: Manager): Promise<void>;
|
||||
}
|
Loading…
Reference in New Issue
Block a user