Skip to content

Commit

Permalink
fix for lag when opening sidebar using toggle sidebar brick (#8295)
Browse files Browse the repository at this point in the history
* fix for lag when opening sidebar using toggle sidebar brick

* memoize sidebar open check
  • Loading branch information
grahamlangford authored Apr 19, 2024
1 parent bc5d406 commit 4f2d3fc
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions src/contentScript/sidebarController.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,28 @@ import { showMySidePanel } from "@/background/messenger/strict/api";
import { getSidebarElement } from "@/contentScript/sidebarDomControllerLite";
import focusController from "@/utils/focusController";
import selectionController from "@/utils/selectionController";
import { messenger } from "webext-messenger";
import { getSidebarTargetForCurrentTab } from "@/utils/sidePanelUtils";

const HIDE_SIDEBAR_EVENT_NAME = "pixiebrix:hideSidebar";

/*
* Only one check at a time
* Cannot throttle because subsequent checks need to be able to be made immediately
*/
const isSidePanelOpenMv3 = memoizeUntilSettled(async () => {
try {
await messenger(
"SIDEBAR_PING",
{ retry: false },
await getSidebarTargetForCurrentTab(),
);
return true;
} catch {
return false;
}
});

export const isSidePanelOpen = isMV3()
? isSidePanelOpenMv3
: sidebarMv2.isSidebarFrameVisible;
Expand All @@ -69,15 +88,6 @@ const pingSidebar = memoizeUntilSettled(
}, 1000) as () => Promise<void>,
);

async function isSidePanelOpenMv3() {
try {
await pingSidebar();
return true;
} catch {
return false;
}
}

/**
* Event listeners triggered when the sidebar shows and is ready to receive messages.
*/
Expand Down

0 comments on commit 4f2d3fc

Please sign in to comment.