webext-messenger 0.21.0-0 → 0.22.0-1

Sign up to get free protection for your applications and to get access to all the features.
@@ -14,4 +14,5 @@ export declare const debug: (...args: any[]) => void;
14
14
  export declare const warn: (...args: any[]) => void;
15
15
  export declare function isErrorObject(error: unknown): error is ErrorObject;
16
16
  export declare function delay(milliseconds: number): Promise<void>;
17
+ export declare function once<Callback extends (...arguments_: unknown[]) => unknown>(function_: Callback): Callback;
17
18
  export {};
@@ -40,3 +40,14 @@ export async function delay(milliseconds) {
40
40
  setTimeout(resolve, milliseconds);
41
41
  });
42
42
  }
43
+ export function once(function_) {
44
+ let called = false;
45
+ let returnValue;
46
+ return function (...arguments_) {
47
+ if (called) {
48
+ returnValue = function_.apply(this, arguments_);
49
+ called = true;
50
+ }
51
+ return returnValue;
52
+ };
53
+ }
@@ -1,4 +1,5 @@
1
1
  import { AnyTarget, Message, MessengerMeta, Sender } from "./types.js";
2
2
  export declare function getActionForMessage(from: Sender, message: Message): "respond" | "forward" | "ignore";
3
3
  export declare function __getTabData(this: MessengerMeta): AnyTarget;
4
+ export declare function getThisTarget(): Promise<AnyTarget>;
4
5
  export declare function initPrivateApi(): void;
@@ -1,7 +1,7 @@
1
1
  import { isBackground, isContentScript, isExtensionContext, } from "webext-detect-page";
2
2
  import { messenger } from "./sender.js";
3
3
  import { registerMethods } from "./receiver.js";
4
- import { debug, MessengerError } from "./shared.js";
4
+ import { debug, MessengerError, once } from "./shared.js";
5
5
  /**
6
6
  * @file This file exists because `runtime.sendMessage` acts as a broadcast to
7
7
  * all open extension pages, so the receiver needs a way to figure out if the
@@ -91,7 +91,7 @@ export function getActionForMessage(from, message) {
91
91
  }
92
92
  return isThisTarget ? "respond" : "ignore";
93
93
  }
94
- async function storeTabData() {
94
+ const storeTabData = once(async () => {
95
95
  if (tabDataStatus !== "needed") {
96
96
  return;
97
97
  }
@@ -108,13 +108,17 @@ async function storeTabData() {
108
108
  // @ts-expect-error TODO: update lib to accept Error#cause
109
109
  { cause: error });
110
110
  }
111
- }
111
+ });
112
112
  export function __getTabData() {
113
113
  return { tabId: this.trace[0]?.tab?.id, frameId: this.trace[0]?.frameId };
114
114
  }
115
+ export async function getThisTarget() {
116
+ await storeTabData(); // It should already have been called by we still need to await it
117
+ return thisTarget;
118
+ }
115
119
  export function initPrivateApi() {
116
120
  if (isExtensionContext()) {
117
- // Only `runtime` pages can handle this message but I can't remove it because its listener
121
+ // Only `runtime` pages can handle this message but I can't remove it because its listener
118
122
  // also serves the purpose of throwing a specific error when no methods have been registered.
119
123
  // https://github.com/pixiebrix/webext-messenger/pull/80
120
124
  registerMethods({ __getTabData });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "webext-messenger",
3
- "version": "0.21.0-0",
3
+ "version": "0.22.0-1",
4
4
  "description": "Browser Extension component messaging framework",
5
5
  "keywords": [],
6
6
  "repository": "pixiebrix/webext-messenger",