Skip to content

Commit

Permalink
Move logging toggle to a safer location (#165)
Browse files Browse the repository at this point in the history
  • Loading branch information
fregante authored Oct 26, 2023
1 parent d75d3b8 commit 1460ff8
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 24 deletions.
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@
"license": "MIT",
"author": "Federico Brigante for PixieBrix <[email protected]> (https://www.pixiebrix.com)",
"type": "module",
"main": "distribution/index.js",
"exports": {
".": {
"types": "./distribution/index.js",
"default": "./distribution/index.js"
},
"./*": "./distribution/*"
},
"scripts": {
"build": "tsc",
"demo:watch": "parcel watch --no-cache --no-hmr",
Expand Down
2 changes: 1 addition & 1 deletion source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export * from "./receiver.js";
export * from "./sender.js";
export * from "./types.js";
export { getThisFrame, getTopLevelFrame } from "./thisTarget.js";
export { toggleLogging } from "./shared.js";
export { toggleLogging } from "./logging.js";

import { initPrivateApi } from "./thisTarget.js";

Expand Down
17 changes: 17 additions & 0 deletions source/logging.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* Warning: Do not use import browser-polyfill directly or indirectly */

// .bind preserves the call location in the console
const debug = console.debug.bind(console, "Messenger:");
const warn = console.warn.bind(console, "Messenger:");

const noop: (...args: unknown[]) => void = () => {
/* */
};

// Default to "no logs"
export const log = { debug: noop, warn: noop };

export function toggleLogging(enabled: boolean): void {
log.debug = enabled ? debug : noop;
log.warn = enabled ? warn : noop;
}
3 changes: 2 additions & 1 deletion source/receiver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {
type Method,
type Sender,
} from "./types.js";
import { isObject, MessengerError, log, __webextMessenger } from "./shared.js";
import { isObject, MessengerError, __webextMessenger } from "./shared.js";
import { log } from "./logging.js";
import { getActionForMessage } from "./thisTarget.js";
import { didUserRegisterMethods, handlers } from "./handlers.js";

Expand Down
3 changes: 2 additions & 1 deletion source/sender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import {
type PageTarget,
type AnyTarget,
} from "./types.js";
import { isObject, MessengerError, __webextMessenger, log } from "./shared.js";
import { isObject, MessengerError, __webextMessenger } from "./shared.js";
import { log } from "./logging.js";
import { type SetReturnType } from "type-fest";
import { handlers } from "./handlers.js";

Expand Down
18 changes: 0 additions & 18 deletions source/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,13 @@ export function isObject(value: unknown): value is Record<string, unknown> {
return typeof value === "object" && value !== null;
}

function noop() {
/* */
}

export class MessengerError extends Error {
override name = "MessengerError";
}

// @ts-expect-error Wrong `errorConstructors` types
errorConstructors.set("MessengerError", MessengerError);

// .bind preserves the call location in the console
const debug = console.debug.bind(console, "Messenger:");
const warn = console.warn.bind(console, "Messenger:");

export const log = { debug, warn };

export function toggleLogging(enabled: boolean): void {
log.debug = enabled ? debug : noop;
log.warn = enabled ? warn : noop;
}

// Default to "no logs"
toggleLogging(false);

export function isErrorObject(error: unknown): error is ErrorObject {
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- This is a type guard function and it uses ?.
return typeof (error as any)?.message === "string";
Expand Down
5 changes: 3 additions & 2 deletions source/thisTarget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import {
type Sender,
type FrameTarget,
} from "./types.js";
import { log, MessengerError, once } from "./shared.js";
import { MessengerError, once } from "./shared.js";
import { log } from "./logging.js";
import { type Entries } from "type-fest";

/**
Expand Down Expand Up @@ -152,7 +153,7 @@ export function __getTabData(this: MessengerMeta): AnyTarget {
}

export async function getThisFrame(): Promise<FrameTarget> {
await storeTabData(); // It should already have been called by we still need to await it
await storeTabData(); // It should already have been called but we still need to await it

const { tabId, frameId } = thisTarget;

Expand Down

0 comments on commit 1460ff8

Please sign in to comment.