webext-messenger 0.17.0-0 → 0.17.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -32,18 +32,21 @@ action) {
32
32
  const { trace = [] } = options;
33
33
  trace.push(sender);
34
34
  const meta = { trace };
35
- debug(type, "↘️ received", { sender, args, wasForwarded: trace.length > 1 });
36
35
  let handleMessage;
37
36
  if (action === "forward") {
38
37
  debug(type, "🔀 forwarded", { sender, target });
39
38
  handleMessage = async () => messenger(type, meta, target, ...args);
40
39
  }
41
40
  else {
41
+ debug(type, "↘️ received in", getContextName(), {
42
+ sender,
43
+ args,
44
+ wasForwarded: trace.length > 1,
45
+ });
42
46
  const localHandler = handlers.get(type);
43
47
  if (!localHandler) {
44
48
  throw new MessengerError(`No handler registered for ${type} in ${getContextName()}`);
45
49
  }
46
- debug(type, "➡️ will be handled here,", getContextName());
47
50
  handleMessage = async () => localHandler.apply(meta, args);
48
51
  }
49
52
  const response = await handleMessage().then((value) => ({ value }), (error) => ({
@@ -62,7 +65,7 @@ export function registerMethods(methods) {
62
65
  if (handlers.has(type)) {
63
66
  throw new MessengerError(`Handler already set for ${type}`);
64
67
  }
65
- console.debug("Messenger: Registered", type);
68
+ debug("Registered", type);
66
69
  handlers.set(type, method);
67
70
  }
68
71
  browser.runtime.onMessage.addListener(onMessageListener);
@@ -1,3 +1,15 @@
1
+ const logging = (() => {
2
+ try {
3
+ // @ts-expect-error it would break Webpack
4
+ return process.env.WEBEXT_MESSENGER_LOGGING === "true";
5
+ }
6
+ catch {
7
+ return false;
8
+ }
9
+ })();
10
+ function noop() {
11
+ /* */
12
+ }
1
13
  export const __webextMessenger = true;
2
14
  export function isObject(value) {
3
15
  return typeof value === "object" && value !== null;
@@ -15,8 +27,8 @@ export class MessengerError extends Error {
15
27
  }
16
28
  export const handlers = new Map();
17
29
  // .bind preserves the call location in the console
18
- export const debug = console.debug.bind(console, "Messenger:");
19
- export const warn = console.warn.bind(console, "Messenger:");
30
+ export const debug = logging ? console.debug.bind(console, "Messenger:") : noop;
31
+ export const warn = logging ? console.warn.bind(console, "Messenger:") : noop;
20
32
  export function isErrorObject(error) {
21
33
  var _a;
22
34
  // eslint-disable-next-line @typescript-eslint/no-explicit-any -- This is a type guard function and it uses ?.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "webext-messenger",
3
- "version": "0.17.0-0",
3
+ "version": "0.17.0",
4
4
  "description": "Browser Extension component messaging framework",
5
5
  "keywords": [],
6
6
  "repository": "pixiebrix/webext-messenger",
@@ -18,7 +18,7 @@
18
18
  },
19
19
  "scripts": {
20
20
  "build": "tsc",
21
- "demo:watch": "parcel watch --no-cache --no-hmr",
21
+ "demo:watch": "WEBEXT_MESSENGER_LOGGING=true parcel watch --no-cache --no-hmr",
22
22
  "demo:build": "parcel build --no-cache",
23
23
  "prepack": "tsc --sourceMap false",
24
24
  "test": "eslint . && tsc --noEmit",