mirror of
https://github.com/bitwarden/browser.git
synced 2024-12-22 16:29:09 +01:00
[PS-2303] MV3 Service Cache Enhancements (#4507)
* Add Should Listen Gate * Fixed ServiceCache Being Sent to Wrong Arg * Await Combined Listener
This commit is contained in:
parent
c5e99b807e
commit
9a98e8eb70
@ -21,7 +21,12 @@ if (BrowserApi.manifestVersion === 3) {
|
||||
chrome.tabs.onReplaced.addListener(tabsOnReplacedListener);
|
||||
chrome.tabs.onUpdated.addListener(tabsOnUpdatedListener);
|
||||
chrome.contextMenus.onClicked.addListener(contextMenusClickedListener);
|
||||
BrowserApi.messageListener("runtime.background", runtimeMessageListener);
|
||||
BrowserApi.messageListener(
|
||||
"runtime.background",
|
||||
(message: { command: string }, sender, sendResponse) => {
|
||||
runtimeMessageListener(message, sender);
|
||||
}
|
||||
);
|
||||
} else {
|
||||
const bitwardenMain = ((window as any).bitwardenMain = new MainBackground());
|
||||
bitwardenMain.bootstrap().then(() => {
|
||||
|
@ -120,13 +120,24 @@ export class CipherContextMenuHandler {
|
||||
await cipherContextMenuHandler.update(tab.url);
|
||||
}
|
||||
|
||||
static async messageListener(message: { command: string }, cachedServices: CachedServices) {
|
||||
static async messageListener(
|
||||
message: { command: string },
|
||||
sender: chrome.runtime.MessageSender,
|
||||
cachedServices: CachedServices
|
||||
) {
|
||||
if (!CipherContextMenuHandler.shouldListen(message)) {
|
||||
return;
|
||||
}
|
||||
const cipherContextMenuHandler = await CipherContextMenuHandler.create(cachedServices);
|
||||
await cipherContextMenuHandler.messageListener(message);
|
||||
}
|
||||
|
||||
async messageListener(message: { command: string }) {
|
||||
if (!LISTENED_TO_COMMANDS.includes(message.command)) {
|
||||
private static shouldListen(message: { command: string }) {
|
||||
return LISTENED_TO_COMMANDS.includes(message.command);
|
||||
}
|
||||
|
||||
async messageListener(message: { command: string }, sender?: chrome.runtime.MessageSender) {
|
||||
if (!CipherContextMenuHandler.shouldListen(message)) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -126,6 +126,7 @@ export class ContextMenuClickedHandler {
|
||||
|
||||
static async messageListener(
|
||||
message: { command: string; data: LockedVaultPendingNotificationsItem },
|
||||
sender: chrome.runtime.MessageSender,
|
||||
cachedServices: CachedServices
|
||||
) {
|
||||
if (
|
||||
|
@ -1,22 +1,24 @@
|
||||
import { combine } from "./combine";
|
||||
|
||||
describe("combine", () => {
|
||||
it("runs", () => {
|
||||
it("runs", async () => {
|
||||
const combined = combine([
|
||||
(arg: Record<string, unknown>, serviceCache: Record<string, unknown>) => {
|
||||
arg["one"] = true;
|
||||
serviceCache["one"] = true;
|
||||
return Promise.resolve();
|
||||
},
|
||||
(arg: Record<string, unknown>, serviceCache: Record<string, unknown>) => {
|
||||
if (serviceCache["one"] !== true) {
|
||||
throw new Error("One should have ran.");
|
||||
}
|
||||
arg["two"] = true;
|
||||
return Promise.resolve();
|
||||
},
|
||||
]);
|
||||
|
||||
const arg: Record<string, unknown> = {};
|
||||
combined(arg);
|
||||
await combined(arg);
|
||||
|
||||
expect(arg["one"]).toBeTruthy();
|
||||
|
||||
|
@ -1,15 +1,15 @@
|
||||
import { CachedServices } from "../background/service_factories/factory-options";
|
||||
|
||||
type Listener<T extends unknown[]> = (...args: [...T, CachedServices]) => void;
|
||||
type Listener<T extends unknown[]> = (...args: [...T, CachedServices]) => Promise<void>;
|
||||
|
||||
export const combine = <T extends unknown[]>(
|
||||
listeners: Listener<T>[],
|
||||
startingServices: CachedServices = {}
|
||||
) => {
|
||||
return (...args: T) => {
|
||||
return async (...args: T) => {
|
||||
const cachedServices = { ...startingServices };
|
||||
for (const listener of listeners) {
|
||||
listener(...[...args, cachedServices]);
|
||||
await listener(...[...args, cachedServices]);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
@ -23,7 +23,10 @@ const tabsOnUpdatedListener = combine([
|
||||
|
||||
const contextMenusClickedListener = ContextMenuClickedHandler.onClickedListener;
|
||||
|
||||
const runtimeMessageListener = combine([
|
||||
// TODO: All message listeners should be RuntimeMessage in Notifications follow up then this type annotation can be inferred
|
||||
const runtimeMessageListener = combine<
|
||||
[message: { command: string }, sender: chrome.runtime.MessageSender]
|
||||
>([
|
||||
UpdateBadge.messageListener,
|
||||
CipherContextMenuHandler.messageListener,
|
||||
ContextMenuClickedHandler.messageListener,
|
||||
|
Loading…
Reference in New Issue
Block a user