@widy/sdk 1.0.10 → 1.0.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import type { WidgetMutation, WidgetQuery, WidgetSubscription } from "./types";
|
|
2
2
|
export default class WidgetOutboundBridge {
|
|
3
|
+
private timeout;
|
|
3
4
|
private readonly pendingRequests;
|
|
4
5
|
private readonly subscriptionHandlers;
|
|
5
|
-
|
|
6
|
-
constructor();
|
|
6
|
+
constructor(timeout?: number);
|
|
7
7
|
private handleMessage;
|
|
8
|
-
action<P, R = unknown>(scope: WidgetQuery | WidgetMutation,
|
|
8
|
+
action<P, R = unknown>(scope: WidgetQuery | WidgetMutation, arg?: P): Promise<R>;
|
|
9
9
|
subscribe<T>(scope: WidgetSubscription, onMessage: (message: T) => void): () => void;
|
|
10
10
|
destroy(): void;
|
|
11
11
|
}
|
|
@@ -1,12 +1,13 @@
|
|
|
1
|
-
class WidgetOutboundBridge {
|
|
2
|
-
constructor() {
|
|
1
|
+
export default class WidgetOutboundBridge {
|
|
2
|
+
constructor(timeout = 5000) {
|
|
3
|
+
this.timeout = timeout;
|
|
3
4
|
this.pendingRequests = new Map();
|
|
4
5
|
this.subscriptionHandlers = new Map();
|
|
5
6
|
this.handleMessage = (event) => {
|
|
6
7
|
var _a;
|
|
7
|
-
const { id,
|
|
8
|
+
const { id, data, error } = event.data;
|
|
8
9
|
if (this.subscriptionHandlers.has(id)) {
|
|
9
|
-
(_a = this.subscriptionHandlers.get(id)) === null || _a === void 0 ? void 0 : _a(
|
|
10
|
+
(_a = this.subscriptionHandlers.get(id)) === null || _a === void 0 ? void 0 : _a(data);
|
|
10
11
|
return;
|
|
11
12
|
}
|
|
12
13
|
if (!this.pendingRequests.has(id))
|
|
@@ -14,25 +15,25 @@ class WidgetOutboundBridge {
|
|
|
14
15
|
const { resolve, reject } = this.pendingRequests.get(id);
|
|
15
16
|
this.pendingRequests.delete(id);
|
|
16
17
|
if (error) {
|
|
17
|
-
reject(
|
|
18
|
+
reject(error);
|
|
18
19
|
}
|
|
19
20
|
else {
|
|
20
|
-
resolve(
|
|
21
|
+
resolve(data);
|
|
21
22
|
}
|
|
22
23
|
};
|
|
23
24
|
window.addEventListener("message", this.handleMessage);
|
|
24
25
|
}
|
|
25
|
-
action(scope,
|
|
26
|
+
action(scope, arg) {
|
|
26
27
|
return new Promise((resolve, reject) => {
|
|
27
28
|
const id = crypto.randomUUID();
|
|
28
29
|
this.pendingRequests.set(id, { resolve, reject });
|
|
29
|
-
window.parent.postMessage({ id, scope,
|
|
30
|
+
window.parent.postMessage({ id, scope, arg }, "*");
|
|
30
31
|
setTimeout(() => {
|
|
31
32
|
if (this.pendingRequests.has(id)) {
|
|
32
33
|
this.pendingRequests.delete(id);
|
|
33
34
|
reject(new Error(`Parent request "${scope}" timed out`));
|
|
34
35
|
}
|
|
35
|
-
},
|
|
36
|
+
}, this.timeout);
|
|
36
37
|
});
|
|
37
38
|
}
|
|
38
39
|
subscribe(scope, onMessage) {
|
|
@@ -49,5 +50,3 @@ class WidgetOutboundBridge {
|
|
|
49
50
|
window.removeEventListener("message", this.handleMessage);
|
|
50
51
|
}
|
|
51
52
|
}
|
|
52
|
-
WidgetOutboundBridge.REQUEST_TIMEOUT_MS = 5000;
|
|
53
|
-
export default WidgetOutboundBridge;
|
package/dist/types.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@widy/sdk",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.12",
|
|
4
4
|
"description": "A TypeScript SDK for Widy widget integrations.",
|
|
5
5
|
"license": "ISC",
|
|
6
6
|
"author": "ik1s3v",
|
|
@@ -23,6 +23,7 @@
|
|
|
23
23
|
],
|
|
24
24
|
"scripts": {
|
|
25
25
|
"build": "tsc",
|
|
26
|
+
"publish": "npm run build && npm publish --access public",
|
|
26
27
|
"prepublishOnly": "npm run build"
|
|
27
28
|
},
|
|
28
29
|
"devDependencies": {
|