Skip to content

Commit

Permalink
Fix expectContext
Browse files Browse the repository at this point in the history
  • Loading branch information
fregante committed Jan 21, 2024
1 parent 9584538 commit 03b2427
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/contrib/uipath/LocalProcessOptions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const LocalProcessOptions: React.FunctionComponent<BlockOptionProps> = ({
}) => {
// Crash the React tree, because it's a programming error to use this configuration outside the dev tools
expectContext(
"devTools",
"pageEditor",
"useLocalRobot only works in the Page Editor due to its `thisTab` usage",
);

Expand Down
28 changes: 12 additions & 16 deletions src/utils/expectContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
isContentScript,
isExtensionContext,
isWebPage,
contextNames,
} from "webext-detect-page";

function isBrowserSidebar(): boolean {
Expand Down Expand Up @@ -52,18 +51,15 @@ function createError(
return new errorOrCtor(defaultMessage);
}

// eslint-disable-next-line local-rules/persistBackgroundData -- Static
const contexts = [...contextNames, "sidebar", "pageEditor"] as const;

// eslint-disable-next-line local-rules/persistBackgroundData -- Functions
const contextMap = new Map<(typeof contexts)[number], () => boolean>([
["web", isWebPage],
["extension", isExtensionContext],
["background", isBackground],
["pageEditor", isPageEditor],
["contentScript", isContentScript],
["sidebar", isBrowserSidebar],
]);
const contextMap = {
web: isWebPage,
extension: isExtensionContext,
background: isBackground,
pageEditor: isPageEditor,
contentScript: isContentScript,
sidebar: isBrowserSidebar,
} as const;

/**
* @example expectContext('extension')
Expand All @@ -72,10 +68,10 @@ const contextMap = new Map<(typeof contexts)[number], () => boolean>([
* @example expectContext('extension', new Error('Wrong context and this is my custom error'))
*/
export function expectContext(
context: (typeof contexts)[number],
context: keyof typeof contextMap,
error?: ErrorBaseType,
): void {
const isContext = contextMap.get(context);
const isContext = contextMap[context];
if (!isContext) {
throw new TypeError(`Context "${context}" not found`);
}
Expand All @@ -95,10 +91,10 @@ export function expectContext(
* @example forbidContext('extension', new Error('Wrong context and this is my custom error'))
*/
export function forbidContext(
context: (typeof contexts)[number],
context: keyof typeof contextMap,
error?: ErrorBaseType,
): void {
const isContext = contextMap.get(context);
const isContext = contextMap[context];
if (!isContext) {
throw new TypeError(`Context "${context}" not found`);
}
Expand Down

0 comments on commit 03b2427

Please sign in to comment.